Route.php 4.0 KB

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