@@ -90,7 +90,7 @@ |
||
| 90 | 90 | public function new(string $sBag, string $sKey, $xValue): void |
| 91 | 91 | { |
| 92 | 92 | // Set the value only if it doesn't already exist. |
| 93 | - if(!isset($this->aData[$sBag]) || !key_exists($sKey, $this->aData[$sBag])) |
|
| 93 | + if (!isset($this->aData[$sBag]) || !key_exists($sKey, $this->aData[$sBag])) |
|
| 94 | 94 | { |
| 95 | 95 | $this->set($sBag, $sKey, $xValue); |
| 96 | 96 | } |
@@ -11,110 +11,110 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Databag implements JsonSerializable |
| 13 | 13 | { |
| 14 | - /** |
|
| 14 | +/** |
|
| 15 | 15 | * @var DatabagPlugin |
| 16 | 16 | */ |
| 17 | - protected $xPlugin; |
|
| 17 | +protected $xPlugin; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 19 | +/** |
|
| 20 | 20 | * @var array |
| 21 | 21 | */ |
| 22 | - protected $aData = []; |
|
| 22 | +protected $aData = []; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 24 | +/** |
|
| 25 | 25 | * @var bool |
| 26 | 26 | */ |
| 27 | - protected $bTouched = false; |
|
| 27 | +protected $bTouched = false; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 29 | +/** |
|
| 30 | 30 | * The constructor |
| 31 | 31 | * |
| 32 | 32 | * @param array $aData |
| 33 | 33 | */ |
| 34 | - public function __construct(DatabagPlugin $xPlugin, array $aData) |
|
| 35 | - { |
|
| 36 | - $this->xPlugin = $xPlugin; |
|
| 37 | - // Ensure all contents are arrays. |
|
| 38 | - $this->aData = array_map(function($aValue) { |
|
| 39 | - return is_array($aValue) ? $aValue : []; |
|
| 40 | - }, $aData); |
|
| 41 | - } |
|
| 34 | +public function __construct(DatabagPlugin $xPlugin, array $aData) |
|
| 35 | +{ |
|
| 36 | +$this->xPlugin = $xPlugin; |
|
| 37 | +// Ensure all contents are arrays. |
|
| 38 | +$this->aData = array_map(function($aValue) { |
|
| 39 | +return is_array($aValue) ? $aValue : []; |
|
| 40 | +}, $aData); |
|
| 41 | +} |
|
| 42 | 42 | |
| 43 | - /** |
|
| 43 | +/** |
|
| 44 | 44 | * @return bool |
| 45 | 45 | */ |
| 46 | - public function touched(): bool |
|
| 47 | - { |
|
| 48 | - return $this->bTouched; |
|
| 49 | - } |
|
| 46 | +public function touched(): bool |
|
| 47 | +{ |
|
| 48 | +return $this->bTouched; |
|
| 49 | +} |
|
| 50 | 50 | |
| 51 | - /** |
|
| 51 | +/** |
|
| 52 | 52 | * @return array |
| 53 | 53 | */ |
| 54 | - public function getAll(): array |
|
| 55 | - { |
|
| 56 | - return $this->aData; |
|
| 57 | - } |
|
| 54 | +public function getAll(): array |
|
| 55 | +{ |
|
| 56 | +return $this->aData; |
|
| 57 | +} |
|
| 58 | 58 | |
| 59 | - /** |
|
| 59 | +/** |
|
| 60 | 60 | * @param string $sBag |
| 61 | 61 | * |
| 62 | 62 | * @return void |
| 63 | 63 | */ |
| 64 | - public function clear(string $sBag): void |
|
| 65 | - { |
|
| 66 | - $this->aData[$sBag] = []; |
|
| 67 | - $this->xPlugin->addCommand('databag.clear', ['bag' => $sBag]); |
|
| 68 | - } |
|
| 64 | +public function clear(string $sBag): void |
|
| 65 | +{ |
|
| 66 | +$this->aData[$sBag] = []; |
|
| 67 | +$this->xPlugin->addCommand('databag.clear', ['bag' => $sBag]); |
|
| 68 | +} |
|
| 69 | 69 | |
| 70 | - /** |
|
| 70 | +/** |
|
| 71 | 71 | * @param string $sBag |
| 72 | 72 | * @param string $sKey |
| 73 | 73 | * @param mixed $xValue |
| 74 | 74 | * |
| 75 | 75 | * @return void |
| 76 | 76 | */ |
| 77 | - public function set(string $sBag, string $sKey, $xValue): void |
|
| 78 | - { |
|
| 79 | - $this->bTouched = true; |
|
| 80 | - $this->aData[$sBag][$sKey] = $xValue; |
|
| 81 | - } |
|
| 77 | +public function set(string $sBag, string $sKey, $xValue): void |
|
| 78 | +{ |
|
| 79 | +$this->bTouched = true; |
|
| 80 | +$this->aData[$sBag][$sKey] = $xValue; |
|
| 81 | +} |
|
| 82 | 82 | |
| 83 | - /** |
|
| 83 | +/** |
|
| 84 | 84 | * @param string $sBag |
| 85 | 85 | * @param string $sKey |
| 86 | 86 | * @param mixed $xValue |
| 87 | 87 | * |
| 88 | 88 | * @return void |
| 89 | 89 | */ |
| 90 | - public function new(string $sBag, string $sKey, $xValue): void |
|
| 91 | - { |
|
| 92 | - // Set the value only if it doesn't already exist. |
|
| 93 | - if(!isset($this->aData[$sBag]) || !key_exists($sKey, $this->aData[$sBag])) |
|
| 94 | - { |
|
| 95 | - $this->set($sBag, $sKey, $xValue); |
|
| 96 | - } |
|
| 97 | - } |
|
| 90 | +public function new(string $sBag, string $sKey, $xValue): void |
|
| 91 | +{ |
|
| 92 | +// Set the value only if it doesn't already exist. |
|
| 93 | +if(!isset($this->aData[$sBag]) || !key_exists($sKey, $this->aData[$sBag])) |
|
| 94 | +{ |
|
| 95 | +$this->set($sBag, $sKey, $xValue); |
|
| 96 | +} |
|
| 97 | +} |
|
| 98 | 98 | |
| 99 | - /** |
|
| 99 | +/** |
|
| 100 | 100 | * @param string $sBag |
| 101 | 101 | * @param string $sKey |
| 102 | 102 | * @param mixed $xValue |
| 103 | 103 | * |
| 104 | 104 | * @return mixed |
| 105 | 105 | */ |
| 106 | - public function get(string $sBag, string $sKey, $xValue = null): mixed |
|
| 107 | - { |
|
| 108 | - return $this->aData[$sBag][$sKey] ?? $xValue; |
|
| 109 | - } |
|
| 106 | +public function get(string $sBag, string $sKey, $xValue = null): mixed |
|
| 107 | +{ |
|
| 108 | +return $this->aData[$sBag][$sKey] ?? $xValue; |
|
| 109 | +} |
|
| 110 | 110 | |
| 111 | - /** |
|
| 111 | +/** |
|
| 112 | 112 | * Convert this call to array, when converting the response into json. |
| 113 | 113 | * |
| 114 | 114 | * @return array |
| 115 | 115 | */ |
| 116 | - public function jsonSerialize(): array |
|
| 117 | - { |
|
| 118 | - return $this->aData; |
|
| 119 | - } |
|
| 116 | +public function jsonSerialize(): array |
|
| 117 | +{ |
|
| 118 | +return $this->aData; |
|
| 119 | +} |
|
| 120 | 120 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | private function initDatabag(): void |
| 36 | 36 | { |
| 37 | - if($this->xDatabag !== null) |
|
| 37 | + if ($this->xDatabag !== null) |
|
| 38 | 38 | { |
| 39 | 39 | return; |
| 40 | 40 | } |
@@ -44,8 +44,7 @@ discard block |
||
| 44 | 44 | $aBody = $xRequest->getParsedBody(); |
| 45 | 45 | $aParams = $xRequest->getQueryParams(); |
| 46 | 46 | $aData = is_array($aBody) ? |
| 47 | - $this->readData($aBody['jxnbags'] ?? []) : |
|
| 48 | - $this->readData($aParams['jxnbags'] ?? []); |
|
| 47 | + $this->readData($aBody['jxnbags'] ?? []) : $this->readData($aParams['jxnbags'] ?? []); |
|
| 49 | 48 | $this->xDatabag = new Databag($this, $aData); |
| 50 | 49 | } |
| 51 | 50 | |
@@ -67,8 +66,7 @@ discard block |
||
| 67 | 66 | // Todo: clean input data. |
| 68 | 67 | // Todo: verify the checksums. |
| 69 | 68 | return is_string($xData) ? |
| 70 | - (json_decode($xData, true) ?: []) : |
|
| 71 | - (is_array($xData) ? $xData : []); |
|
| 69 | + (json_decode($xData, true) ?: []) : (is_array($xData) ? $xData : []); |
|
| 72 | 70 | } |
| 73 | 71 | |
| 74 | 72 | /** |
@@ -86,7 +84,7 @@ discard block |
||
| 86 | 84 | public function writeCommand(): void |
| 87 | 85 | { |
| 88 | 86 | $this->initDatabag(); |
| 89 | - if($this->xDatabag->touched()) |
|
| 87 | + if ($this->xDatabag->touched()) |
|
| 90 | 88 | { |
| 91 | 89 | // Todo: calculate the checksums. |
| 92 | 90 | $this->addCommand('databag.set', ['values' => $this->xDatabag]); |
@@ -18,7 +18,7 @@ |
||
| 18 | 18 | use Jaxon\App\Metadata\Metadata; |
| 19 | 19 | use Attribute; |
| 20 | 20 | |
| 21 | -#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] |
|
| 21 | +#[Attribute(Attribute::TARGET_CLASS|Attribute::TARGET_METHOD|Attribute::IS_REPEATABLE)] |
|
| 22 | 22 | class Databag extends AbstractAttribute |
| 23 | 23 | { |
| 24 | 24 | /** |
@@ -28,40 +28,40 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | class DatabagAnnotation extends AbstractAnnotation |
| 30 | 30 | { |
| 31 | - /** |
|
| 31 | +/** |
|
| 32 | 32 | * The data bag name |
| 33 | 33 | * |
| 34 | 34 | * @var string |
| 35 | 35 | */ |
| 36 | - protected $sName = ''; |
|
| 36 | +protected $sName = ''; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 38 | +/** |
|
| 39 | 39 | * @inheritDoc |
| 40 | 40 | */ |
| 41 | - public static function parseAnnotation($value) |
|
| 42 | - { |
|
| 43 | - $aParams = preg_split('/[\s]+/', $value, 2); |
|
| 44 | - return count($aParams) === 1 ? ['name' => $aParams[0]] : ['name' => $aParams[0], 'extra' => $aParams[1]]; |
|
| 45 | - } |
|
| 41 | +public static function parseAnnotation($value) |
|
| 42 | +{ |
|
| 43 | +$aParams = preg_split('/[\s]+/', $value, 2); |
|
| 44 | +return count($aParams) === 1 ? ['name' => $aParams[0]] : ['name' => $aParams[0], 'extra' => $aParams[1]]; |
|
| 45 | +} |
|
| 46 | 46 | |
| 47 | - /** |
|
| 47 | +/** |
|
| 48 | 48 | * @inheritDoc |
| 49 | 49 | * @throws AnnotationException |
| 50 | 50 | */ |
| 51 | - public function initAnnotation(array $properties) |
|
| 52 | - { |
|
| 53 | - if(count($properties) !== 1 || !isset($properties['name']) || !is_string($properties['name'])) |
|
| 54 | - { |
|
| 55 | - throw new AnnotationException('The @databag annotation requires a property "name" of type string'); |
|
| 56 | - } |
|
| 57 | - $this->sName = $properties['name']; |
|
| 58 | - } |
|
| 51 | +public function initAnnotation(array $properties) |
|
| 52 | +{ |
|
| 53 | +if(count($properties) !== 1 || !isset($properties['name']) || !is_string($properties['name'])) |
|
| 54 | +{ |
|
| 55 | +throw new AnnotationException('The @databag annotation requires a property "name" of type string'); |
|
| 56 | +} |
|
| 57 | +$this->sName = $properties['name']; |
|
| 58 | +} |
|
| 59 | 59 | |
| 60 | - /** |
|
| 60 | +/** |
|
| 61 | 61 | * @inheritDoc |
| 62 | 62 | */ |
| 63 | - public function saveValue(Metadata $xMetadata, string $sMethod = '*'): void |
|
| 64 | - { |
|
| 65 | - $xMetadata->databag($sMethod)->addValue($this->sName); |
|
| 66 | - } |
|
| 63 | +public function saveValue(Metadata $xMetadata, string $sMethod = '*'): void |
|
| 64 | +{ |
|
| 65 | +$xMetadata->databag($sMethod)->addValue($this->sName); |
|
| 66 | +} |
|
| 67 | 67 | } |
@@ -50,7 +50,7 @@ |
||
| 50 | 50 | */ |
| 51 | 51 | public function initAnnotation(array $properties) |
| 52 | 52 | { |
| 53 | - if(count($properties) !== 1 || !isset($properties['name']) || !is_string($properties['name'])) |
|
| 53 | + if (count($properties) !== 1 || !isset($properties['name']) || !is_string($properties['name'])) |
|
| 54 | 54 | { |
| 55 | 55 | throw new AnnotationException('The @databag annotation requires a property "name" of type string'); |
| 56 | 56 | } |
@@ -4,58 +4,58 @@ |
||
| 4 | 4 | |
| 5 | 5 | class DatabagContext |
| 6 | 6 | { |
| 7 | - /** |
|
| 7 | +/** |
|
| 8 | 8 | * @var Databag |
| 9 | 9 | */ |
| 10 | - protected $xDatabag; |
|
| 10 | +protected $xDatabag; |
|
| 11 | 11 | |
| 12 | - /** |
|
| 12 | +/** |
|
| 13 | 13 | * @var string |
| 14 | 14 | */ |
| 15 | - protected $sName; |
|
| 15 | +protected $sName; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 17 | +/** |
|
| 18 | 18 | * The constructor |
| 19 | 19 | * |
| 20 | 20 | * @param Databag $xDatabag |
| 21 | 21 | * @param string $sName |
| 22 | 22 | */ |
| 23 | - public function __construct(Databag $xDatabag, string $sName) |
|
| 24 | - { |
|
| 25 | - $this->xDatabag = $xDatabag; |
|
| 26 | - $this->sName = $sName; |
|
| 27 | - } |
|
| 23 | +public function __construct(Databag $xDatabag, string $sName) |
|
| 24 | +{ |
|
| 25 | +$this->xDatabag = $xDatabag; |
|
| 26 | +$this->sName = $sName; |
|
| 27 | +} |
|
| 28 | 28 | |
| 29 | - /** |
|
| 29 | +/** |
|
| 30 | 30 | * @param string $sKey |
| 31 | 31 | * @param mixed $xValue |
| 32 | 32 | * |
| 33 | 33 | * @return void |
| 34 | 34 | */ |
| 35 | - public function set(string $sKey, $xValue): void |
|
| 36 | - { |
|
| 37 | - $this->xDatabag->set($this->sName, $sKey, $xValue); |
|
| 38 | - } |
|
| 35 | +public function set(string $sKey, $xValue): void |
|
| 36 | +{ |
|
| 37 | +$this->xDatabag->set($this->sName, $sKey, $xValue); |
|
| 38 | +} |
|
| 39 | 39 | |
| 40 | - /** |
|
| 40 | +/** |
|
| 41 | 41 | * @param string $sKey |
| 42 | 42 | * @param mixed $xValue |
| 43 | 43 | * |
| 44 | 44 | * @return void |
| 45 | 45 | */ |
| 46 | - public function new(string $sKey, $xValue): void |
|
| 47 | - { |
|
| 48 | - $this->xDatabag->new($this->sName, $sKey, $xValue); |
|
| 49 | - } |
|
| 46 | +public function new(string $sKey, $xValue): void |
|
| 47 | +{ |
|
| 48 | +$this->xDatabag->new($this->sName, $sKey, $xValue); |
|
| 49 | +} |
|
| 50 | 50 | |
| 51 | - /** |
|
| 51 | +/** |
|
| 52 | 52 | * @param string $sKey |
| 53 | 53 | * @param mixed $xValue |
| 54 | 54 | * |
| 55 | 55 | * @return mixed |
| 56 | 56 | */ |
| 57 | - public function get(string $sKey, $xValue = null): mixed |
|
| 58 | - { |
|
| 59 | - return $this->xDatabag->get($this->sName, $sKey, $xValue); |
|
| 60 | - } |
|
| 57 | +public function get(string $sKey, $xValue = null): mixed |
|
| 58 | +{ |
|
| 59 | +return $this->xDatabag->get($this->sName, $sKey, $xValue); |
|
| 60 | +} |
|
| 61 | 61 | } |