Permalink
Browse files

Refactored EntityContext so that any inspection can be done to any deep

property and not only those having a value present in the context
  • Loading branch information...
lorenzo committed Feb 28, 2014
1 parent dc6a1b9 commit d20cefc733d67c252f82c784723bb7e6026bd2f2
Showing with 38 additions and 46 deletions.
  1. +38 −46 src/View/Form/EntityContext.php
@@ -158,14 +158,7 @@ public function primaryKey() {
*/
public function isPrimaryKey($field) {
$parts = explode('.', $field);
-
- if ($parts['0'] === $this->_rootName) {
- $parts = array_slice($parts, 1);
- }
-
- list(, $prop) = $this->_getEntity($parts);
- $table = $this->_getTable($prop);
-
+ $table = $this->_getTable($parts);
$primaryKey = (array)$table->primaryKey();
return in_array(array_pop($parts), $primaryKey);
}
@@ -205,7 +198,7 @@ public function val($field) {
return null;
}
$parts = explode('.', $field);
- list($entity) = $this->_getEntity($parts);
+ $entity = $this->_getEntity($parts);
if ($entity instanceof Entity) {
return $entity->get(array_pop($parts));
}
@@ -219,22 +212,20 @@ public function val($field) {
* 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
+ * @param array $path Each one of the parts in a path for a field name
+ * @return \Cake\DataSource\EntityInterface|boolean
* @throws \RuntimeException When properties cannot be read.
*/
protected function _getEntity($path) {
$oneElement = count($path) === 1;
if ($oneElement && $this->_isCollection) {
- return [false, false];
+ return false;
}
$entity = $this->_context['entity'];
if ($oneElement) {
- return [$entity, $this->_rootName];
+ return $entity;
}
- $lastProp = $this->_rootName;
-
if ($path[0] === $this->_rootName) {
$path = array_slice($path, 1);
}
@@ -246,10 +237,7 @@ protected function _getEntity($path) {
!($next instanceof Traversable) &&
!($next instanceof Entity)
) {
- return [$entity, $lastProp];
- }
- if (!is_numeric($prop)) {
- $lastProp = $prop;
+ return $entity;
}
$entity = $next;
}
@@ -294,14 +282,14 @@ public function isRequired($field) {
return false;
}
$parts = explode('.', $field);
- list($entity, $prop) = $this->_getEntity($parts);
+ $entity = $this->_getEntity($parts);
$isNew = true;
if ($entity instanceof Entity) {
$isNew = $entity->isNew();
}
- $validator = $this->_getValidator($prop);
+ $validator = $this->_getValidator($parts);
$field = array_pop($parts);
if (!$validator->hasField($field)) {
return false;
@@ -314,11 +302,11 @@ public function isRequired($field) {
* Get the validator associated to an entity based on naming
* conventions.
*
- * @param string $entity The entity name to get a validator for.
+ * @param array $parts Each one of the parts in a path for a field name
* @return Validator
*/
- protected function _getValidator($entity) {
- $table = $this->_getTable($entity);
+ protected function _getValidator($parts) {
+ $table = $this->_getTable($parts);
$alias = $table->alias();
$method = 'default';
@@ -331,27 +319,34 @@ protected function _getValidator($entity) {
}
/**
- * Get the table instance
+ * Get the table instance from a property path
*
- * @param string $prop The property name to get a table for.
- * @return \Cake\ORM\Table The table instance.
+ * @param array $parts Each one of the parts in a path for a field name
+ * @return array containing the table instance in the first position and the
+ * property name in the second position
*/
- protected function _getTable($prop) {
- if (isset($this->_tables[$prop])) {
- return $this->_tables[$prop];
+ protected function _getTable($parts) {
+ if ($parts[0] === $this->_rootName) {
+ $parts = array_slice($parts, 1);
}
- $root = $this->_tables[$this->_rootName];
- $assoc = $root->associations()->getByProperty($prop);
- // No assoc, use the default table to prevent
- // downstream failures.
- if (!$assoc) {
- return $root;
+ $table = $this->_tables[$this->_rootName];
+
+ $normalized = array_filter()
+ foreach ($parts as $part) {
+ if (is_numeric($part)) {
+ continue;
+ }
+
+ $assoc = $table->associations()->getByProperty($part);
+ if (!$assoc) {
+ break;
+ }
+
+ $table = $assoc->target();
}
- $target = $assoc->target();
- $this->_tables[$prop] = $target;
- return $target;
+ return $table;
}
/**
@@ -363,10 +358,8 @@ protected function _getTable($prop) {
*/
public function type($field) {
$parts = explode('.', $field);
- list($entity, $prop) = $this->_getEntity($parts);
- $table = $this->_getTable($prop);
- $column = array_pop($parts);
- return $table->schema()->columnType($column);
+ $table = $this->_getTable($parts);
+ return $table->schema()->columnType(array_pop($parts));
}
/**
@@ -377,8 +370,7 @@ public function type($field) {
*/
public function attributes($field) {
$parts = explode('.', $field);
- list($entity, $prop) = $this->_getEntity($parts);
- $table = $this->_getTable($prop);
+ $table = $this->_getTable($parts);
$column = (array)$table->schema()->column(array_pop($parts));
$whitelist = ['length' => null, 'precision' => null];
return array_intersect_key($column, $whitelist);
@@ -402,7 +394,7 @@ public function hasError($field) {
*/
public function error($field) {
$parts = explode('.', $field);
- list($entity, $prop) = $this->_getEntity($parts);
+ $entity= $this->_getEntity($parts);
if ($entity instanceof Entity) {
return $entity->errors(array_pop($parts));

0 comments on commit d20cefc

Please sign in to comment.