1 | <?php |
||
21 | class Entity { |
||
22 | |||
23 | /** |
||
24 | * The {@see EntityDefinition} defining how this entity looks like. |
||
25 | */ |
||
26 | protected $definition; |
||
27 | |||
28 | /** |
||
29 | * Holds the key value data of the entity. |
||
30 | */ |
||
31 | protected $entity; |
||
32 | |||
33 | |||
34 | /** |
||
35 | * Converts a given value to the given type. |
||
36 | * |
||
37 | * @param mixed $value |
||
38 | * the value to convert |
||
39 | * @param string $type |
||
40 | * the type to convert to like 'integer' or 'float' |
||
41 | * |
||
42 | * @return mixed |
||
43 | * the converted value |
||
44 | */ |
||
45 | protected function toType($value, $type) { |
||
46 | if (in_array($type, ['integer', 'float']) && $value !== '' && $value !== null) { |
||
47 | settype($value, $type); |
||
48 | } else if ($type == 'boolean') { |
||
49 | $value = (bool)$value; |
||
50 | } else if ($type == 'many') { |
||
51 | $value = $value ?: []; |
||
52 | } else if (in_array($type, ['datetime', 'date', 'reference'])) { |
||
53 | $value = $value === '' ? null : $value; |
||
54 | } |
||
55 | return $value; |
||
56 | } |
||
57 | |||
58 | |||
59 | /** |
||
60 | * Constructor. |
||
61 | * |
||
62 | * @param EntityDefinition $definition |
||
63 | * the definition how this entity looks |
||
64 | */ |
||
65 | public function __construct(EntityDefinition $definition) { |
||
69 | |||
70 | /** |
||
71 | * Sets a field value pair of this entity. |
||
72 | * |
||
73 | * @param string $field |
||
74 | * the field |
||
75 | * @param mixed $value |
||
76 | * the value |
||
77 | */ |
||
78 | public function set($field, $value) { |
||
81 | |||
82 | /** |
||
83 | * Gets the raw value of a field no matter what type it is. |
||
84 | * This is usefull for input validation for example. |
||
85 | * |
||
86 | * @param string $field |
||
87 | * the field |
||
88 | * |
||
89 | * @return mixed |
||
90 | * null on invalid field or else the raw value |
||
91 | */ |
||
92 | public function getRaw($field) { |
||
98 | |||
99 | /** |
||
100 | * Gets the value of a field in its specific type. |
||
101 | * |
||
102 | * @param string $field |
||
103 | * the field |
||
104 | * |
||
105 | * @return mixed |
||
106 | * null on invalid field, an integer if the definition says that the |
||
107 | * type of the field is an integer, a boolean if the field is a boolean or |
||
108 | * else the raw value |
||
109 | */ |
||
110 | public function get($field) { |
||
124 | |||
125 | /** |
||
126 | * Gets the entity definition. |
||
127 | * |
||
128 | * @return EntityDefinition |
||
129 | * the definition |
||
130 | */ |
||
131 | public function getDefinition() { |
||
134 | |||
135 | /** |
||
136 | * Populates the entities fields from the requests parameters. |
||
137 | * |
||
138 | * @param Request $request |
||
139 | * the request to take the field data from |
||
140 | */ |
||
141 | public function populateViaRequest(Request $request) { |
||
169 | |||
170 | } |
||
171 |