MergeVariablesTrait.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Utility;
  15. /**
  16. * Provides features for merging object properties recursively with
  17. * parent classes.
  18. */
  19. trait MergeVariablesTrait
  20. {
  21. /**
  22. * Merge the list of $properties with all parent classes of the current class.
  23. *
  24. * ### Options:
  25. *
  26. * - `associative` - A list of properties that should be treated as associative arrays.
  27. * Properties in this list will be passed through Hash::normalize() before merging.
  28. *
  29. * @param array $properties An array of properties and the merge strategy for them.
  30. * @param array $options The options to use when merging properties.
  31. * @return void
  32. */
  33. protected function _mergeVars($properties, $options = [])
  34. {
  35. $class = get_class($this);
  36. $parents = [];
  37. while (true) {
  38. $parent = get_parent_class($class);
  39. if (!$parent) {
  40. break;
  41. }
  42. $parents[] = $parent;
  43. $class = $parent;
  44. }
  45. foreach ($properties as $property) {
  46. if (!property_exists($this, $property)) {
  47. continue;
  48. }
  49. $thisValue = $this->{$property};
  50. if ($thisValue === null || $thisValue === false) {
  51. continue;
  52. }
  53. $this->_mergeProperty($property, $parents, $options);
  54. }
  55. }
  56. /**
  57. * Merge a single property with the values declared in all parent classes.
  58. *
  59. * @param string $property The name of the property being merged.
  60. * @param array $parentClasses An array of classes you want to merge with.
  61. * @param array $options Options for merging the property, see _mergeVars()
  62. * @return void
  63. */
  64. protected function _mergeProperty($property, $parentClasses, $options)
  65. {
  66. $thisValue = $this->{$property};
  67. $isAssoc = false;
  68. if (isset($options['associative']) &&
  69. in_array($property, (array)$options['associative'])
  70. ) {
  71. $isAssoc = true;
  72. }
  73. if ($isAssoc) {
  74. $thisValue = Hash::normalize($thisValue);
  75. }
  76. foreach ($parentClasses as $class) {
  77. $parentProperties = get_class_vars($class);
  78. if (empty($parentProperties[$property])) {
  79. continue;
  80. }
  81. $parentProperty = $parentProperties[$property];
  82. if (!is_array($parentProperty)) {
  83. continue;
  84. }
  85. $thisValue = $this->_mergePropertyData($thisValue, $parentProperty, $isAssoc);
  86. }
  87. $this->{$property} = $thisValue;
  88. }
  89. /**
  90. * Merge each of the keys in a property together.
  91. *
  92. * @param array $current The current merged value.
  93. * @param array $parent The parent class' value.
  94. * @param bool $isAssoc Whether or not the merging should be done in associative mode.
  95. * @return mixed The updated value.
  96. */
  97. protected function _mergePropertyData($current, $parent, $isAssoc)
  98. {
  99. if (!$isAssoc) {
  100. return array_merge($parent, $current);
  101. }
  102. $parent = Hash::normalize($parent);
  103. foreach ($parent as $key => $value) {
  104. if (!isset($current[$key])) {
  105. $current[$key] = $value;
  106. }
  107. }
  108. return $current;
  109. }
  110. }