Pagekit\Filter\FilterManager::get PHP Метод

get() публичный Метод

Gets a filter.
public get ( string $name, array $options = [] ) : Pagekit\Filter\FilterInterface
$name string
$options array
Результат Pagekit\Filter\FilterInterface The filter
    public function get($name, array $options = [])
    {
        if (array_key_exists($name, $this->defaults)) {
            $this->filters[$name] = $this->defaults[$name];
        }
        if (!array_key_exists($name, $this->filters)) {
            throw new \InvalidArgumentException(sprintf('Filter "%s" is not defined.', $name));
        }
        if (is_string($class = $this->filters[$name])) {
            $this->filters[$name] = new $class();
        }
        $filter = clone $this->filters[$name];
        $filter->setOptions($options);
        return $filter;
    }

Usage Example

Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function get($index)
 {
     if (!isset($this->params[$index])) {
         return null;
     }
     /**
      * @var string $name
      * @var string $type
      * @var array  $options
      */
     extract($this->params[$index]);
     foreach (['query', 'request'] as $bag) {
         $value = $this->request->{$bag}->get($name);
         if ($value !== null) {
             if ($type == 'array') {
                 $value = (array) $value;
             } elseif (strpos($type, '[]') !== false) {
                 $value = (array) $value;
                 $filter = $this->filterManager->get(str_replace('[]', '', $type), $options);
                 array_walk($value, function (&$val) use($filter, $name) {
                     if (is_array($val)) {
                         throw new \RuntimeException(sprintf("Query parameter cannot be a nested array.", $name));
                     }
                     $val = $filter->filter($val);
                 });
             } elseif ($type) {
                 $value = $this->filterManager->get($type, $options)->filter($value);
             }
             break;
         }
     }
     return $value;
 }