|
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
|
23 |
|
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 |
|
return $this->toType($this->entity[$field], $type); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Gets the entity definition. |
|
131
|
|
|
* |
|
132
|
|
|
* @return EntityDefinition |
|
133
|
|
|
* the definition |
|
134
|
|
|
*/ |
|
135
|
12 |
|
public function getDefinition() |
|
136
|
|
|
{ |
|
137
|
12 |
|
return $this->definition; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Populates the entities fields from the requests parameters. |
|
142
|
|
|
* |
|
143
|
|
|
* @param Request $request |
|
144
|
|
|
* the request to take the field data from |
|
145
|
|
|
*/ |
|
146
|
5 |
|
public function populateViaRequest(Request $request) |
|
147
|
|
|
{ |
|
148
|
5 |
|
$fields = $this->definition->getEditableFieldNames(); |
|
149
|
5 |
|
foreach ($fields as $field) { |
|
150
|
5 |
|
$type = $this->definition->getType($field); |
|
151
|
5 |
|
if ($type === 'file') { |
|
152
|
5 |
|
$file = $request->files->get($field); |
|
153
|
5 |
|
if ($file) { |
|
154
|
5 |
|
$this->set($field, $file->getClientOriginalName()); |
|
155
|
|
|
} |
|
156
|
5 |
|
} else if ($type === 'reference') { |
|
157
|
5 |
|
$value = $request->get($field); |
|
158
|
5 |
|
if ($value === '') { |
|
159
|
2 |
|
$value = null; |
|
160
|
|
|
} |
|
161
|
5 |
|
$this->set($field, ['id' => $value]); |
|
162
|
5 |
|
} else if ($type === 'many') { |
|
163
|
1 |
|
$array = $request->get($field, []); |
|
164
|
1 |
|
if (is_array($array)) { |
|
165
|
|
|
$many = array_map(function($id) { |
|
166
|
1 |
|
return ['id' => $id]; |
|
167
|
1 |
|
}, $array); |
|
168
|
1 |
|
$this->set($field, $many); |
|
169
|
|
|
} |
|
170
|
|
|
} else { |
|
171
|
5 |
|
$this->set($field, $request->get($field)); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
5 |
|
} |
|
175
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|