Skip to content

Commit

Permalink
Extracting commong logic to a trait
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 15, 2014
1 parent 3bc6c2a commit 3823c90
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 67 deletions.
37 changes: 4 additions & 33 deletions src/ORM/Associations.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\ORM;

use Cake\ORM\Association;
use Cake\ORM\AssociationsNormalizerTrait;
use Cake\ORM\Entity;
use Cake\ORM\Table;

Expand All @@ -26,6 +27,8 @@
*/
class Associations {

use AssociationsNormalizerTrait;

/**
* Stored associations
*
Expand Down Expand Up @@ -262,39 +265,7 @@ public function normalizeKeys($keys) {
return [];
}

$result = [];
foreach ($keys as $table => $options) {
$pointer =& $result;

if (is_int($table)) {
$table = $options;
$options = [];
}

if (!strpos($table, '.')) {
$result[$table] = $options;
continue;
}

$path = explode('.', $table);
$table = array_pop($path);
$first = array_shift($path);
$pointer += [$first => []];
$pointer =& $pointer[$first];
$pointer += ['associated' => []];

foreach ($path as $t) {
$pointer += ['associated' => []];
$pointer['associated'] += [$t => []];
$pointer['associated'][$t] += ['associated' => []];
$pointer =& $pointer['associated'][$t];
}

$pointer['associated'] += [$table => []];
$pointer['associated'][$table] = $options + $pointer['associated'][$table];
}

return isset($result['associated']) ? $result['associated'] : $result;
return $this->_normalizeAssociations($keys);
}

}
37 changes: 3 additions & 34 deletions src/ORM/Marshaller.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Database\Type;
use Cake\Datasource\EntityInterface;
use Cake\ORM\Association;
use Cake\ORM\AssociationsNormalizerTrait;
use Cake\ORM\Table;

/**
Expand All @@ -33,6 +34,8 @@
*/
class Marshaller {

use AssociationsNormalizerTrait;

/**
* Whether or not this marhshaller is in safe mode.
*
Expand Down Expand Up @@ -84,40 +87,6 @@ protected function _buildPropertyMap($include) {
return $map;
}

/**
* Returns an array out of the original passed associations list where dot notation
* is transformed into nested arrays so that they can be parsed by other association
* marshallers.
*
* @param array $associations The array of included associations.
* @return array An array having dot notation trnasformed into nested arrays
*/
protected function _normalizeAssociations($associations) {
$result = [];
foreach ($associations as $table => $options) {
$pointer =& $result;

if (is_int($table)) {
$table = $options;
$options = [];
}

if (strpos($table, '.')) {
$path = explode('.', $table);
$table = array_pop($path);
foreach ($path as $t) {
$pointer += [$t => ['associated' => []]];
$pointer =& $pointer[$t]['associated'];
}
}

$pointer += [$table => []];
$pointer[$table] = $options + $pointer[$table];
}

return $result;
}

/**
* Hydrate one entity and its associated data.
*
Expand Down

0 comments on commit 3823c90

Please sign in to comment.