1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\ORM;
16:
17: /**
18: * Contains methods for parsing the associated tables array that is typically
19: * passed to a save operation
20: */
21: trait AssociationsNormalizerTrait
22: {
23:
24: /**
25: * Returns an array out of the original passed associations list where dot notation
26: * is transformed into nested arrays so that they can be parsed by other routines
27: *
28: * @param array $associations The array of included associations.
29: * @return array An array having dot notation transformed into nested arrays
30: */
31: protected function _normalizeAssociations($associations)
32: {
33: $result = [];
34: foreach ((array)$associations as $table => $options) {
35: $pointer =& $result;
36:
37: if (is_int($table)) {
38: $table = $options;
39: $options = [];
40: }
41:
42: if (!strpos($table, '.')) {
43: $result[$table] = $options;
44: continue;
45: }
46:
47: $path = explode('.', $table);
48: $table = array_pop($path);
49: $first = array_shift($path);
50: $pointer += [$first => []];
51: $pointer =& $pointer[$first];
52: $pointer += ['associated' => []];
53:
54: foreach ($path as $t) {
55: $pointer += ['associated' => []];
56: $pointer['associated'] += [$t => []];
57: $pointer['associated'][$t] += ['associated' => []];
58: $pointer =& $pointer['associated'][$t];
59: }
60:
61: $pointer['associated'] += [$table => []];
62: $pointer['associated'][$table] = $options + $pointer['associated'][$table];
63: }
64:
65: return isset($result['associated']) ? $result['associated'] : $result;
66: }
67: }
68: