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
|
15 |
|
public static function make(array $components): Makeable |
27
|
|
|
{ |
28
|
15 |
|
$entity = new static(); |
29
|
|
|
|
30
|
15 |
|
foreach ($components as $k => $v) { |
31
|
15 |
|
$entity->{'set' . ChangeCase::snakeToCamel($k)}($v); |
32
|
|
|
} |
33
|
15 |
|
return $entity; |
34
|
|
|
} |
35
|
|
|
|
36
|
12 |
|
public function toArray(): array |
37
|
|
|
{ |
38
|
12 |
|
return ArrayableHelper::processArray($this->data); |
39
|
|
|
} |
40
|
|
|
|
41
|
6 |
|
public function toJson(int $options = 0): string |
42
|
|
|
{ |
43
|
6 |
|
return json_encode($this->toArray(), $options); |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
public function __toString(): string |
47
|
|
|
{ |
48
|
3 |
|
return $this->toJson(); |
49
|
|
|
} |
50
|
|
|
|
51
|
33 |
|
public function __call(string $name, array $arguments) |
52
|
|
|
{ |
53
|
33 |
|
$action = substr($name, 0, 3); |
54
|
33 |
|
$property = ChangeCase::camelToSnake(substr($name, 3)); |
55
|
11 |
|
switch ($action) { |
56
|
33 |
|
case 'set': |
57
|
24 |
|
return $this->set($property, $arguments); |
58
|
21 |
|
case 'get': |
59
|
15 |
|
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
|
24 |
|
private function set(string $property, array $arguments) |
72
|
|
|
{ |
73
|
24 |
|
$this->checkArguments($arguments, $property); |
74
|
21 |
|
$this->data[$property] = $arguments[0]; |
75
|
|
|
|
76
|
21 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
15 |
|
private function get(string $property) |
80
|
|
|
{ |
81
|
15 |
|
if (array_key_exists($property, $this->data)) { |
82
|
12 |
|
return $this->data[$property]; |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
6 |
|
private function add(string $name, array $arguments) |
89
|
|
|
{ |
90
|
6 |
|
if (!isset($this->data[$name]) || is_array($this->data[$name])) { |
91
|
3 |
|
$this->data[$name][] = $arguments[0]; |
92
|
|
|
|
93
|
3 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
3 |
|
throw new InvalidArgumentException('Expected property [' . $name . '] to be array type'); |
97
|
|
|
} |
98
|
|
|
|
99
|
24 |
|
private function checkArguments(array $arguments, $property): void |
100
|
|
|
{ |
101
|
24 |
|
if (count($arguments) == 0) { |
102
|
3 |
|
throw new \RuntimeException('Missing argument on method ' . __CLASS__ . '::set_' . $property . '() call'); |
103
|
|
|
} |
104
|
21 |
|
} |
105
|
|
|
} |
106
|
|
|
|