Skip to content

Commit

Permalink
[zen-70] Options annotation
Browse files Browse the repository at this point in the history
- Added missing "Options" annotation, for providing options to forms,
  fieldsets, and elements
  • Loading branch information
weierophinney committed Jul 5, 2012
1 parent a09f8a1 commit 2c94918
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions library/Zend/Form/Annotation/AnnotationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class AnnotationBuilder implements EventManagerAwareInterface
'Input',
'InputFilter',
'Name',
'Options',
'Required',
'Type',
'Validator',
Expand Down
20 changes: 20 additions & 0 deletions library/Zend/Form/Annotation/ElementAnnotationsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function attach(EventManagerInterface $events)
$this->listeners[] = $events->attach('configureElement', array($this, 'handleFilterAnnotation'));
$this->listeners[] = $events->attach('configureElement', array($this, 'handleFlagsAnnotation'));
$this->listeners[] = $events->attach('configureElement', array($this, 'handleInputAnnotation'));
$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'));
Expand Down Expand Up @@ -248,6 +249,25 @@ public function handleInputAnnotation($e)
$filterSpec[$name] = $annotation->getInput();
}

/**
* 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
*
Expand Down
20 changes: 20 additions & 0 deletions library/Zend/Form/Annotation/FormAnnotationsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function attach(EventManagerInterface $events)
$this->listeners[] = $events->attach('configureForm', array($this, 'handleFlagsAnnotation'));
$this->listeners[] = $events->attach('configureForm', array($this, 'handleHydratorAnnotation'));
$this->listeners[] = $events->attach('configureForm', array($this, 'handleInputFilterAnnotation'));
$this->listeners[] = $events->attach('configureForm', array($this, 'handleOptionsAnnotation'));
$this->listeners[] = $events->attach('configureForm', array($this, 'handleTypeAnnotation'));

$this->listeners[] = $events->attach('discoverName', array($this, 'handleNameAnnotation'));
Expand Down Expand Up @@ -141,6 +142,25 @@ public function handleInputFilterAnnotation($e)
$formSpec['input_filter'] = $annotation->getInputFilter();
}

/**
* Handle the Options annotation
*
* Sets the options key of the form specification.
*
* @param \Zend\EventManager\EventInterface $e
* @return void
*/
public function handleOptionsAnnotation($e)
{
$annotation = $e->getParam('annotation');
if (!$annotation instanceof Options) {
return;
}

$formSpec = $e->getParam('formSpec');
$formSpec['options'] = $annotation->getOptions();
}

/**
* Handle the Type annotation
*
Expand Down
49 changes: 49 additions & 0 deletions library/Zend/Form/Annotation/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Form
* @subpackage Annotation
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Form\Annotation;

/**
* Options annotation
*
* Allows passing element, fieldset, or form options to the form factory.
* Options are used to alter the behavior of the object they address.
*
* The value should be an associative array.
*
* @Annotation
* @package Zend_Form
* @subpackage Annotation
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Options extends AbstractArrayAnnotation
{
/**
* Retrieve the options
*
* @return null|array
*/
public function getOptions()
{
return $this->value;
}
}
17 changes: 17 additions & 0 deletions tests/Zend/Form/Annotation/AnnotationBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,21 @@ public function testAllowsComposingChildEntities()
$this->assertTrue($composed->has('username'));
$this->assertTrue($composed->has('password'));
}

public function testCanHandleOptionsAnnotation()
{
$entity = new TestAsset\Annotation\EntityUsingOptions();
$builder = new Annotation\AnnotationBuilder();
$form = $builder->createForm($entity);

$this->assertTrue($form->useAsBaseFieldset());

$this->assertTrue($form->has('username'));

$username = $form->get('username');
$this->assertInstanceOf('Zend\Form\Element', $username);

$this->assertEquals('Username:', $username->getLabel());
$this->assertEquals(array('class' => 'label'), $username->getLabelAttributes());
}
}
15 changes: 15 additions & 0 deletions tests/Zend/Form/TestAsset/Annotation/EntityUsingOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace ZendTest\Form\TestAsset\Annotation;

use Zend\Form\Annotation;

/**
* @Annotation\Options({"use_as_base_fieldset":true})
*/
class EntityUsingOptions
{
/**
* @Annotation\Options({"label":"Username:", "label_attributes": {"class": "label"}})
*/
public $username;
}

0 comments on commit 2c94918

Please sign in to comment.