TreePrinter.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\CollectionTrait;
  17. use RecursiveIteratorIterator;
  18. /**
  19. * Iterator for flattening elements in a tree structure while adding some
  20. * visual markers for their relative position in the tree
  21. */
  22. class TreePrinter extends RecursiveIteratorIterator
  23. {
  24. use CollectionTrait;
  25. /**
  26. * A callable to generate the iteration key
  27. *
  28. * @var callable
  29. */
  30. protected $_key;
  31. /**
  32. * A callable to extract the display value
  33. *
  34. * @var callable
  35. */
  36. protected $_value;
  37. /**
  38. * Cached value for the current iteration element
  39. *
  40. * @var mixed
  41. */
  42. protected $_current;
  43. /**
  44. * The string to use for prefixing the values according to their depth in the tree.
  45. *
  46. * @var string
  47. */
  48. protected $_spacer;
  49. /**
  50. * Constructor
  51. *
  52. * @param \RecursiveIterator $items The iterator to flatten.
  53. * @param string|callable $valuePath The property to extract or a callable to return
  54. * the display value.
  55. * @param string|callable $keyPath The property to use as iteration key or a
  56. * callable returning the key value.
  57. * @param string $spacer The string to use for prefixing the values according to
  58. * their depth in the tree.
  59. * @param int $mode Iterator mode.
  60. */
  61. public function __construct($items, $valuePath, $keyPath, $spacer, $mode = RecursiveIteratorIterator::SELF_FIRST)
  62. {
  63. parent::__construct($items, $mode);
  64. $this->_value = $this->_propertyExtractor($valuePath);
  65. $this->_key = $this->_propertyExtractor($keyPath);
  66. $this->_spacer = $spacer;
  67. }
  68. /**
  69. * Returns the current iteration key
  70. *
  71. * @return mixed
  72. */
  73. public function key()
  74. {
  75. $extractor = $this->_key;
  76. return $extractor($this->_fetchCurrent(), parent::key(), $this);
  77. }
  78. /**
  79. * Returns the current iteration value
  80. *
  81. * @return string
  82. */
  83. public function current()
  84. {
  85. $extractor = $this->_value;
  86. $current = $this->_fetchCurrent();
  87. $spacer = str_repeat($this->_spacer, $this->getDepth());
  88. return $spacer . $extractor($current, parent::key(), $this);
  89. }
  90. /**
  91. * Advances the cursor one position
  92. *
  93. * @return void
  94. */
  95. public function next()
  96. {
  97. parent::next();
  98. $this->_current = null;
  99. }
  100. /**
  101. * Returns the current iteration element and caches its value
  102. *
  103. * @return mixed
  104. */
  105. protected function _fetchCurrent()
  106. {
  107. if ($this->_current !== null) {
  108. return $this->_current;
  109. }
  110. return $this->_current = parent::current();
  111. }
  112. }