Make ArrayType and RecordType simpler
Signed-off-by: David Arendsen <darendsen@gamepoint.com>
This commit is contained in:
parent
cd938243fc
commit
fa99c1f393
4 changed files with 28 additions and 46 deletions
11
src/Type.php
11
src/Type.php
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Arendsen\FluxQueryBuilder;
|
namespace Arendsen\FluxQueryBuilder;
|
||||||
|
|
||||||
|
use Arendsen\FluxQueryBuilder\Type\TypeInterface;
|
||||||
use Arendsen\FluxQueryBuilder\Type\ArrayType;
|
use Arendsen\FluxQueryBuilder\Type\ArrayType;
|
||||||
use Arendsen\FluxQueryBuilder\Type\BooleanType;
|
use Arendsen\FluxQueryBuilder\Type\BooleanType;
|
||||||
use Arendsen\FluxQueryBuilder\Type\TimeType;
|
use Arendsen\FluxQueryBuilder\Type\TimeType;
|
||||||
|
|
@ -14,15 +15,9 @@ class Type
|
||||||
*/
|
*/
|
||||||
protected $value;
|
protected $value;
|
||||||
|
|
||||||
/**
|
public function __construct($value)
|
||||||
* @var Settings|null $settings
|
|
||||||
*/
|
|
||||||
protected $settings;
|
|
||||||
|
|
||||||
public function __construct($value, Settings $settings = null)
|
|
||||||
{
|
{
|
||||||
$this->value = $value;
|
$this->value = $value;
|
||||||
$this->settings = $settings ? $settings : Settings::set([]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
|
|
@ -38,7 +33,7 @@ class Type
|
||||||
case 'boolean':
|
case 'boolean':
|
||||||
return new BooleanType($this->value);
|
return new BooleanType($this->value);
|
||||||
case 'array':
|
case 'array':
|
||||||
return new ArrayType($this->value, $this->settings);
|
return new ArrayType($this->value);
|
||||||
case 'NULL':
|
case 'NULL':
|
||||||
return 'null';
|
return 'null';
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
|
|
@ -7,43 +7,27 @@ use Arendsen\FluxQueryBuilder\Type;
|
||||||
|
|
||||||
class ArrayType implements TypeInterface
|
class ArrayType implements TypeInterface
|
||||||
{
|
{
|
||||||
public const SETTING_IS_NESTED_ARRAY = 'isNestedArray';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array $value
|
* @var array $value
|
||||||
*/
|
*/
|
||||||
protected $value;
|
protected $value;
|
||||||
|
|
||||||
/**
|
public function __construct(array $value)
|
||||||
* @var Settings|null $settings
|
|
||||||
*/
|
|
||||||
protected $settings;
|
|
||||||
|
|
||||||
public function __construct(array $value, Settings $settings = null)
|
|
||||||
{
|
{
|
||||||
$this->value = $value;
|
$this->value = $value;
|
||||||
$this->settings = $settings ? $settings : Settings::set([]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
if ($this->settings->get(RecordType::SETTING_IS_RECORD)) {
|
|
||||||
return new RecordType($this->value);
|
|
||||||
}
|
|
||||||
|
|
||||||
array_walk($this->value, function (&$value, $key) {
|
array_walk($this->value, function (&$value, $key) {
|
||||||
if ($this->isAssociativeArray($key)) {
|
if ($this->isAssociativeArray($key)) {
|
||||||
$value = $key . ': ' . new Type($value, Settings::set([
|
$value = $key . ': ' . $this->getPrefix($value) . new Type($value) . $this->getSuffix($value);
|
||||||
self::SETTING_IS_NESTED_ARRAY => is_array($value)
|
|
||||||
]));
|
|
||||||
} else {
|
} else {
|
||||||
$value = new Type($value, Settings::set([
|
$value = $this->getPrefix($value) . new Type($value) . $this->getSuffix($value);
|
||||||
self::SETTING_IS_NESTED_ARRAY => is_array($value)
|
|
||||||
]));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return $this->getPrefix() . implode(', ', $this->value) . $this->getSuffix();
|
return implode(', ', $this->value);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function isAssociativeArray($key): bool
|
protected function isAssociativeArray($key): bool
|
||||||
|
|
@ -51,18 +35,13 @@ class ArrayType implements TypeInterface
|
||||||
return is_string($key);
|
return is_string($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function isNestedArray(): bool
|
protected function getPrefix($value): string
|
||||||
{
|
{
|
||||||
return $this->settings->get(self::SETTING_IS_NESTED_ARRAY) ? true : false;
|
return is_array($value) ? '[' : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getPrefix(): string
|
protected function getSuffix($value): string
|
||||||
{
|
{
|
||||||
return $this->isNestedArray() ? '[' : '';
|
return is_array($value) ? ']' : '';
|
||||||
}
|
|
||||||
|
|
||||||
protected function getSuffix(): string
|
|
||||||
{
|
|
||||||
return $this->isNestedArray() ? ']' : '';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,11 @@ class FieldRecordType implements TypeInterface
|
||||||
{
|
{
|
||||||
public const SETTING_IS_RECORD = 'isRecord';
|
public const SETTING_IS_RECORD = 'isRecord';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array $value
|
||||||
|
*/
|
||||||
|
private $value;
|
||||||
|
|
||||||
public function __construct(array $value)
|
public function __construct(array $value)
|
||||||
{
|
{
|
||||||
$this->value = $value;
|
$this->value = $value;
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,14 @@
|
||||||
|
|
||||||
namespace Arendsen\FluxQueryBuilder\Type;
|
namespace Arendsen\FluxQueryBuilder\Type;
|
||||||
|
|
||||||
use Arendsen\FluxQueryBuilder\Settings;
|
|
||||||
use Arendsen\FluxQueryBuilder\Type;
|
use Arendsen\FluxQueryBuilder\Type;
|
||||||
|
|
||||||
class RecordType implements TypeInterface
|
class RecordType implements TypeInterface
|
||||||
{
|
{
|
||||||
public const SETTING_IS_RECORD = 'isRecord';
|
/**
|
||||||
|
* @var array $value
|
||||||
|
*/
|
||||||
|
private $value;
|
||||||
|
|
||||||
public function __construct(array $value)
|
public function __construct(array $value)
|
||||||
{
|
{
|
||||||
|
|
@ -17,17 +19,18 @@ class RecordType implements TypeInterface
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
array_walk($this->value, function (&$value, $key) {
|
array_walk($this->value, function (&$value, $key) {
|
||||||
if (is_string($key)) {
|
if (is_array($value)) {
|
||||||
$value = $key . ': ' . new Type($value, Settings::set([
|
$value = $this->getPrefix($key) . new RecordType($value);
|
||||||
self::SETTING_IS_RECORD => true,
|
|
||||||
]));
|
|
||||||
} else {
|
} else {
|
||||||
$value = new Type($value, Settings::set([
|
$value = $this->getPrefix($key) . new Type($value);
|
||||||
self::SETTING_IS_RECORD => true,
|
|
||||||
]));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return '{' . implode(', ', $this->value) . '}';
|
return '{' . implode(', ', $this->value) . '}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getPrefix($key): string
|
||||||
|
{
|
||||||
|
return is_string($key) ? $key . ': ' : '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue