/** * @expectedException InvalidArgumentException */ public function testOptions() { $element = new Element('test', array(), array('opt1' => 'value')); $this->assertInternalType('null', $element->getOption('opt_unknown')); $this->assertEquals('value', $element->getOption('opt1')); $this->assertEquals(array('opt1' => 'value'), $element->getOptions()); $element->setOptions(array('model_link' => array('Model\\TestModel'))); $this->assertEquals(2, count($element->getModelLink())); $element = new Element('test', array(), array('opt1' => 'value')); $element->setOption('model_link', array('Model\\TestModel')); $this->assertEquals(2, count($element->getModelLink())); $element = new Element('test', array(), array('opt' => 'value')); $this->assertEquals('value', $element->getOption('opt')); $this->assertEquals(array('value', 'value2'), $element->addOption('opt', 'value2')->getOption('opt')); // Exception $element->setOptions(true); }
/** * Configure an element based on the provided specification * * Specification can contain any of the following: * - type: the Element class to use; defaults to \Zend\Form\Element * - name: what name to provide the element, if any * - options: an array, Traversable, or ArrayAccess object of element options * - attributes: an array, Traversable, or ArrayAccess object of element * attributes to assign * * @param \App\Form\Element|\App\Form\Element $element * @param array|Traversable|ArrayAccess $spec * @return Element */ public function configureElement(Element $element, $spec) { $spec = $this->validateSpecification($spec, __METHOD__); $name = isset($spec['name']) ? $spec['name'] : null; $options = isset($spec['options']) ? $spec['options'] : null; $attributes = isset($spec['attributes']) ? $spec['attributes'] : null; if ($name !== null && $name !== '') { $element->setName($name); } if (is_array($options) || $options instanceof Traversable || $options instanceof ArrayAccess) { $element->setOptions($options); } if (is_array($attributes) || $attributes instanceof Traversable || $attributes instanceof ArrayAccess) { $element->setAttributes($attributes); } return $element; }