/monica/monica/vendor/zendframework/zendframework/library/Zend/Form/Annotation/ElementAnnotationsListener.php

https://bitbucket.org/alexandretaz/maniac_divers · PHP · 369 lines · 184 code · 38 blank · 147 comment · 21 complexity · eb28af554a30e87ea5670a545138b553 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Form\Annotation;
  10. use Zend\EventManager\EventManagerInterface;
  11. /**
  12. * Default listeners for element annotations
  13. *
  14. * Defines and attaches a set of default listeners for element annotations
  15. * (which are defined on object properties). These include:
  16. *
  17. * - AllowEmpty
  18. * - Attributes
  19. * - ErrorMessage
  20. * - Filter
  21. * - Flags
  22. * - Input
  23. * - Hydrator
  24. * - Object
  25. * - Required
  26. * - Type
  27. * - Validator
  28. *
  29. * See the individual annotation classes for more details. The handlers registered
  30. * work with the annotation values, as well as the element and input specification
  31. * passed in the event object.
  32. */
  33. class ElementAnnotationsListener extends AbstractAnnotationsListener
  34. {
  35. /**
  36. * Attach listeners
  37. *
  38. * @param EventManagerInterface $events
  39. * @return void
  40. */
  41. public function attach(EventManagerInterface $events)
  42. {
  43. $this->listeners[] = $events->attach('configureElement', array($this, 'handleAllowEmptyAnnotation'));
  44. $this->listeners[] = $events->attach('configureElement', array($this, 'handleAttributesAnnotation'));
  45. $this->listeners[] = $events->attach('configureElement', array($this, 'handleComposedObjectAnnotation'));
  46. $this->listeners[] = $events->attach('configureElement', array($this, 'handleErrorMessageAnnotation'));
  47. $this->listeners[] = $events->attach('configureElement', array($this, 'handleFilterAnnotation'));
  48. $this->listeners[] = $events->attach('configureElement', array($this, 'handleFlagsAnnotation'));
  49. $this->listeners[] = $events->attach('configureElement', array($this, 'handleHydratorAnnotation'));
  50. $this->listeners[] = $events->attach('configureElement', array($this, 'handleInputAnnotation'));
  51. $this->listeners[] = $events->attach('configureElement', array($this, 'handleObjectAnnotation'));
  52. $this->listeners[] = $events->attach('configureElement', array($this, 'handleOptionsAnnotation'));
  53. $this->listeners[] = $events->attach('configureElement', array($this, 'handleRequiredAnnotation'));
  54. $this->listeners[] = $events->attach('configureElement', array($this, 'handleTypeAnnotation'));
  55. $this->listeners[] = $events->attach('configureElement', array($this, 'handleValidatorAnnotation'));
  56. $this->listeners[] = $events->attach('discoverName', array($this, 'handleNameAnnotation'));
  57. $this->listeners[] = $events->attach('discoverName', array($this, 'discoverFallbackName'));
  58. $this->listeners[] = $events->attach('checkForExclude', array($this, 'handleExcludeAnnotation'));
  59. }
  60. /**
  61. * Handle the AllowEmpty annotation
  62. *
  63. * Sets the allow_empty flag on the input specification array.
  64. *
  65. * @param \Zend\EventManager\EventInterface $e
  66. * @return void
  67. */
  68. public function handleAllowEmptyAnnotation($e)
  69. {
  70. $annotation = $e->getParam('annotation');
  71. if (!$annotation instanceof AllowEmpty) {
  72. return;
  73. }
  74. $inputSpec = $e->getParam('inputSpec');
  75. $inputSpec['allow_empty'] = true;
  76. }
  77. /**
  78. * Handle the Attributes annotation
  79. *
  80. * Sets the attributes array of the element specification.
  81. *
  82. * @param \Zend\EventManager\EventInterface $e
  83. * @return void
  84. */
  85. public function handleAttributesAnnotation($e)
  86. {
  87. $annotation = $e->getParam('annotation');
  88. if (!$annotation instanceof Attributes) {
  89. return;
  90. }
  91. $elementSpec = $e->getParam('elementSpec');
  92. if (isset($elementSpec['spec']['attributes'])) {
  93. $elementSpec['spec']['attributes'] = array_merge($elementSpec['spec']['attributes'], $annotation->getAttributes());
  94. return;
  95. }
  96. $elementSpec['spec']['attributes'] = $annotation->getAttributes();
  97. }
  98. /**
  99. * Allow creating fieldsets from composed entity properties
  100. *
  101. * @param \Zend\EventManager\EventInterface $e
  102. * @return void
  103. */
  104. public function handleComposedObjectAnnotation($e)
  105. {
  106. $annotation = $e->getParam('annotation');
  107. if (!$annotation instanceof ComposedObject) {
  108. return;
  109. }
  110. $class = $annotation->getComposedObject();
  111. $annotationManager = $e->getTarget();
  112. $specification = $annotationManager->getFormSpecification($class);
  113. $name = $e->getParam('name');
  114. $elementSpec = $e->getParam('elementSpec');
  115. $filterSpec = $e->getParam('filterSpec');
  116. // Compose input filter into parent input filter
  117. $inputFilter = $specification['input_filter'];
  118. if (!isset($inputFilter['type'])) {
  119. $inputFilter['type'] = 'Zend\InputFilter\InputFilter';
  120. }
  121. $e->setParam('inputSpec', $inputFilter);
  122. unset($specification['input_filter']);
  123. // Compose specification as a fieldset into parent form/fieldset
  124. if (!isset($specification['type'])) {
  125. $specification['type'] = 'Zend\Form\Fieldset';
  126. }
  127. $elementSpec['spec'] = $specification;
  128. $elementSpec['spec']['name'] = $name;
  129. }
  130. /**
  131. * Handle the ErrorMessage annotation
  132. *
  133. * Sets the error_message of the input specification.
  134. *
  135. * @param \Zend\EventManager\EventInterface $e
  136. * @return void
  137. */
  138. public function handleErrorMessageAnnotation($e)
  139. {
  140. $annotation = $e->getParam('annotation');
  141. if (!$annotation instanceof ErrorMessage) {
  142. return;
  143. }
  144. $inputSpec = $e->getParam('inputSpec');
  145. $inputSpec['error_message'] = $annotation->getMessage();
  146. }
  147. /**
  148. * Determine if the element has been marked to exclude from the definition
  149. *
  150. * @param \Zend\EventManager\EventInterface $e
  151. * @return bool
  152. */
  153. public function handleExcludeAnnotation($e)
  154. {
  155. $annotations = $e->getParam('annotations');
  156. if ($annotations->hasAnnotation('Zend\Form\Annotation\Exclude')) {
  157. return true;
  158. }
  159. return false;
  160. }
  161. /**
  162. * Handle the Filter annotation
  163. *
  164. * Adds a filter to the filter chain specification for the input.
  165. *
  166. * @param \Zend\EventManager\EventInterface $e
  167. * @return void
  168. */
  169. public function handleFilterAnnotation($e)
  170. {
  171. $annotation = $e->getParam('annotation');
  172. if (!$annotation instanceof Filter) {
  173. return;
  174. }
  175. $inputSpec = $e->getParam('inputSpec');
  176. if (!isset($inputSpec['filters'])) {
  177. $inputSpec['filters'] = array();
  178. }
  179. $inputSpec['filters'][] = $annotation->getFilter();
  180. }
  181. /**
  182. * Handle the Flags annotation
  183. *
  184. * Sets the element flags in the specification (used typically for setting
  185. * priority).
  186. *
  187. * @param \Zend\EventManager\EventInterface $e
  188. * @return void
  189. */
  190. public function handleFlagsAnnotation($e)
  191. {
  192. $annotation = $e->getParam('annotation');
  193. if (!$annotation instanceof Flags) {
  194. return;
  195. }
  196. $elementSpec = $e->getParam('elementSpec');
  197. $elementSpec['flags'] = $annotation->getFlags();
  198. }
  199. /**
  200. * Handle the Hydrator annotation
  201. *
  202. * Sets the hydrator class to use in the fieldset specification.
  203. *
  204. * @param \Zend\EventManager\EventInterface $e
  205. * @return void
  206. */
  207. public function handleHydratorAnnotation($e)
  208. {
  209. $annotation = $e->getParam('annotation');
  210. if (!$annotation instanceof Hydrator) {
  211. return;
  212. }
  213. $elementSpec = $e->getParam('element');
  214. $elementSpec['hydrator'] = $annotation->getHydrator();
  215. }
  216. /**
  217. * Handle the Input annotation
  218. *
  219. * Sets the filter specification for the current element to the specified
  220. * input class name.
  221. *
  222. * @param \Zend\EventManager\EventInterface $e
  223. * @return void
  224. */
  225. public function handleInputAnnotation($e)
  226. {
  227. $annotation = $e->getParam('annotation');
  228. if (!$annotation instanceof Input) {
  229. return;
  230. }
  231. $name = $e->getParam('name');
  232. $filterSpec = $e->getParam('filterSpec');
  233. $filterSpec[$name] = $annotation->getInput();
  234. }
  235. /**
  236. * Handle the Object annotation
  237. *
  238. * Sets the object to bind to the form or fieldset
  239. *
  240. * @param \Zend\EventManager\EventInterface $e
  241. * @return void
  242. */
  243. public function handleObjectAnnotation($e)
  244. {
  245. $annotation = $e->getParam('annotation');
  246. if (!$annotation instanceof Object) {
  247. return;
  248. }
  249. $elementSpec = $e->getParam('elementSpec');
  250. $elementSpec['object'] = $annotation->getObject();
  251. }
  252. /**
  253. * Handle the Options annotation
  254. *
  255. * Sets the element options in the specification.
  256. *
  257. * @param \Zend\EventManager\EventInterface $e
  258. * @return void
  259. */
  260. public function handleOptionsAnnotation($e)
  261. {
  262. $annotation = $e->getParam('annotation');
  263. if (!$annotation instanceof Options) {
  264. return;
  265. }
  266. $elementSpec = $e->getParam('elementSpec');
  267. $elementSpec['spec']['options'] = $annotation->getOptions();
  268. }
  269. /**
  270. * Handle the Required annotation
  271. *
  272. * Sets the required flag on the input based on the annotation value.
  273. *
  274. * @param \Zend\EventManager\EventInterface $e
  275. * @return void
  276. */
  277. public function handleRequiredAnnotation($e)
  278. {
  279. $annotation = $e->getParam('annotation');
  280. if (!$annotation instanceof Required) {
  281. return;
  282. }
  283. $required = (bool) $annotation->getRequired();
  284. $inputSpec = $e->getParam('inputSpec');
  285. $inputSpec['required'] = $required;
  286. if ($required) {
  287. $elementSpec = $e->getParam('elementSpec');
  288. if (!isset($elementSpec['spec']['attributes'])) {
  289. $elementSpec['spec']['attributes'] = array();
  290. }
  291. $elementSpec['spec']['attributes']['required'] = 'required';
  292. }
  293. }
  294. /**
  295. * Handle the Type annotation
  296. *
  297. * Sets the element class type to use in the element specification.
  298. *
  299. * @param \Zend\EventManager\EventInterface $e
  300. * @return void
  301. */
  302. public function handleTypeAnnotation($e)
  303. {
  304. $annotation = $e->getParam('annotation');
  305. if (!$annotation instanceof Type) {
  306. return;
  307. }
  308. $elementSpec = $e->getParam('elementSpec');
  309. $elementSpec['spec']['type'] = $annotation->getType();
  310. }
  311. /**
  312. * Handle the Validator annotation
  313. *
  314. * Adds a validator to the validator chain of the input specification.
  315. *
  316. * @param \Zend\EventManager\EventInterface $e
  317. * @return void
  318. */
  319. public function handleValidatorAnnotation($e)
  320. {
  321. $annotation = $e->getParam('annotation');
  322. if (!$annotation instanceof Validator) {
  323. return;
  324. }
  325. $inputSpec = $e->getParam('inputSpec');
  326. if (!isset($inputSpec['validators'])) {
  327. $inputSpec['validators'] = array();
  328. }
  329. $inputSpec['validators'][] = $annotation->getValidator();
  330. }
  331. }