Add new Settings object to use as a DTO
This commit is contained in:
parent
923cb7dcb7
commit
4de01cfcb8
4 changed files with 82 additions and 22 deletions
26
src/Settings.php
Normal file
26
src/Settings.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace Arendsen\FluxQueryBuilder;
|
||||
|
||||
class Settings
|
||||
{
|
||||
/**
|
||||
* @var array $settings
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
public function __construct(array $settings = [])
|
||||
{
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public static function set(array $settings = [])
|
||||
{
|
||||
return new self($settings);
|
||||
}
|
||||
|
||||
public function get(string $key)
|
||||
{
|
||||
return isset($this->settings[$key]) ? $this->settings[$key] : null;
|
||||
}
|
||||
}
|
||||
14
src/Type.php
14
src/Type.php
|
|
@ -9,10 +9,20 @@ use DateTime;
|
|||
|
||||
class Type
|
||||
{
|
||||
public function __construct($value, $settings = [])
|
||||
/**
|
||||
* @var mixed $value
|
||||
*/
|
||||
protected $value;
|
||||
|
||||
/**
|
||||
* @var Settings|null $settings
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
public function __construct($value, Settings $settings = null)
|
||||
{
|
||||
$this->value = $value;
|
||||
$this->settings = $settings;
|
||||
$this->settings = $settings ? $settings : Settings::set([]);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
|
|
|
|||
|
|
@ -2,46 +2,67 @@
|
|||
|
||||
namespace Arendsen\FluxQueryBuilder\Type;
|
||||
|
||||
use Arendsen\FluxQueryBuilder\Settings;
|
||||
use Arendsen\FluxQueryBuilder\Type;
|
||||
|
||||
class ArrayType implements TypeInterface
|
||||
{
|
||||
public const SETTING_IS_NESTED_ARRAY = 'isNestedArray';
|
||||
|
||||
/**
|
||||
* @var array $value
|
||||
*/
|
||||
protected $value;
|
||||
|
||||
/**
|
||||
* @var array $settings
|
||||
* @var Settings|null $settings
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
public function __construct(array $value, $settings = [])
|
||||
public function __construct(array $value, Settings $settings = null)
|
||||
{
|
||||
$this->value = $value;
|
||||
$this->settings = $settings;
|
||||
$this->settings = $settings ? $settings : Settings::set([]);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
if (isset($this->settings['isRecord']) && $this->settings['isRecord']) {
|
||||
if ($this->settings->get(RecordType::SETTING_IS_RECORD)) {
|
||||
return new RecordType($this->value);
|
||||
}
|
||||
|
||||
$subArray = isset($this->settings['isNestedArray']) && $this->settings['isNestedArray'];
|
||||
|
||||
array_walk($this->value, function (&$value, $key) {
|
||||
if (is_string($key)) {
|
||||
$value = $key . ': ' . new Type($value, [
|
||||
'isNestedArray' => is_array($value)
|
||||
]);
|
||||
if ($this->isAssociativeArray($key)) {
|
||||
$value = $key . ': ' . new Type($value, Settings::set([
|
||||
self::SETTING_IS_NESTED_ARRAY => is_array($value)
|
||||
]));
|
||||
} else {
|
||||
$value = new Type($value, [
|
||||
'isNestedArray' => is_array($value)
|
||||
]);
|
||||
$value = new Type($value, Settings::set([
|
||||
self::SETTING_IS_NESTED_ARRAY => is_array($value)
|
||||
]));
|
||||
}
|
||||
});
|
||||
|
||||
return ($subArray ? '[' : '') . implode(', ', $this->value) . ($subArray ? ']' : '');
|
||||
return $this->getPrefix() . implode(', ', $this->value) . $this->getSuffix();
|
||||
}
|
||||
|
||||
protected function isAssociativeArray($key): bool
|
||||
{
|
||||
return is_string($key);
|
||||
}
|
||||
|
||||
protected function isNestedArray(): bool
|
||||
{
|
||||
return $this->settings->get(self::SETTING_IS_NESTED_ARRAY) ? true : false;
|
||||
}
|
||||
|
||||
protected function getPrefix(): string
|
||||
{
|
||||
return $this->isNestedArray() ? '[' : '';
|
||||
}
|
||||
|
||||
protected function getSuffix(): string
|
||||
{
|
||||
return $this->isNestedArray() ? ']' : '';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,13 @@
|
|||
|
||||
namespace Arendsen\FluxQueryBuilder\Type;
|
||||
|
||||
use Arendsen\FluxQueryBuilder\Settings;
|
||||
use Arendsen\FluxQueryBuilder\Type;
|
||||
|
||||
class RecordType implements TypeInterface
|
||||
{
|
||||
public const SETTING_IS_RECORD = 'isRecord';
|
||||
|
||||
public function __construct(array $value)
|
||||
{
|
||||
$this->value = $value;
|
||||
|
|
@ -15,13 +18,13 @@ class RecordType implements TypeInterface
|
|||
{
|
||||
array_walk($this->value, function (&$value, $key) {
|
||||
if (is_string($key)) {
|
||||
$value = $key . ': ' . new Type($value, [
|
||||
'isRecord' => true,
|
||||
]);
|
||||
$value = $key . ': ' . new Type($value, Settings::set([
|
||||
self::SETTING_IS_RECORD => true,
|
||||
]));
|
||||
} else {
|
||||
$value = new Type($value, [
|
||||
'isRecord' => true,
|
||||
]);
|
||||
$value = new Type($value, Settings::set([
|
||||
self::SETTING_IS_RECORD => true,
|
||||
]));
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
editor.link_modal.header
Reference in a new issue