AssociationsNormalizerTrait.php 2.3 KB

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