菜谱项目

DumperCollection.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing\Matcher\Dumper;
  11. /**
  12. * Collection of routes.
  13. *
  14. * @author Arnaud Le Blanc <arnaud.lb@gmail.com>
  15. *
  16. * @internal
  17. */
  18. class DumperCollection implements \IteratorAggregate
  19. {
  20. /**
  21. * @var DumperCollection|null
  22. */
  23. private $parent;
  24. /**
  25. * @var DumperCollection[]|DumperRoute[]
  26. */
  27. private $children = array();
  28. /**
  29. * @var array
  30. */
  31. private $attributes = array();
  32. /**
  33. * Returns the children routes and collections.
  34. *
  35. * @return self[]|DumperRoute[]
  36. */
  37. public function all()
  38. {
  39. return $this->children;
  40. }
  41. /**
  42. * Adds a route or collection.
  43. *
  44. * @param DumperRoute|DumperCollection The route or collection
  45. */
  46. public function add($child)
  47. {
  48. if ($child instanceof self) {
  49. $child->setParent($this);
  50. }
  51. $this->children[] = $child;
  52. }
  53. /**
  54. * Sets children.
  55. *
  56. * @param array $children The children
  57. */
  58. public function setAll(array $children)
  59. {
  60. foreach ($children as $child) {
  61. if ($child instanceof self) {
  62. $child->setParent($this);
  63. }
  64. }
  65. $this->children = $children;
  66. }
  67. /**
  68. * Returns an iterator over the children.
  69. *
  70. * @return \Iterator|DumperCollection[]|DumperRoute[] The iterator
  71. */
  72. public function getIterator()
  73. {
  74. return new \ArrayIterator($this->children);
  75. }
  76. /**
  77. * Returns the root of the collection.
  78. *
  79. * @return self The root collection
  80. */
  81. public function getRoot()
  82. {
  83. return (null !== $this->parent) ? $this->parent->getRoot() : $this;
  84. }
  85. /**
  86. * Returns the parent collection.
  87. *
  88. * @return self|null The parent collection or null if the collection has no parent
  89. */
  90. protected function getParent()
  91. {
  92. return $this->parent;
  93. }
  94. /**
  95. * Sets the parent collection.
  96. */
  97. protected function setParent(DumperCollection $parent)
  98. {
  99. $this->parent = $parent;
  100. }
  101. /**
  102. * Returns true if the attribute is defined.
  103. *
  104. * @param string $name The attribute name
  105. *
  106. * @return bool true if the attribute is defined, false otherwise
  107. */
  108. public function hasAttribute($name)
  109. {
  110. return array_key_exists($name, $this->attributes);
  111. }
  112. /**
  113. * Returns an attribute by name.
  114. *
  115. * @param string $name The attribute name
  116. * @param mixed $default Default value is the attribute doesn't exist
  117. *
  118. * @return mixed The attribute value
  119. */
  120. public function getAttribute($name, $default = null)
  121. {
  122. return $this->hasAttribute($name) ? $this->attributes[$name] : $default;
  123. }
  124. /**
  125. * Sets an attribute by name.
  126. *
  127. * @param string $name The attribute name
  128. * @param mixed $value The attribute value
  129. */
  130. public function setAttribute($name, $value)
  131. {
  132. $this->attributes[$name] = $value;
  133. }
  134. /**
  135. * Sets multiple attributes.
  136. *
  137. * @param array $attributes The attributes
  138. */
  139. public function setAttributes($attributes)
  140. {
  141. $this->attributes = $attributes;
  142. }
  143. }