DumperCollection.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. * @param DumperCollection $parent The parent collection
  98. */
  99. protected function setParent(DumperCollection $parent)
  100. {
  101. $this->parent = $parent;
  102. }
  103. /**
  104. * Returns true if the attribute is defined.
  105. *
  106. * @param string $name The attribute name
  107. *
  108. * @return bool true if the attribute is defined, false otherwise
  109. */
  110. public function hasAttribute($name)
  111. {
  112. return array_key_exists($name, $this->attributes);
  113. }
  114. /**
  115. * Returns an attribute by name.
  116. *
  117. * @param string $name The attribute name
  118. * @param mixed $default Default value is the attribute doesn't exist
  119. *
  120. * @return mixed The attribute value
  121. */
  122. public function getAttribute($name, $default = null)
  123. {
  124. return $this->hasAttribute($name) ? $this->attributes[$name] : $default;
  125. }
  126. /**
  127. * Sets an attribute by name.
  128. *
  129. * @param string $name The attribute name
  130. * @param mixed $value The attribute value
  131. */
  132. public function setAttribute($name, $value)
  133. {
  134. $this->attributes[$name] = $value;
  135. }
  136. /**
  137. * Sets multiple attributes.
  138. *
  139. * @param array $attributes The attributes
  140. */
  141. public function setAttributes($attributes)
  142. {
  143. $this->attributes = $attributes;
  144. }
  145. }