1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CRUDlex package. |
5
|
|
|
* |
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CRUDlex; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Represents a single set of data in field value pairs like the row in a |
18
|
|
|
* database. Depends of course on the AbstractData implementation being used. |
19
|
|
|
* With this objects, the data is passed arround and validated. |
20
|
|
|
*/ |
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) |
68
|
|
|
{ |
69
|
39 |
|
$this->definition = $definition; |
70
|
39 |
|
$this->entity = []; |
71
|
39 |
|
} |
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) |
82
|
|
|
{ |
83
|
39 |
|
$this->entity[$field] = $value; |
84
|
39 |
|
} |
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) |
97
|
|
|
{ |
98
|
6 |
|
if (!array_key_exists($field, $this->entity)) { |
99
|
1 |
|
return null; |
100
|
|
|
} |
101
|
6 |
|
return $this->entity[$field]; |
102
|
|
|
} |
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) |
116
|
|
|
{ |
117
|
35 |
|
if ($this->definition->getField($field, 'value') !== null) { |
118
|
33 |
|
return $this->definition->getField($field, 'value'); |
119
|
|
|
} |
120
|
|
|
|
121
|
35 |
|
if (!array_key_exists($field, $this->entity)) { |
122
|
33 |
|
return null; |
123
|
|
|
} |
124
|
|
|
|
125
|
35 |
|
$type = $this->definition->getType($field); |
126
|
35 |
|
$value = $this->toType($this->entity[$field], $type); |
127
|
35 |
|
return $value; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Gets the entity definition. |
132
|
|
|
* |
133
|
|
|
* @return EntityDefinition |
134
|
|
|
* the definition |
135
|
|
|
*/ |
136
|
11 |
|
public function getDefinition() |
137
|
|
|
{ |
138
|
11 |
|
return $this->definition; |
139
|
|
|
} |
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) |
148
|
|
|
{ |
149
|
5 |
|
$fields = $this->definition->getEditableFieldNames(); |
150
|
5 |
|
foreach ($fields as $field) { |
151
|
5 |
|
$type = $this->definition->getType($field); |
152
|
5 |
|
if ($type === 'file') { |
153
|
5 |
|
$file = $request->files->get($field); |
154
|
5 |
|
if ($file) { |
155
|
5 |
|
$this->set($field, $file->getClientOriginalName()); |
156
|
|
|
} |
157
|
5 |
|
} else if ($type === 'reference') { |
158
|
5 |
|
$value = $request->get($field); |
159
|
5 |
|
if ($value === '') { |
160
|
1 |
|
$value = null; |
161
|
|
|
} |
162
|
5 |
|
$this->set($field, ['id' => $value]); |
163
|
5 |
|
} else if ($type === 'many') { |
164
|
1 |
|
$array = $request->get($field, []); |
165
|
1 |
|
if (is_array($array)) { |
166
|
|
|
$many = array_map(function($id) { |
167
|
1 |
|
return ['id' => $id]; |
168
|
1 |
|
}, $array); |
169
|
1 |
|
$this->set($field, $many); |
170
|
|
|
} |
171
|
|
|
} else { |
172
|
5 |
|
$this->set($field, $request->get($field)); |
173
|
|
|
} |
174
|
|
|
} |
175
|
5 |
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|