1 | <?php |
||
21 | class Entity |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * The definition how this entity looks like. |
||
26 | * @var EntityDefinition |
||
27 | */ |
||
28 | protected $definition; |
||
29 | |||
30 | /** |
||
31 | * Holds the key value data of the entity. |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $entity; |
||
35 | |||
36 | |||
37 | /** |
||
38 | * Converts a given value to the given type. |
||
39 | * |
||
40 | * @param mixed $value |
||
41 | * the value to convert |
||
42 | * @param string $type |
||
43 | * the type to convert to like 'integer' or 'float' |
||
44 | * |
||
45 | * @return mixed |
||
46 | * the converted value |
||
47 | */ |
||
48 | 35 | protected function toType($value, $type) |
|
49 | { |
||
50 | 35 | if (in_array($type, ['integer', 'float']) && !in_array($value, ['', null], true)) { |
|
51 | 21 | settype($value, $type); |
|
52 | 35 | } else if ($type == 'boolean') { |
|
53 | 33 | $value = (bool)$value; |
|
54 | 35 | } else if ($type == 'many') { |
|
55 | 34 | $value = $value ?: []; |
|
56 | } |
||
57 | 35 | return $value === '' ? null : $value; |
|
58 | } |
||
59 | |||
60 | |||
61 | /** |
||
62 | * Constructor. |
||
63 | * |
||
64 | * @param EntityDefinition $definition |
||
65 | * the definition how this entity looks |
||
66 | */ |
||
67 | 39 | public function __construct(EntityDefinition $definition) |
|
72 | |||
73 | /** |
||
74 | * Sets a field value pair of this entity. |
||
75 | * |
||
76 | * @param string $field |
||
77 | * the field |
||
78 | * @param mixed $value |
||
79 | * the value |
||
80 | */ |
||
81 | 39 | public function set($field, $value) |
|
85 | |||
86 | /** |
||
87 | * Gets the raw value of a field no matter what type it is. |
||
88 | * This is usefull for input validation for example. |
||
89 | * |
||
90 | * @param string $field |
||
91 | * the field |
||
92 | * |
||
93 | * @return mixed |
||
94 | * null on invalid field or else the raw value |
||
95 | */ |
||
96 | 6 | public function getRaw($field) |
|
103 | |||
104 | /** |
||
105 | * Gets the value of a field in its specific type. |
||
106 | * |
||
107 | * @param string $field |
||
108 | * the field |
||
109 | * |
||
110 | * @return mixed |
||
111 | * null on invalid field, an integer if the definition says that the |
||
112 | * type of the field is an integer, a boolean if the field is a boolean or |
||
113 | * else the raw value |
||
114 | */ |
||
115 | 35 | public function get($field) |
|
129 | |||
130 | /** |
||
131 | * Gets the entity definition. |
||
132 | * |
||
133 | * @return EntityDefinition |
||
134 | * the definition |
||
135 | */ |
||
136 | 11 | public function getDefinition() |
|
140 | |||
141 | /** |
||
142 | * Populates the entities fields from the requests parameters. |
||
143 | * |
||
144 | * @param Request $request |
||
145 | * the request to take the field data from |
||
146 | */ |
||
147 | 5 | public function populateViaRequest(Request $request) |
|
176 | |||
177 | } |
||
178 |