Skip to content

Instantly share code, notes, and snippets.

@bakura10 bakura10/test.php
Created May 21, 2014

Embed
What would you like to do?
class UserInputFilterFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $service)
{
$factory = // Get from SL;
$inputCollection = new InputCollection('user');
$input = $factory->createInput([
'name' => 'foo',
'validators' => []
]);
$inputCollection->addInput($input);
}
}
@texdc

This comment has been minimized.

Copy link

texdc commented May 27, 2014

First of all, let me reiterate my hatred for the term InputFilter. :)

Any component that allows adding other components should have a factory injected in the constructor.

namespace Zend\Input;

use Zend\Stdlib\PriorityQueue;

class Collection implements InputInterface
{
    private $factory;

    private $name;

    private $inputs;

    public function __construct($name, FactoryInterface $factory, array $inputSpecs = [])
    {
        $this->name    = trim($name);
        $this->factory = $factory;
        $this->inputs  = new PriorityQueue;
        $this->addInputSpecs($inputSpecs);
    }

    public function addInput(InputInterface $input, $priority = 0)
    {
        $this->inputs->append($input, $priority);
    }

    public function addInputSpec(array $spec)
    {
        $priority = isset($spec['priority']) ? $spec['priority'] : 0;
        $this->addInput($this->factory->build($spec), $priority);
    }

    public function addInputSpecs(array $specs)
    {
        foreach ($specs as $spec) {
            $this->addInputSpec($spec);
        }
    }
}

then…

namespace My\User\Serivce;

use Zend\Input\Collection;
use Zend\Service\FactoryInterface;
use Zend\Service\LocatorInterface;

class InputCollectionFactory implements FactoryInterface
{
    public function createService(LocatorInterface $locator)
    {
        $factory = $locator->get('Zend\Input\FactoryInterface');
        $specs   = $locator->get('my.user.input');
        return new Collection('user', $factory, $specs);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.