Skip to content

Instantly share code, notes, and snippets.

@bakura10
Created May 21, 2014 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakura10/6b9c4e8d89b518375140 to your computer and use it in GitHub Desktop.
Save bakura10/6b9c4e8d89b518375140 to your computer and use it in GitHub Desktop.
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
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