|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Bushido\Foundation package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Wojciech Nowicki <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Bushido\Foundation\SmartEntity; |
|
12
|
|
|
|
|
13
|
|
|
use Bushido\Foundation\Contracts\Makeable; |
|
14
|
|
|
use Bushido\Foundation\Exceptions\InvalidArgumentException; |
|
15
|
|
|
use Bushido\Foundation\Helpers\ArrayableHelper; |
|
16
|
|
|
use Bushido\Foundation\Helpers\ChangeCase; |
|
17
|
|
|
|
|
18
|
|
|
class FlexEntity implements Entity |
|
19
|
|
|
{ |
|
20
|
|
|
private $data = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param array $components |
|
24
|
|
|
* @return static |
|
25
|
|
|
*/ |
|
26
|
32 |
|
public static function make(array $components): Makeable |
|
27
|
|
|
{ |
|
28
|
32 |
|
$entity = new static(); |
|
29
|
|
|
|
|
30
|
32 |
|
foreach ($components as $k => $v) { |
|
31
|
17 |
|
$entity->{'set' . ChangeCase::snakeToCamel($k)}($v); |
|
32
|
|
|
} |
|
33
|
32 |
|
return $entity; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
14 |
|
public function toArray(): array |
|
37
|
|
|
{ |
|
38
|
14 |
|
return ArrayableHelper::processArray($this->data); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
6 |
|
public function toJson(int $options = 0): string |
|
42
|
|
|
{ |
|
43
|
6 |
|
return (string) json_encode($this->toArray(), $options); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
3 |
|
public function __toString(): string |
|
47
|
|
|
{ |
|
48
|
3 |
|
return $this->toJson(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
47 |
|
public function __call(string $name, array $arguments) |
|
52
|
|
|
{ |
|
53
|
47 |
|
$action = substr($name, 0, 3); |
|
54
|
47 |
|
$property = ChangeCase::camelToSnake(substr($name, 3)); |
|
55
|
32 |
|
switch ($action) { |
|
56
|
47 |
|
case 'set': |
|
57
|
38 |
|
return $this->set($property, $arguments); |
|
58
|
24 |
|
case 'get': |
|
59
|
17 |
|
return $this->get($property); |
|
60
|
9 |
|
case 'add': |
|
61
|
6 |
|
return $this->add($property, $arguments); |
|
62
|
|
|
} |
|
63
|
3 |
|
throw new \RuntimeException('Call to undefined method '.__CLASS__.'::'.$name.'()'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $property |
|
68
|
|
|
* @param array $arguments |
|
69
|
|
|
* @return $this |
|
70
|
|
|
*/ |
|
71
|
29 |
|
protected function set(string $property, array $arguments) |
|
72
|
|
|
{ |
|
73
|
29 |
|
$this->data[$property] = $this->fetchValue($arguments, $property); |
|
74
|
|
|
|
|
75
|
26 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
17 |
|
private function get(string $property) |
|
79
|
|
|
{ |
|
80
|
17 |
|
if (array_key_exists($property, $this->data)) { |
|
81
|
14 |
|
return $this->data[$property]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
3 |
|
return null; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
6 |
|
private function add(string $name, array $arguments) |
|
88
|
|
|
{ |
|
89
|
6 |
|
if (!isset($this->data[$name]) || is_array($this->data[$name])) { |
|
90
|
3 |
|
$this->data[$name][] = $arguments[0]; |
|
91
|
|
|
|
|
92
|
3 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
3 |
|
throw new InvalidArgumentException('Cannot add to [' . $name . '] property as it is not an array.'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
38 |
|
protected function fetchValue(array $arguments, $propertyName) |
|
99
|
|
|
{ |
|
100
|
38 |
|
if (count($arguments) == 0) { |
|
101
|
3 |
|
throw new \RuntimeException('Missing argument on ' . __CLASS__ . '::set_' . $propertyName . '() call'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
35 |
|
return $arguments[0]; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|