[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/zendframework/zend-stdlib/src/Hydrator/Filter/ -> FilterComposite.php (source)

   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\Stdlib\Hydrator\Filter;
  10  
  11  use ArrayObject;
  12  use Zend\Stdlib\Exception\InvalidArgumentException;
  13  
  14  class FilterComposite implements FilterInterface
  15  {
  16      /**
  17       * @var ArrayObject
  18       */
  19      protected $orFilter;
  20  
  21      /**
  22       * @var ArrayObject
  23       */
  24      protected $andFilter;
  25  
  26      /**
  27       * Constant to add with "or" conditition
  28       */
  29      const CONDITION_OR = 1;
  30  
  31      /**
  32       * Constant to add with "and" conditition
  33       */
  34      const CONDITION_AND = 2;
  35  
  36      /**
  37       * Define default Filter
  38       *
  39       * @param  array $orFilter
  40       * @param  array $andFilter
  41       * @throws InvalidArgumentException
  42       */
  43      public function __construct($orFilter = array(), $andFilter = array())
  44      {
  45          array_walk(
  46              $orFilter,
  47              function ($value, $key) {
  48                  if (!is_callable($value) && !$value instanceof FilterInterface) {
  49                      throw new InvalidArgumentException(
  50                          'The value of ' . $key . ' should be either a callable or ' .
  51                          'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface'
  52                      );
  53                  }
  54              }
  55          );
  56  
  57          array_walk(
  58              $andFilter,
  59              function ($value, $key) {
  60                  if (!is_callable($value) && !$value instanceof FilterInterface) {
  61                      throw new InvalidArgumentException(
  62                          'The value of ' . $key . '  should be either a callable or ' .
  63                          'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface'
  64                      );
  65                  }
  66              }
  67          );
  68  
  69          $this->orFilter = new ArrayObject($orFilter);
  70          $this->andFilter = new ArrayObject($andFilter);
  71      }
  72  
  73      /**
  74       * Add a filter to the composite. Has to be indexed with $name in
  75       * order to identify a specific filter.
  76       *
  77       * This example will exclude all methods from the hydration, that starts with 'getService'
  78       * <code>
  79       * $composite->addFilter('exclude',
  80       *     function ($method) {
  81       *         if (preg_match('/^getService/', $method) {
  82       *             return false;
  83       *         }
  84       *         return true;
  85       *     }, FilterComposite::CONDITION_AND
  86       * );
  87       * </code>
  88       *
  89       * @param  string                   $name
  90       * @param  callable|FilterInterface $filter
  91       * @param  int                      $condition Can be either FilterComposite::CONDITION_OR or FilterComposite::CONDITION_AND
  92       * @throws InvalidArgumentException
  93       * @return FilterComposite
  94       */
  95      public function addFilter($name, $filter, $condition = self::CONDITION_OR)
  96      {
  97          if (!is_callable($filter) && !($filter instanceof FilterInterface)) {
  98              throw new InvalidArgumentException(
  99                  'The value of ' . $name . ' should be either a callable or ' .
 100                  'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface'
 101              );
 102          }
 103  
 104          if ($condition === self::CONDITION_OR) {
 105              $this->orFilter[$name] = $filter;
 106          } elseif ($condition === self::CONDITION_AND) {
 107              $this->andFilter[$name] = $filter;
 108          }
 109  
 110          return $this;
 111      }
 112  
 113      /**
 114       * Remove a filter from the composition
 115       *
 116       * @param $name string Identifier for the filter
 117       * @return FilterComposite
 118       */
 119      public function removeFilter($name)
 120      {
 121          if (isset($this->orFilter[$name])) {
 122              unset($this->orFilter[$name]);
 123          }
 124  
 125          if (isset($this->andFilter[$name])) {
 126              unset($this->andFilter[$name]);
 127          }
 128  
 129          return $this;
 130      }
 131  
 132      /**
 133       * Check if $name has a filter registered
 134       *
 135       * @param $name string Identifier for the filter
 136       * @return bool
 137       */
 138      public function hasFilter($name)
 139      {
 140          return isset($this->orFilter[$name]) || isset($this->andFilter[$name]);
 141      }
 142  
 143      /**
 144       * Filter the composite based on the AND and OR condition
 145       * Will return true if one from the "or conditions" and all from
 146       * the "and condition" returns true. Otherwise false
 147       *
 148       * @param $property string Parameter will be e.g. Parent\Namespace\Class::method
 149       * @return bool
 150       */
 151      public function filter($property)
 152      {
 153          $andCount = count($this->andFilter);
 154          $orCount = count($this->orFilter);
 155          // return true if no filters are registered
 156          if ($orCount === 0 && $andCount === 0) {
 157              return true;
 158          } elseif ($orCount === 0 && $andCount !== 0) {
 159              $returnValue = true;
 160          } else {
 161              $returnValue = false;
 162          }
 163  
 164          // Check if 1 from the or filters return true
 165          foreach ($this->orFilter as $filter) {
 166              if (is_callable($filter)) {
 167                  if ($filter($property) === true) {
 168                      $returnValue = true;
 169                      break;
 170                  }
 171                  continue;
 172              } else {
 173                  if ($filter->filter($property) === true) {
 174                      $returnValue = true;
 175                      break;
 176                  }
 177              }
 178          }
 179  
 180          // Check if all of the and condition return true
 181          foreach ($this->andFilter as $filter) {
 182              if (is_callable($filter)) {
 183                  if ($filter($property) === false) {
 184                      return false;
 185                  }
 186                  continue;
 187              } else {
 188                  if ($filter->filter($property) === false) {
 189                      return false;
 190                  }
 191              }
 192          }
 193  
 194          return $returnValue;
 195      }
 196  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1