1: <?php
2:
3: namespace Charcoal\Ui\FormInput;
4:
5: use \Pimple\Container;
6:
7: // From 'charcoal-factory'
8: use \Charcoal\Factory\FactoryInterface;
9:
10: /**
11: * Form Input Builder
12: */
13: class FormInputBuilder
14: {
15: /**
16: * The default, concrete, form input model.
17: *
18: * @const string
19: */
20: const DEFAULT_TYPE = 'charcoal/ui/form-input/generic';
21:
22: /**
23: * Store the form input factory instance.
24: *
25: * @var FactoryInterface
26: */
27: protected $factory;
28:
29: /**
30: * Store the dependency-injection container to fulfill the required services.
31: *
32: * @var Container $container
33: */
34: protected $container;
35:
36: /**
37: * Return a new form input builder.
38: *
39: * @param FactoryInterface $factory A dashboard factory.
40: * @param Container $container The DI container.
41: */
42: public function __construct(FactoryInterface $factory, Container $container)
43: {
44: $this->factory = $factory;
45: $this->container = $container;
46: }
47:
48: /**
49: * Build and return a new form input.
50: *
51: * @param array|\ArrayAccess $options The form input build options.
52: * @return FormInputInterface
53: */
54: public function build($options)
55: {
56: $container = $this->container;
57: $objType = isset($options['type']) ? $options['type'] : self::DEFAULT_TYPE;
58:
59: $obj = $this->factory->create($objType, [
60: 'logger' => $container['logger'],
61: 'view' => $container['view']
62: ]);
63: $obj->setData($options);
64:
65: return $obj;
66: }
67: }
68: