Skip to content

Commit

Permalink
Renaming reformatting function to better express its purpose, also
Browse files Browse the repository at this point in the history
making it a real method instead of a closure as there was no need for it
to be one
  • Loading branch information
lorenzo committed Jun 11, 2013
1 parent f909cae commit fea72f8
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions lib/Cake/ORM/Query.php
Expand Up @@ -245,28 +245,35 @@ public function contain($associations = null, $override = false) {
return $this;
}

$normalizer = function($associations) use (&$normalizer) {
$result = [];
foreach ((array)$associations as $table => $options) {
if (is_int($table)) {
$table = $options;
$options = [];
} elseif (is_array($options) && !isset($this->_containOptions[$table])) {
$options = $normalizer($options);
}
$result[$table] = $options;
}
return $result;
};

$old = $this->_containments->getArrayCopy();
$associations = array_merge($old, $normalizer($associations));
$associations = array_merge($old, $this->_reformatContain($associations));
$this->_containments->exchangeArray($associations);
$this->_normalizedContainments = null;
$this->_dirty = true;
return $this;
}

/**
* Formats the containments array so that associations are always set as keys
* in the array.
*
* @param array $associations user provided containments array
* @return array
*/
protected function _reformatContain($associations) {
$result = [];
foreach ((array)$associations as $table => $options) {
if (is_int($table)) {
$table = $options;
$options = [];
} elseif (is_array($options) && !isset($this->_containOptions[$table])) {
$options = $this->_reformatContain($options);
}
$result[$table] = $options;
}
return $result;
}

/**
* Returns the fully normalized array of associations that should be eagerly
* loaded. The normalized array will restructure the original one by sorting
Expand All @@ -275,7 +282,6 @@ public function contain($associations = null, $override = false) {
* Additionally it will set an 'instance' key per association containing the
* association instance from the corresponding source table
*
*
* @return array
*/
public function normalizedContainments() {
Expand Down

0 comments on commit fea72f8

Please sign in to comment.