1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the PommProject/ModelManager package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 - 2015 Grégoire HUBERT <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace PommProject\ModelManager\Model; |
11
|
|
|
|
12
|
|
|
use PommProject\ModelManager\Exception\ModelException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* RowStructure |
16
|
|
|
* |
17
|
|
|
* Represent a composite structure like table or row. |
18
|
|
|
* |
19
|
|
|
* @package ModelManager |
20
|
|
|
* @copyright 2014 - 2015 Grégoire HUBERT |
21
|
|
|
* @author Grégoire HUBERT <[email protected]> |
22
|
|
|
* @license MIT/X11 {@link http://opensource.org/licenses/mit-license.php} |
23
|
|
|
*/ |
24
|
|
|
class RowStructure implements \ArrayAccess |
25
|
|
|
{ |
26
|
|
|
protected $primary_key = []; |
27
|
|
|
protected $field_definitions = []; |
28
|
|
|
protected $relation; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* setDefinition |
32
|
|
|
* |
33
|
|
|
* Add a complete definition. |
34
|
|
|
* |
35
|
|
|
* @access public |
36
|
|
|
* @param array $definition |
37
|
|
|
* @return RowStructure $this |
38
|
|
|
*/ |
39
|
|
|
public function setDefinition(array $definition) |
40
|
|
|
{ |
41
|
|
|
$this->field_definitions = $definition; |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* inherits |
48
|
|
|
* |
49
|
|
|
* Add inherited structure. |
50
|
|
|
* |
51
|
|
|
* @access public |
52
|
|
|
* @param RowStructure $structure |
53
|
|
|
* @return RowStructure $this |
54
|
|
|
*/ |
55
|
|
|
public function inherits(RowStructure $structure) |
56
|
|
|
{ |
57
|
|
|
foreach ($structure->getDefinition() as $field => $type) { |
58
|
|
|
$this->addField($field, $type); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* setRelation |
66
|
|
|
* |
67
|
|
|
* Set or change the relation. |
68
|
|
|
* |
69
|
|
|
* @access public |
70
|
|
|
* @param string $relation |
71
|
|
|
* @return RowStructure $this |
72
|
|
|
*/ |
73
|
|
|
public function setRelation($relation) |
74
|
|
|
{ |
75
|
|
|
$this->relation = $relation; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* setPrimaryKey |
82
|
|
|
* |
83
|
|
|
* Set or change the primary key definition. |
84
|
|
|
* |
85
|
|
|
* @access public |
86
|
|
|
* @param array $primary_key |
87
|
|
|
* @return RowStructure $this |
88
|
|
|
*/ |
89
|
|
|
public function setPrimaryKey(array $primary_key) |
90
|
|
|
{ |
91
|
|
|
$this->primary_key = $primary_key; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* addField |
98
|
|
|
* |
99
|
|
|
* Add a new field structure. |
100
|
|
|
* |
101
|
|
|
* @access public |
102
|
|
|
* @param string $name |
103
|
|
|
* @param string $type |
104
|
|
|
* @throws ModelException if type or name is null |
105
|
|
|
* @return RowStructure $this |
106
|
|
|
*/ |
107
|
|
|
public function addField($name, $type) |
108
|
|
|
{ |
109
|
|
|
$this->checkNotNull($type, 'type') |
110
|
|
|
->checkNotNull($name, 'name') |
111
|
|
|
->field_definitions[$name] = $type; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* getFieldNames |
118
|
|
|
* |
119
|
|
|
* Return an array of all field names |
120
|
|
|
* |
121
|
|
|
* @access public |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function getFieldNames() |
125
|
|
|
{ |
126
|
|
|
return array_keys($this->field_definitions); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* hasField |
131
|
|
|
* |
132
|
|
|
* Check if a field exist in the structure |
133
|
|
|
* |
134
|
|
|
* @access public |
135
|
|
|
* @param string $name |
136
|
|
|
* @throws ModelException if $name is null |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function hasField($name) |
140
|
|
|
{ |
141
|
|
|
return array_key_exists($name, $this->checkNotNull($name, 'name')->field_definitions); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* getTypeFor |
146
|
|
|
* |
147
|
|
|
* Return the type associated with the field |
148
|
|
|
* |
149
|
|
|
* @access public |
150
|
|
|
* @param string $name |
151
|
|
|
* @throws ModelException if $name is null or name does not exist. |
152
|
|
|
* @return string $type |
153
|
|
|
*/ |
154
|
|
|
public function getTypeFor($name) |
155
|
|
|
{ |
156
|
|
|
return $this->checkExist($name)->field_definitions[$name]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* getDefinition |
161
|
|
|
* |
162
|
|
|
* Return all fields and types |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public function getDefinition() |
167
|
|
|
{ |
168
|
|
|
return $this->field_definitions; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* getRelation |
173
|
|
|
* |
174
|
|
|
* Return the relation name. |
175
|
|
|
* |
176
|
|
|
* @access public |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function getRelation() |
180
|
|
|
{ |
181
|
|
|
return $this->relation; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* getPrimaryKey |
186
|
|
|
* |
187
|
|
|
* Return the primary key definition. |
188
|
|
|
* |
189
|
|
|
* @access public |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
public function getPrimaryKey() |
193
|
|
|
{ |
194
|
|
|
return $this->primary_key; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* checkNotNull |
199
|
|
|
* |
200
|
|
|
* Test if given value is null. |
201
|
|
|
* |
202
|
|
|
* @access private |
203
|
|
|
* @param string $val |
204
|
|
|
* @param string $name |
205
|
|
|
* @throws \InvalidArgumentException if $val is null |
206
|
|
|
* @return RowStructure $this |
207
|
|
|
*/ |
208
|
|
|
private function checkNotNull($val, $name) |
209
|
|
|
{ |
210
|
|
|
if ($val === null) { |
211
|
|
|
throw new \InvalidArgumentException(sprintf("'%s' cannot be null in '%s'.", $name, get_class($this))); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* checkExist |
219
|
|
|
* |
220
|
|
|
* Test if a field exist. |
221
|
|
|
* |
222
|
|
|
* @access private |
223
|
|
|
* @param string $name |
224
|
|
|
* @throws ModelException if $name does not exist. |
225
|
|
|
* @return RowStructure $this |
226
|
|
|
*/ |
227
|
|
|
private function checkExist($name) |
228
|
|
|
{ |
229
|
|
|
if (!$this->hasField($name)) { |
230
|
|
|
throw new ModelException( |
231
|
|
|
sprintf( |
232
|
|
|
"Field '%s' is not defined in structure '%s'. Defined fields are {%s}", |
233
|
|
|
$name, |
234
|
|
|
get_class($this), |
235
|
|
|
join(', ', array_keys($this->field_definitions)) |
236
|
|
|
) |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @see \ArrayAccess |
245
|
|
|
*/ |
246
|
|
|
public function offsetSet($name, $type) |
247
|
|
|
{ |
248
|
|
|
$this->addField($name, $type); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @see \ArrayAccess |
253
|
|
|
*/ |
254
|
|
|
public function offsetGet($name) |
255
|
|
|
{ |
256
|
|
|
return $this->getTypeFor($name); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @see \ArrayAccess |
261
|
|
|
*/ |
262
|
|
|
public function offsetExists($name) |
263
|
|
|
{ |
264
|
|
|
return $this->hasField($name); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @see \ArrayAccess |
269
|
|
|
*/ |
270
|
|
|
public function offsetUnset($name) |
271
|
|
|
{ |
272
|
|
|
throw new ModelException(sprintf("Cannot unset a structure field ('%s').", $name)); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|