Route.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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\Annotation;
  11. /**
  12. * Annotation class for @Route().
  13. *
  14. * @Annotation
  15. * @Target({"CLASS", "METHOD"})
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class Route
  20. {
  21. private $path;
  22. private $localizedPaths = [];
  23. private $name;
  24. private $requirements = [];
  25. private $options = [];
  26. private $defaults = [];
  27. private $host;
  28. private $methods = [];
  29. private $schemes = [];
  30. private $condition;
  31. private $priority;
  32. /**
  33. * @param array $data An array of key/value parameters
  34. *
  35. * @throws \BadMethodCallException
  36. */
  37. public function __construct(array $data)
  38. {
  39. if (isset($data['localized_paths'])) {
  40. throw new \BadMethodCallException(sprintf('Unknown property "localized_paths" on annotation "%s".', static::class));
  41. }
  42. if (isset($data['value'])) {
  43. $data[\is_array($data['value']) ? 'localized_paths' : 'path'] = $data['value'];
  44. unset($data['value']);
  45. }
  46. if (isset($data['path']) && \is_array($data['path'])) {
  47. $data['localized_paths'] = $data['path'];
  48. unset($data['path']);
  49. }
  50. if (isset($data['locale'])) {
  51. $data['defaults']['_locale'] = $data['locale'];
  52. unset($data['locale']);
  53. }
  54. if (isset($data['format'])) {
  55. $data['defaults']['_format'] = $data['format'];
  56. unset($data['format']);
  57. }
  58. if (isset($data['utf8'])) {
  59. $data['options']['utf8'] = filter_var($data['utf8'], \FILTER_VALIDATE_BOOLEAN) ?: false;
  60. unset($data['utf8']);
  61. }
  62. if (isset($data['stateless'])) {
  63. $data['defaults']['_stateless'] = filter_var($data['stateless'], \FILTER_VALIDATE_BOOLEAN) ?: false;
  64. unset($data['stateless']);
  65. }
  66. foreach ($data as $key => $value) {
  67. $method = 'set'.str_replace('_', '', $key);
  68. if (!method_exists($this, $method)) {
  69. throw new \BadMethodCallException(sprintf('Unknown property "%s" on annotation "%s".', $key, static::class));
  70. }
  71. $this->$method($value);
  72. }
  73. }
  74. public function setPath($path)
  75. {
  76. $this->path = $path;
  77. }
  78. public function getPath()
  79. {
  80. return $this->path;
  81. }
  82. public function setLocalizedPaths(array $localizedPaths)
  83. {
  84. $this->localizedPaths = $localizedPaths;
  85. }
  86. public function getLocalizedPaths(): array
  87. {
  88. return $this->localizedPaths;
  89. }
  90. public function setHost($pattern)
  91. {
  92. $this->host = $pattern;
  93. }
  94. public function getHost()
  95. {
  96. return $this->host;
  97. }
  98. public function setName($name)
  99. {
  100. $this->name = $name;
  101. }
  102. public function getName()
  103. {
  104. return $this->name;
  105. }
  106. public function setRequirements($requirements)
  107. {
  108. $this->requirements = $requirements;
  109. }
  110. public function getRequirements()
  111. {
  112. return $this->requirements;
  113. }
  114. public function setOptions($options)
  115. {
  116. $this->options = $options;
  117. }
  118. public function getOptions()
  119. {
  120. return $this->options;
  121. }
  122. public function setDefaults($defaults)
  123. {
  124. $this->defaults = $defaults;
  125. }
  126. public function getDefaults()
  127. {
  128. return $this->defaults;
  129. }
  130. public function setSchemes($schemes)
  131. {
  132. $this->schemes = \is_array($schemes) ? $schemes : [$schemes];
  133. }
  134. public function getSchemes()
  135. {
  136. return $this->schemes;
  137. }
  138. public function setMethods($methods)
  139. {
  140. $this->methods = \is_array($methods) ? $methods : [$methods];
  141. }
  142. public function getMethods()
  143. {
  144. return $this->methods;
  145. }
  146. public function setCondition($condition)
  147. {
  148. $this->condition = $condition;
  149. }
  150. public function getCondition()
  151. {
  152. return $this->condition;
  153. }
  154. public function setPriority(int $priority): void
  155. {
  156. $this->priority = $priority;
  157. }
  158. public function getPriority(): ?int
  159. {
  160. return $this->priority;
  161. }
  162. }