InsertIterator.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\Collection\Iterator;
  16. use Cake\Collection\Collection;
  17. /**
  18. * This iterator will insert values into a property of each of the records returned.
  19. * The values to be inserted come out of another traversal object. This is useful
  20. * when you have two separate collections and want to merge them together by placing
  21. * each of the values from one collection into a property inside the other collection.
  22. */
  23. class InsertIterator extends Collection
  24. {
  25. /**
  26. * The collection from which to extract the values to be inserted
  27. *
  28. * @var \Cake\Collection\Collection
  29. */
  30. protected $_values;
  31. /**
  32. * Holds whether the values collection is still valid. (has more records)
  33. *
  34. * @var bool
  35. */
  36. protected $_validValues = true;
  37. /**
  38. * An array containing each of the properties to be traversed to reach the
  39. * point where the values should be inserted.
  40. *
  41. * @var array
  42. */
  43. protected $_path;
  44. /**
  45. * The property name to which values will be assigned
  46. *
  47. * @var string
  48. */
  49. protected $_target;
  50. /**
  51. * Constructs a new collection that will dynamically add properties to it out of
  52. * the values found in $values.
  53. *
  54. * @param array|\Traversable $into The target collection to which the values will
  55. * be inserted at the specified path.
  56. * @param string $path A dot separated list of properties that need to be traversed
  57. * to insert the value into the target collection.
  58. * @param array|\Traversable $values The source collection from which the values will
  59. * be inserted at the specified path.
  60. */
  61. public function __construct($into, $path, $values)
  62. {
  63. parent::__construct($into);
  64. if (!($values instanceof Collection)) {
  65. $values = new Collection($values);
  66. }
  67. $path = explode('.', $path);
  68. $target = array_pop($path);
  69. $this->_path = $path;
  70. $this->_target = $target;
  71. $this->_values = $values;
  72. }
  73. /**
  74. * Advances the cursor to the next record
  75. *
  76. * @return void
  77. */
  78. public function next()
  79. {
  80. parent::next();
  81. if ($this->_validValues) {
  82. $this->_values->next();
  83. }
  84. $this->_validValues = $this->_values->valid();
  85. }
  86. /**
  87. * Returns the current element in the target collection after inserting
  88. * the value from the source collection into the specified path.
  89. *
  90. * @return mixed
  91. */
  92. public function current()
  93. {
  94. $row = parent::current();
  95. if (!$this->_validValues) {
  96. return $row;
  97. }
  98. $pointer =& $row;
  99. foreach ($this->_path as $step) {
  100. if (!isset($pointer[$step])) {
  101. return $row;
  102. }
  103. $pointer =& $pointer[$step];
  104. }
  105. $pointer[$this->_target] = $this->_values->current();
  106. return $row;
  107. }
  108. /**
  109. * Resets the collection pointer.
  110. *
  111. * @return void
  112. */
  113. public function rewind()
  114. {
  115. parent::rewind();
  116. $this->_values->rewind();
  117. $this->_validValues = $this->_values->valid();
  118. }
  119. }