Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Pass options to factories as creation options #45

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ public function create($spec)
$spec = $this->validateSpecification($spec, __METHOD__);
$type = isset($spec['type']) ? $spec['type'] : 'Zend\Form\Element';

$element = $this->getFormElementManager()->get($type);
$creationOptions = [];
if (isset($spec['options']) && is_array($spec['options'])) {
$creationOptions = $spec['options'];
}

$element = $this->getFormElementManager()->get($type, $creationOptions);

if ($element instanceof FormInterface) {
return $this->configureForm($element, $spec);
Expand Down
30 changes: 29 additions & 1 deletion test/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
namespace ZendTest\Form;

use PHPUnit_Framework_TestCase as TestCase;
use DateTime;
use Zend\Filter;
use Zend\Form;
use Zend\Form\FormElementManager;
use Zend\Form\Factory as FormFactory;
use Zend\Form\FormElementManager;
use Zend\Form\FormInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\Hydrator\HydratorPluginManager;

Expand Down Expand Up @@ -754,4 +756,30 @@ public function testCanCreateFormWithNullElements()
$this->assertFalse($form->has('baz'));
$this->assertTrue($form->has('bat'));
}

public function testOptionsArePassedAsCreationOptionsToFactories()
{
$formManager = $this->factory->getFormElementManager();
$formManager->setFactory('customCreatedForm', TestAsset\CustomCreatedFormFactory::class);

/* @var $form TestAsset\CustomCreatedForm */
$form = $this->factory->create([
'name' => 'some_name',
'type' => 'customCreatedForm',
'options' => [
'created' => '2016-02-19',
'some_other_option' => 1234
]
]);

$this->assertInstanceOf(FormInterface::class, $form);
$this->assertInstanceOf(TestAsset\CustomCreatedForm::class, $form);
$this->assertSame('some_name', $form->getName());
$this->assertSame(1234, $form->getOption('some_other_option'));

/* @var $created DateTime */
$created = $form->getCreated();
$this->assertInstanceOf(DateTime::class, $created);
$this->assertSame('2016-02-19', $created->format('Y-m-d'));
}
}
30 changes: 30 additions & 0 deletions test/TestAsset/CustomCreatedForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Form\TestAsset;

use Zend\Form\Form;
use DateTime;

class CustomCreatedForm extends Form
{
private $created;

public function __construct(DateTime $created, $name = null, $options = array())
{
parent::__construct($name, $options);
$this->created = $created;
}

public function getCreated()
{
return $this->created;
}
}
47 changes: 47 additions & 0 deletions test/TestAsset/CustomCreatedFormFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Form\TestAsset;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\MutableCreationOptionsInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use DateTime;

class CustomCreatedFormFactory implements FactoryInterface, MutableCreationOptionsInterface
{
private $creationOptions = [];

public function createService(ServiceLocatorInterface $serviceLocator)
{
$creationString = 'now';
if (isset($this->creationOptions['created'])) {
$creationString = $this->creationOptions['created'];
unset($this->creationOptions['created']);
}

$created = new DateTime($creationString);

$name = null;
if (isset($this->creationOptions['name'])) {
$name = $this->creationOptions['name'];
unset($this->creationOptions['name']);
}

$options = $this->creationOptions;

$form = new CustomCreatedForm($created, $name, $options);
return $form;
}

public function setCreationOptions(array $options)
{
$this->creationOptions = $options;
}
}