Source of file ElementAnnotationsListener.php
Size: 13,361 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Form/Annotation/ElementAnnotationsListener.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Form\Annotation; use Zend\EventManager\EventManagerInterface; use Zend\Stdlib\ArrayObject; /** * Default listeners for element annotations * * Defines and attaches a set of default listeners for element annotations * (which are defined on object properties). These include: * * - AllowEmpty * - Attributes * - ErrorMessage * - Filter * - Flags * - Input * - Hydrator * - Object * - Required * - Type * - Validator * * See the individual annotation classes for more details. The handlers registered * work with the annotation values, as well as the element and input specification * passed in the event object. */ class ElementAnnotationsListener extends AbstractAnnotationsListener { /** * {@inheritDoc} */ public function attach(EventManagerInterface $events) { $this->listeners[] = $events->attach('configureElement', array($this, 'handleAllowEmptyAnnotation')); $this->listeners[] = $events->attach('configureElement', array($this, 'handleAttributesAnnotation')); $this->listeners[] = $events->attach('configureElement', array($this, 'handleComposedObjectAnnotation')); $this->listeners[] = $events->attach('configureElement', array($this, 'handleErrorMessageAnnotation')); $this->listeners[] = $events->attach('configureElement', array($this, 'handleFilterAnnotation')); $this->listeners[] = $events->attach('configureElement', array($this, 'handleFlagsAnnotation')); $this->listeners[] = $events->attach('configureElement', array($this, 'handleHydratorAnnotation')); $this->listeners[] = $events->attach('configureElement', array($this, 'handleInputAnnotation')); $this->listeners[] = $events->attach('configureElement', array($this, 'handleObjectAnnotation')); $this->listeners[] = $events->attach('configureElement', array($this, 'handleOptionsAnnotation')); $this->listeners[] = $events->attach('configureElement', array($this, 'handleRequiredAnnotation')); $this->listeners[] = $events->attach('configureElement', array($this, 'handleTypeAnnotation')); $this->listeners[] = $events->attach('configureElement', array($this, 'handleValidatorAnnotation')); $this->listeners[] = $events->attach('discoverName', array($this, 'handleNameAnnotation')); $this->listeners[] = $events->attach('discoverName', array($this, 'discoverFallbackName')); $this->listeners[] = $events->attach('checkForExclude', array($this, 'handleExcludeAnnotation')); } /** * Handle the AllowEmpty annotation * * Sets the allow_empty flag on the input specification array. * * @param \Zend\EventManager\EventInterface $e * @return void */ public function handleAllowEmptyAnnotation($e) { $annotation = $e->getParam('annotation'); if (!$annotation instanceof AllowEmpty) { return; } $inputSpec = $e->getParam('inputSpec'); $inputSpec['allow_empty'] = true; } /** * Handle the Attributes annotation * * Sets the attributes array of the element specification. * * @param \Zend\EventManager\EventInterface $e * @return void */ public function handleAttributesAnnotation($e) { $annotation = $e->getParam('annotation'); if (!$annotation instanceof Attributes) { return; } $elementSpec = $e->getParam('elementSpec'); if (isset($elementSpec['spec']['attributes'])) { $elementSpec['spec']['attributes'] = array_merge($elementSpec['spec']['attributes'], $annotation->getAttributes()); return; } $elementSpec['spec']['attributes'] = $annotation->getAttributes(); } /** * Allow creating fieldsets from composed entity properties * * @param \Zend\EventManager\EventInterface $e * @return void */ public function handleComposedObjectAnnotation($e) { $annotation = $e->getParam('annotation'); if (!$annotation instanceof ComposedObject) { return; } $class = $annotation->getComposedObject(); $annotationManager = $e->getTarget(); $specification = $annotationManager->getFormSpecification($class); $name = $e->getParam('name'); $elementSpec = $e->getParam('elementSpec'); $filterSpec = $e->getParam('filterSpec'); if ($annotation->isCollection()) { // Compose specification as a fieldset into parent form/fieldset if (!isset($specification['type'])) { //use input filter provider fieldset so we can compose the input filter into the fieldset //it is assumed that if someone uses a custom fieldset, they will take care of the input //filtering themselves or consume the input_filter_spec option. $specification['type'] = 'Zend\Form\InputFilterProviderFieldset'; } $inputFilter = $specification['input_filter']; if (!isset($inputFilter['type'])) { $inputFilter['type'] = 'Zend\InputFilter\InputFilter'; } unset($specification['input_filter']); $elementSpec['spec']['type'] = 'Zend\Form\Element\Collection'; $elementSpec['spec']['name'] = $name; $elementSpec['spec']['options'] = new ArrayObject($annotation->getOptions()); $elementSpec['spec']['options']['target_element'] = $specification; $elementSpec['spec']['options']['target_element']['options']['input_filter_spec'] = $inputFilter; if (isset($specification['hydrator'])) { $elementSpec['spec']['hydrator'] = $specification['hydrator']; } } else { // Compose input filter into parent input filter $inputFilter = $specification['input_filter']; if (!isset($inputFilter['type'])) { $inputFilter['type'] = 'Zend\InputFilter\InputFilter'; } $e->setParam('inputSpec', $inputFilter); unset($specification['input_filter']); // Compose specification as a fieldset into parent form/fieldset if (!isset($specification['type'])) { $specification['type'] = 'Zend\Form\Fieldset'; } // Merge options of composed object with the ones of the target object: $options = (isset($elementSpec['spec']['options']) && is_array($elementSpec['spec']['options'])) ? $elementSpec['spec']['options'] : array(); $options = array_merge($options, $annotation->getOptions()); // Add element spec: $elementSpec['spec'] = $specification; $elementSpec['spec']['name'] = $name; $elementSpec['spec']['options'] = new ArrayObject($options); } } /** * Handle the ErrorMessage annotation * * Sets the error_message of the input specification. * * @param \Zend\EventManager\EventInterface $e * @return void */ public function handleErrorMessageAnnotation($e) { $annotation = $e->getParam('annotation'); if (!$annotation instanceof ErrorMessage) { return; } $inputSpec = $e->getParam('inputSpec'); $inputSpec['error_message'] = $annotation->getMessage(); } /** * Determine if the element has been marked to exclude from the definition * * @param \Zend\EventManager\EventInterface $e * @return bool */ public function handleExcludeAnnotation($e) { $annotations = $e->getParam('annotations'); if ($annotations->hasAnnotation('Zend\Form\Annotation\Exclude')) { return true; } return false; } /** * Handle the Filter annotation * * Adds a filter to the filter chain specification for the input. * * @param \Zend\EventManager\EventInterface $e * @return void */ public function handleFilterAnnotation($e) { $annotation = $e->getParam('annotation'); if (!$annotation instanceof Filter) { return; } $inputSpec = $e->getParam('inputSpec'); if (!isset($inputSpec['filters'])) { $inputSpec['filters'] = array(); } $inputSpec['filters'][] = $annotation->getFilter(); } /** * Handle the Flags annotation * * Sets the element flags in the specification (used typically for setting * priority). * * @param \Zend\EventManager\EventInterface $e * @return void */ public function handleFlagsAnnotation($e) { $annotation = $e->getParam('annotation'); if (!$annotation instanceof Flags) { return; } $elementSpec = $e->getParam('elementSpec'); $elementSpec['flags'] = $annotation->getFlags(); } /** * Handle the Hydrator annotation * * Sets the hydrator class to use in the fieldset specification. * * @param \Zend\EventManager\EventInterface $e * @return void */ public function handleHydratorAnnotation($e) { $annotation = $e->getParam('annotation'); if (!$annotation instanceof Hydrator) { return; } $elementSpec = $e->getParam('elementSpec'); $elementSpec['spec']['hydrator'] = $annotation->getHydrator(); } /** * Handle the Input annotation * * Sets the filter specification for the current element to the specified * input class name. * * @param \Zend\EventManager\EventInterface $e * @return void */ public function handleInputAnnotation($e) { $annotation = $e->getParam('annotation'); if (!$annotation instanceof Input) { return; } $inputSpec = $e->getParam('inputSpec'); $inputSpec['type'] = $annotation->getInput(); } /** * Handle the Object annotation * * Sets the object to bind to the form or fieldset * * @param \Zend\EventManager\EventInterface $e * @return void */ public function handleObjectAnnotation($e) { $annotation = $e->getParam('annotation'); if (!$annotation instanceof Object) { return; } $elementSpec = $e->getParam('elementSpec'); $elementSpec['spec']['object'] = $annotation->getObject(); } /** * Handle the Options annotation * * Sets the element options in the specification. * * @param \Zend\EventManager\EventInterface $e * @return void */ public function handleOptionsAnnotation($e) { $annotation = $e->getParam('annotation'); if (!$annotation instanceof Options) { return; } $elementSpec = $e->getParam('elementSpec'); $elementSpec['spec']['options'] = $annotation->getOptions(); } /** * Handle the Required annotation * * Sets the required flag on the input based on the annotation value. * * @param \Zend\EventManager\EventInterface $e * @return void */ public function handleRequiredAnnotation($e) { $annotation = $e->getParam('annotation'); if (!$annotation instanceof Required) { return; } $required = (bool) $annotation->getRequired(); $inputSpec = $e->getParam('inputSpec'); $inputSpec['required'] = $required; if ($required) { $elementSpec = $e->getParam('elementSpec'); if (!isset($elementSpec['spec']['attributes'])) { $elementSpec['spec']['attributes'] = array(); } $elementSpec['spec']['attributes']['required'] = 'required'; } } /** * Handle the Type annotation * * Sets the element class type to use in the element specification. * * @param \Zend\EventManager\EventInterface $e * @return void */ public function handleTypeAnnotation($e) { $annotation = $e->getParam('annotation'); if (!$annotation instanceof Type) { return; } $elementSpec = $e->getParam('elementSpec'); $elementSpec['spec']['type'] = $annotation->getType(); } /** * Handle the Validator annotation * * Adds a validator to the validator chain of the input specification. * * @param \Zend\EventManager\EventInterface $e * @return void */ public function handleValidatorAnnotation($e) { $annotation = $e->getParam('annotation'); if (!$annotation instanceof Validator) { return; } $inputSpec = $e->getParam('inputSpec'); if (!isset($inputSpec['validators'])) { $inputSpec['validators'] = array(); } $inputSpec['validators'][] = $annotation->getValidator(); } } |