/vendor/zendframework/zend-form/src/FormElementManager.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 193 lines · 109 code · 17 blank · 67 comment · 14 complexity · bcc7eef9a0ce0a491bb98990454cbc60 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Form;
  10. use Zend\ServiceManager\AbstractPluginManager;
  11. use Zend\ServiceManager\ConfigInterface;
  12. use Zend\ServiceManager\Exception\ServiceNotCreatedException;
  13. use Zend\ServiceManager\ServiceLocatorInterface;
  14. use Zend\Stdlib\InitializableInterface;
  15. /**
  16. * Plugin manager implementation for form elements.
  17. *
  18. * Enforces that elements retrieved are instances of ElementInterface.
  19. */
  20. class FormElementManager extends AbstractPluginManager
  21. {
  22. /**
  23. * Default set of helpers
  24. *
  25. * @var array
  26. */
  27. protected $invokableClasses = array(
  28. 'button' => 'Zend\Form\Element\Button',
  29. 'captcha' => 'Zend\Form\Element\Captcha',
  30. 'checkbox' => 'Zend\Form\Element\Checkbox',
  31. 'collection' => 'Zend\Form\Element\Collection',
  32. 'color' => 'Zend\Form\Element\Color',
  33. 'csrf' => 'Zend\Form\Element\Csrf',
  34. 'date' => 'Zend\Form\Element\Date',
  35. 'dateselect' => 'Zend\Form\Element\DateSelect',
  36. 'datetime' => 'Zend\Form\Element\DateTime',
  37. 'datetimelocal' => 'Zend\Form\Element\DateTimeLocal',
  38. 'datetimeselect' => 'Zend\Form\Element\DateTimeSelect',
  39. 'element' => 'Zend\Form\Element',
  40. 'email' => 'Zend\Form\Element\Email',
  41. 'fieldset' => 'Zend\Form\Fieldset',
  42. 'file' => 'Zend\Form\Element\File',
  43. 'form' => 'Zend\Form\Form',
  44. 'hidden' => 'Zend\Form\Element\Hidden',
  45. 'image' => 'Zend\Form\Element\Image',
  46. 'month' => 'Zend\Form\Element\Month',
  47. 'monthselect' => 'Zend\Form\Element\MonthSelect',
  48. 'multicheckbox' => 'Zend\Form\Element\MultiCheckbox',
  49. 'number' => 'Zend\Form\Element\Number',
  50. 'password' => 'Zend\Form\Element\Password',
  51. 'radio' => 'Zend\Form\Element\Radio',
  52. 'range' => 'Zend\Form\Element\Range',
  53. 'select' => 'Zend\Form\Element\Select',
  54. 'submit' => 'Zend\Form\Element\Submit',
  55. 'text' => 'Zend\Form\Element\Text',
  56. 'textarea' => 'Zend\Form\Element\Textarea',
  57. 'time' => 'Zend\Form\Element\Time',
  58. 'url' => 'Zend\Form\Element\Url',
  59. 'week' => 'Zend\Form\Element\Week',
  60. );
  61. /**
  62. * Don't share form elements by default
  63. *
  64. * @var bool
  65. */
  66. protected $shareByDefault = false;
  67. /**
  68. * @param ConfigInterface $configuration
  69. */
  70. public function __construct(ConfigInterface $configuration = null)
  71. {
  72. parent::__construct($configuration);
  73. $this->addInitializer(array($this, 'injectFactory'));
  74. $this->addInitializer(array($this, 'callElementInit'), false);
  75. }
  76. /**
  77. * Inject the factory to any element that implements FormFactoryAwareInterface
  78. *
  79. * @param $element
  80. */
  81. public function injectFactory($element)
  82. {
  83. if ($element instanceof FormFactoryAwareInterface) {
  84. $factory = $element->getFormFactory();
  85. $factory->setFormElementManager($this);
  86. if ($this->serviceLocator instanceof ServiceLocatorInterface
  87. && $this->serviceLocator->has('InputFilterManager')
  88. ) {
  89. $inputFilters = $this->serviceLocator->get('InputFilterManager');
  90. $factory->getInputFilterFactory()->setInputFilterManager($inputFilters);
  91. }
  92. }
  93. }
  94. /**
  95. * Call init() on any element that implements InitializableInterface
  96. *
  97. * @internal param $element
  98. */
  99. public function callElementInit($element)
  100. {
  101. if ($element instanceof InitializableInterface) {
  102. $element->init();
  103. }
  104. }
  105. /**
  106. * Validate the plugin
  107. *
  108. * Checks that the element is an instance of ElementInterface
  109. *
  110. * @param mixed $plugin
  111. * @throws Exception\InvalidElementException
  112. * @return void
  113. */
  114. public function validatePlugin($plugin)
  115. {
  116. if ($plugin instanceof ElementInterface) {
  117. return; // we're okay
  118. }
  119. throw new Exception\InvalidElementException(sprintf(
  120. 'Plugin of type %s is invalid; must implement Zend\Form\ElementInterface',
  121. (is_object($plugin) ? get_class($plugin) : gettype($plugin))
  122. ));
  123. }
  124. /**
  125. * Retrieve a service from the manager by name
  126. *
  127. * Allows passing an array of options to use when creating the instance.
  128. * createFromInvokable() will use these and pass them to the instance
  129. * constructor if not null and a non-empty array.
  130. *
  131. * @param string $name
  132. * @param string|array $options
  133. * @param bool $usePeeringServiceManagers
  134. * @return object
  135. */
  136. public function get($name, $options = array(), $usePeeringServiceManagers = true)
  137. {
  138. if (is_string($options)) {
  139. $options = array('name' => $options);
  140. }
  141. return parent::get($name, $options, $usePeeringServiceManagers);
  142. }
  143. /**
  144. * Attempt to create an instance via an invokable class
  145. *
  146. * Overrides parent implementation by passing $creationOptions to the
  147. * constructor, if non-null.
  148. *
  149. * @param string $canonicalName
  150. * @param string $requestedName
  151. * @return null|\stdClass
  152. * @throws ServiceNotCreatedException If resolved class does not exist
  153. */
  154. protected function createFromInvokable($canonicalName, $requestedName)
  155. {
  156. $invokable = $this->invokableClasses[$canonicalName];
  157. if (null === $this->creationOptions
  158. || (is_array($this->creationOptions) && empty($this->creationOptions))
  159. ) {
  160. $instance = new $invokable();
  161. } else {
  162. if (isset($this->creationOptions['name'])) {
  163. $name = $this->creationOptions['name'];
  164. } else {
  165. $name = $requestedName;
  166. }
  167. if (isset($this->creationOptions['options'])) {
  168. $options = $this->creationOptions['options'];
  169. } else {
  170. $options = $this->creationOptions;
  171. }
  172. $instance = new $invokable($name, $options);
  173. }
  174. return $instance;
  175. }
  176. }