Skip to content

Commit

Permalink
Actually adding the file
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 15, 2014
1 parent 3823c90 commit 292c5c5
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/ORM/AssociationsNormalizerTrait.php
@@ -0,0 +1,66 @@
<?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 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\ORM;

/**
* Contains methods for parsing the associated tables array that is typically
* passed to a save operation
*/
trait AssociationsNormalizerTrait {

/**
* 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 routines
*
* @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, '.')) {
$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;
}

}

0 comments on commit 292c5c5

Please sign in to comment.