Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FilterManager.php
Go to the documentation of this file.
1 <?php
7 
32 {
36  protected $objectManager;
37 
41  protected $config;
42 
46  protected $factoryInstances;
47 
52  public function __construct(\Magento\Framework\ObjectManagerInterface $objectManger, FilterManager\Config $config)
53  {
54  $this->objectManager = $objectManger;
55  $this->config = $config;
56  }
57 
66  public function get($filterAlias, array $arguments = [])
67  {
68  $filter = $this->createFilterInstance($filterAlias, $arguments);
69  if (!$filter instanceof \Zend_Filter_Interface) {
70  throw new \UnexpectedValueException(
71  'Filter object must implement Zend_Filter_Interface interface, ' . get_class($filter) . ' was given.'
72  );
73  }
74  return $filter;
75  }
76 
85  protected function createFilterInstance($filterAlias, $arguments)
86  {
88  foreach ($this->getFilterFactories() as $factory) {
89  if ($factory->canCreateFilter($filterAlias)) {
90  return $factory->createFilter($filterAlias, $arguments);
91  }
92  }
93  throw new \InvalidArgumentException('Filter was not found by given alias ' . $filterAlias);
94  }
95 
102  protected function getFilterFactories()
103  {
104  if (null === $this->factoryInstances) {
105  foreach ($this->config->getFactories() as $class) {
106  $factory = $this->objectManager->create($class);
107  if (!$factory instanceof FactoryInterface) {
108  throw new \UnexpectedValueException(
109  'Filter factory must implement FilterFactoryInterface interface, ' . get_class(
110  $factory
111  ) . ' was given.'
112  );
113  }
114  $this->factoryInstances[] = $factory;
115  }
116  }
118  }
119 
127  public function __call($filterAlias, array $arguments = [])
128  {
129  $value = array_shift($arguments);
130  if (count($arguments)) {
131  $arguments = array_shift($arguments);
132  if (!is_array($arguments)) {
134  }
135  }
136  return $this->createFilterInstance($filterAlias, $arguments)->filter($value);
137  }
138 }
__call($filterAlias, array $arguments=[])
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
$arguments
__construct(\Magento\Framework\ObjectManagerInterface $objectManger, FilterManager\Config $config)