Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Merge branch 'pr4666'
Browse files Browse the repository at this point in the history
  • Loading branch information
akrabat committed Jul 4, 2013
2 parents 918bc9c + 3ca7097 commit 4a052e4
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
69 changes: 69 additions & 0 deletions library/Zend/Form/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ public function configureFieldset(FieldsetInterface $fieldset, $spec)
$this->prepareAndInjectFieldsets($spec['fieldsets'], $fieldset, __METHOD__);
}

$factory = (isset($spec['factory']) ? $spec['factory'] : $this);
$this->prepareAndInjectFactory($factory, $fieldset, __METHOD__);

return $fieldset;
}

Expand Down Expand Up @@ -450,6 +453,46 @@ protected function prepareAndInjectHydrator($hydratorOrName, FieldsetInterface $
$fieldset->setHydrator($hydrator);
}

/**
* Prepare and inject a named factory
*
* Takes a string indicating a factory class name (or a concrete instance), try first to instantiates the class
* by pulling it from service manager, and injects the factory instance into the fieldset.
*
* @param string|array|Factory $factoryOrName
* @param FieldsetInterface $fieldset
* @param string $method
* @return void
* @throws Exception\DomainException If $factoryOrName is not a string, does not resolve to a known class, or
* the class does not extend Form\Factory
*/
protected function prepareAndInjectFactory($factoryOrName, FieldsetInterface $fieldset, $method)
{
if (is_array($factoryOrName)) {
if (!isset($factoryOrName['type'])) {
throw new Exception\DomainException(sprintf(
'%s expects array specification to have a type value',
$method
));
}
$factoryOrName = $factoryOrName['type'];
}

if (is_string($factoryOrName)) {
$factoryOrName = $this->getFactoryFromName($factoryOrName);
}

if (!$factoryOrName instanceof Factory) {
throw new Exception\DomainException(sprintf(
'%s expects a valid extention of Zend\Form\Factory; received "%s"',
$method,
$factoryOrName
));
}

$fieldset->setFormFactory($factoryOrName);
}

/**
* Prepare an input filter instance and inject in the provided form
*
Expand Down Expand Up @@ -559,4 +602,30 @@ protected function getHydratorFromName($hydratorName)
$hydrator = new $hydratorName;
return $hydrator;
}

/**
* Try to pull factory from service manager, or instantiates it from its name
*
* @param string $factoryName
* @return mixed
* @throws Exception\DomainException
*/
protected function getFactoryFromName($factoryName)
{
$services = $this->getFormElementManager()->getServiceLocator();

if ($services && $services->has($factoryName)) {
return $services->get($factoryName);
}

if (!class_exists($factoryName)) {
throw new Exception\DomainException(sprintf(
'Expects string factory name to be a valid class name; received "%s"',
$factoryName
));
}

$factory = new $factoryName;
return $factory;
}
}
93 changes: 93 additions & 0 deletions tests/ZendTest/Form/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,99 @@ public function testCanCreateHydratorFromConcreteClass()
$this->assertInstanceOf('Zend\Stdlib\Hydrator\ObjectProperty', $hydrator);
}

public function testCanCreateFormsAndSpecifyFactory()
{
$form = $this->factory->createForm(array(
'name' => 'foo',
'factory' => 'Zend\Form\Factory',
));
$this->assertInstanceOf('Zend\Form\FormInterface', $form);
$factory = $form->getFormFactory();
$this->assertInstanceOf('Zend\Form\Factory', $factory);
}

public function testCanCreateFactoryFromArray()
{
$form = $this->factory->createForm(array(
'name' => 'foo',
'factory' => array(
'type' => 'Zend\Form\Factory',
),
));

$this->assertInstanceOf('Zend\Form\FormInterface', $form);
$factory = $form->getFormFactory();
$this->assertInstanceOf('Zend\Form\Factory', $factory);
}

public function testCanCreateFactoryFromConcreteClass()
{
$factory = new \Zend\Form\Factory();
$form = $this->factory->createForm(array(
'name' => 'foo',
'factory' => $factory,
));

$this->assertInstanceOf('Zend\Form\FormInterface', $form);
$test = $form->getFormFactory();
$this->assertSame($factory, $test);
}

public function testCanCreateFormFromConcreteClassAndSpecifyCustomValidatorByName()
{
$validatorManager = new \Zend\Validator\ValidatorPluginManager();
$validatorManager->setInvokableClass('baz', 'ZendTest\Validator\TestAsset\ConcreteValidator');

$defaultValidatorChain = new \Zend\Validator\ValidatorChain();
$defaultValidatorChain->setPluginManager($validatorManager);

$inputFilterFactory = new \Zend\InputFilter\Factory();
$inputFilterFactory->setDefaultValidatorChain($defaultValidatorChain);

$factory = new FormFactory();
$factory->setInputFilterFactory($inputFilterFactory);

$form = $factory->createForm(array(
'name' => 'foo',
'factory' => $factory,
'input_filter' => array(
'bar' => array(
'name' => 'bar',
'required' => true,
'validators' => array(
array(
'name' => 'baz',
),
),
),
),
));

$this->assertInstanceOf('Zend\Form\FormInterface', $form);

$inputFilter = $form->getInputFilter();
$this->assertInstanceOf('Zend\InputFilter\InputFilterInterface', $inputFilter);

$input = $inputFilter->get('bar');
$this->assertInstanceOf('Zend\InputFilter\Input', $input);

$validatorChain = $input->getValidatorChain();
$this->assertInstanceOf('Zend\Validator\ValidatorChain', $validatorChain);

$validatorArray = $validatorChain->getValidators();
$found = false;
foreach($validatorArray as $validator) {
$validatorInstance = $validator['instance'];
$this->assertInstanceOf('Zend\Validator\ValidatorInterface', $validatorInstance);

if($validatorInstance instanceof \ZendTest\Validator\TestAsset\ConcreteValidator) {
$found = true;
break;
}
}
$this->assertTrue($found);
}

public function testCanCreateFormWithHydratorAndInputFilterAndElementsAndFieldsets()
{
$form = $this->factory->createForm(array(
Expand Down

0 comments on commit 4a052e4

Please sign in to comment.