DumperCollection.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. class DumperCollection implements \IteratorAggregate
  17. {
  18. /**
  19. * @var DumperCollection|null
  20. */
  21. private $parent;
  22. /**
  23. * @var (DumperCollection|DumperRoute)[]
  24. */
  25. private $children = array();
  26. /**
  27. * @var array
  28. */
  29. private $attributes = array();
  30. /**
  31. * Returns the children routes and collections.
  32. *
  33. * @return (DumperCollection|DumperRoute)[] Array of DumperCollection|DumperRoute
  34. */
  35. public function all()
  36. {
  37. return $this->children;
  38. }
  39. /**
  40. * Adds a route or collection
  41. *
  42. * @param DumperRoute|DumperCollection The route or collection
  43. */
  44. public function add($child)
  45. {
  46. if ($child instanceof DumperCollection) {
  47. $child->setParent($this);
  48. }
  49. $this->children[] = $child;
  50. }
  51. /**
  52. * Sets children.
  53. *
  54. * @param array $children The children
  55. */
  56. public function setAll(array $children)
  57. {
  58. foreach ($children as $child) {
  59. if ($child instanceof DumperCollection) {
  60. $child->setParent($this);
  61. }
  62. }
  63. $this->children = $children;
  64. }
  65. /**
  66. * Returns an iterator over the children.
  67. *
  68. * @return \Iterator The iterator
  69. */
  70. public function getIterator()
  71. {
  72. return new \ArrayIterator($this->children);
  73. }
  74. /**
  75. * Returns the root of the collection.
  76. *
  77. * @return DumperCollection The root collection
  78. */
  79. public function getRoot()
  80. {
  81. return (null !== $this->parent) ? $this->parent->getRoot() : $this;
  82. }
  83. /**
  84. * Returns the parent collection.
  85. *
  86. * @return DumperCollection|null The parent collection or null if the collection has no parent
  87. */
  88. protected function getParent()
  89. {
  90. return $this->parent;
  91. }
  92. /**
  93. * Sets the parent collection.
  94. *
  95. * @param DumperCollection $parent 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 Boolean 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. }