vendor/sensio/framework-extra-bundle/src/Configuration/ParamConverter.php line 61

Open in your IDE?
  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 Sensio\Bundle\FrameworkExtraBundle\Configuration;
  11. /**
  12. * The ParamConverter class handles the ParamConverter annotation parts.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. * @Annotation
  16. */
  17. #[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
  18. class ParamConverter extends ConfigurationAnnotation
  19. {
  20. /**
  21. * The parameter name.
  22. *
  23. * @var string
  24. */
  25. private $name;
  26. /**
  27. * The parameter class.
  28. *
  29. * @var string
  30. */
  31. private $class;
  32. /**
  33. * An array of options.
  34. *
  35. * @var array
  36. */
  37. private $options = [];
  38. /**
  39. * Whether or not the parameter is optional.
  40. *
  41. * @var bool
  42. */
  43. private $isOptional = false;
  44. /**
  45. * Use explicitly named converter instead of iterating by priorities.
  46. *
  47. * @var string
  48. */
  49. private $converter;
  50. /**
  51. * @param array|string $data
  52. */
  53. public function __construct(
  54. $data = [],
  55. string $class = null,
  56. array $options = [],
  57. bool $isOptional = false,
  58. string $converter = null
  59. ) {
  60. $values = [];
  61. if (\is_string($data)) {
  62. $values['value'] = $data;
  63. } else {
  64. $values = $data;
  65. }
  66. $values['class'] = $values['class'] ?? $class;
  67. $values['options'] = $values['options'] ?? $options;
  68. $values['isOptional'] = $values['isOptional'] ?? $isOptional;
  69. $values['converter'] = $values['converter'] ?? $converter;
  70. parent::__construct($values);
  71. }
  72. /**
  73. * Returns the parameter name.
  74. *
  75. * @return string
  76. */
  77. public function getName()
  78. {
  79. return $this->name;
  80. }
  81. /**
  82. * Sets the parameter name.
  83. *
  84. * @param string $name The parameter name
  85. */
  86. public function setValue($name)
  87. {
  88. $this->setName($name);
  89. }
  90. /**
  91. * Sets the parameter name.
  92. *
  93. * @param string $name The parameter name
  94. */
  95. public function setName($name)
  96. {
  97. $this->name = $name;
  98. }
  99. /**
  100. * Returns the parameter class name.
  101. *
  102. * @return string
  103. */
  104. public function getClass()
  105. {
  106. return $this->class;
  107. }
  108. /**
  109. * Sets the parameter class name.
  110. *
  111. * @param string $class The parameter class name
  112. */
  113. public function setClass($class)
  114. {
  115. $this->class = $class;
  116. }
  117. /**
  118. * Returns an array of options.
  119. *
  120. * @return array
  121. */
  122. public function getOptions()
  123. {
  124. return $this->options;
  125. }
  126. /**
  127. * Sets an array of options.
  128. *
  129. * @param array $options An array of options
  130. */
  131. public function setOptions($options)
  132. {
  133. $this->options = $options;
  134. }
  135. /**
  136. * Sets whether or not the parameter is optional.
  137. *
  138. * @param bool $optional Whether the parameter is optional
  139. */
  140. public function setIsOptional($optional)
  141. {
  142. $this->isOptional = (bool) $optional;
  143. }
  144. /**
  145. * Returns whether or not the parameter is optional.
  146. *
  147. * @return bool
  148. */
  149. public function isOptional()
  150. {
  151. return $this->isOptional;
  152. }
  153. /**
  154. * Get explicit converter name.
  155. *
  156. * @return string
  157. */
  158. public function getConverter()
  159. {
  160. return $this->converter;
  161. }
  162. /**
  163. * Set explicit converter name.
  164. *
  165. * @param string $converter
  166. */
  167. public function setConverter($converter)
  168. {
  169. $this->converter = $converter;
  170. }
  171. /**
  172. * Returns the annotation alias name.
  173. *
  174. * @return string
  175. *
  176. * @see ConfigurationInterface
  177. */
  178. public function getAliasName()
  179. {
  180. return 'converters';
  181. }
  182. /**
  183. * Multiple ParamConverters are allowed.
  184. *
  185. * @return bool
  186. *
  187. * @see ConfigurationInterface
  188. */
  189. public function allowArray()
  190. {
  191. return true;
  192. }
  193. }