/
EntityContext.php
294 lines (268 loc) · 7.16 KB
/
EntityContext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\View\Form;
use Cake\Network\Request;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\Validation\Validator;
use Traversable;
/**
* Provides a form context around a single entity and its relations.
*
* This class lets FormHelper interface with entities or collections
* of entities.
*
* Important Keys:
*
* - `entity` The entity this context is operating on.
* - `table` Either the ORM\Table instance to fetch schema/validators
* from, an array of table instances in the case of an form spanning
* multiple entities, or the name(s) of the table.
* If this is null the table name(s) will be determined using conventions.
* - `validator` Either the Validation\Validator to use, or the name of the
* validation method to call on the table object. For example 'default'.
* Defaults to 'default'. Can be an array of table alias=>validators when
* dealing with associated forms.
*/
class EntityContext {
/**
* The request object.
*
* @var Cake\Network\Request
*/
protected $_request;
/**
* Context data for this object.
*
* @var array
*/
protected $_context;
/**
* The name of the top level entity/table object.
*
* @var string
*/
protected $_rootName;
/**
* A dictionary of tables
*
* @var array
*/
protected $_tables = [];
/**
* Constructor.
*
* @param Cake\Network\Request
* @param array
*/
public function __construct(Request $request, array $context) {
$this->_request = $request;
$context += [
'entity' => null,
'table' => null,
'validator' => [],
];
$this->_context = $context;
$this->_prepare();
}
/**
* Prepare some additional data from the context.
*
* @return void
*/
protected function _prepare() {
$table = $this->_context['table'];
if (is_string($table)) {
$table = TableRegistry::get($table);
}
$alias = $this->_rootName = $table->alias();
$this->_tables[$alias] = $table;
}
/**
* Get the value for a given path.
*
* Traverses the entity data and finds the value for $path.
*
* @param string $field The dot separated path to the value.
* @return mixed The value of the field or null on a miss.
*/
public function val($field) {
if (empty($this->_context['entity'])) {
return null;
}
$parts = explode('.', $field);
list($entity, $prop) = $this->_getEntity($parts);
if (!$entity) {
return null;
}
return $entity->get(array_pop($parts));
}
/**
* Fetch the leaf entity for the given path.
*
* This method will traverse the given path and find the leaf
* entity. If the path does not contain a leaf entity false
* will be returned.
*
* @param array $path The path to traverse to find the leaf entity.
* @return array
*/
protected function _getEntity($path) {
$entity = $this->_context['entity'];
if (count($path) === 1) {
return [$entity, $this->_rootName];
}
// Remove the Table name if present.
if (count($path) > 1 && $path[0] === $this->_rootName) {
array_shift($path);
}
$lastProp = $this->_rootName;
foreach ($path as $prop) {
$next = $this->_getProp($entity, $prop);
if (
!is_array($next) &&
!($next instanceof Traversable) &&
!($next instanceof Entity)
) {
return [$entity, $lastProp];
}
if (!is_numeric($prop)) {
$lastProp = $prop;
}
$entity = $next;
}
return [false, false];
}
/**
* Read property values or traverse arrays/iterators.
*
* @param mixed $target The entity/array/collection to fetch $field from.
* @param string $field The next field to fetch.
* @return mixed.
*/
protected function _getProp($target, $field) {
if (is_array($target) || $target instanceof Traversable) {
foreach ($target as $i => $val) {
if ($i == $field) {
return $val;
}
}
}
return $target->get($field);
}
/**
* Check if a field should be marked as required.
*
* @param string $field The dot separated path to the field you want to check.
* @return boolean
*/
public function isRequired($field) {
if (empty($this->_context['validator'])) {
return false;
}
$parts = explode('.', $field);
list($entity, $prop) = $this->_getEntity($parts);
if (!$entity) {
return false;
}
$validator = $this->_getValidator($prop);
if (!$validator) {
return false;
}
$field = array_pop($parts);
if (!$validator->hasField($field)) {
return false;
}
$allowed = $validator->isEmptyAllowed($field, $entity->isNew());
return $allowed === false;
}
/**
* Get the validator associated to an entity based on naming
* conventions.
*
* @param string $entity The entity name to get a validator for.
* @return Validator|false
*/
protected function _getValidator($entity) {
$table = $this->_getTable($entity);
$alias = $table->alias();
$method = 'default';
if (is_string($this->_context['validator'])) {
$method = $this->_context['validator'];
} elseif (isset($this->_context['validator'][$alias])){
$method = $this->_context['validator'][$alias];
}
return $table->validator($method);
}
/**
* Get the table instance
*
* @param string $prop The property name to get a table for.
* @return Cake\ORM\Table The table instance.
*/
protected function _getTable($prop) {
if (isset($this->_tables[$prop])) {
return $this->_tables[$prop];
}
$root = $this->_tables[$this->_rootName];
$assoc = $root->associations()->getByProperty($prop);
// No assoc, use the default table to prevent
// downstream failures.
if (!$assoc) {
return $root;
}
$target = $assoc->target();
$this->_tables[$prop] = $target;
return $target;
}
/**
* Get the abstract field type for a given field name.
*
* @param string $field A dot separated path to get a schema type for.
* @return null|string An abstract data type or null.
* @see Cake\Database\Type
*/
public function type($field) {
$parts = explode('.', $field);
list($entity, $prop) = $this->_getEntity($parts);
if (!$entity) {
return null;
}
$table = $this->_getTable($prop);
$column = array_pop($parts);
return $table->schema()->columnType($column);
}
/**
* Get an associative array of other attributes for a field name.
*
* @param string $field A dot separated path to get additional data on.
* @return array An array of data describing the additional attributes on a field.
*/
public function attributes($field) {
$parts = explode('.', $field);
list($entity, $prop) = $this->_getEntity($parts);
if (!$entity) {
return null;
}
$table = $this->_getTable($prop);
$column = $table->schema()->column(array_pop($parts));
$whitelist = ['length' => null, 'precision' => null];
return array_intersect_key($column, $whitelist);
}
public function hasError($field) {
}
public function error($field) {
}
}