Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
FilterComposite.php
001 <?php
002 /**
003 * Zend Framework (http://framework.zend.com/)
004 *
005 * @link http://github.com/zendframework/zf2 for the canonical source repository
006 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
007 * @license http://framework.zend.com/license/new-bsd New BSD License
008 */
009 namespace Zend\Stdlib\Hydrator\Filter;
010
011 use ArrayObject;
012 use Zend\Stdlib\Exception\InvalidArgumentException;
013
014 class FilterComposite implements FilterInterface
015 {
016 /**
017 * @var ArrayObject
018 */
019 protected $orFilter;
020
021 /**
022 * @var ArrayObject
023 */
024 protected $andFilter;
025
026 /**
027 * Constant to add with "or" conditition
028 */
029 const CONDITION_OR = 1;
030
031 /**
032 * Constant to add with "and" conditition
033 */
034 const CONDITION_AND = 2;
035
036 /**
037 * Define default Filter
038 *
039 * @param array $orFilter
040 * @param array $andFilter
041 * @throws InvalidArgumentException
042 */
043 public function __construct($orFilter = array(), $andFilter = array())
044 {
045 array_walk(
046 $orFilter,
047 function ($value, $key) {
048 if (!is_callable($value) && !$value instanceof FilterInterface) {
049 throw new InvalidArgumentException(
050 'The value of ' . $key . ' should be either a callable or ' .
051 'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface'
052 );
053 }
054 }
055 );
056
057 array_walk(
058 $andFilter,
059 function ($value, $key) {
060 if (!is_callable($value) && !$value instanceof FilterInterface) {
061 throw new InvalidArgumentException(
062 'The value of ' . $key . ' should be either a callable or ' .
063 'an instance of Zend\Stdlib\Hydrator\Filter\FilterInterface'
064 );
065 }
066 }
067 );
068
069 $this->orFilter = new ArrayObject($orFilter);
070 $this->andFilter = new ArrayObject($andFilter);
071 }
072
073 /**
074 * Add a filter to the composite. Has to be indexed with $name in
075 * order to identify a specific filter.
076 *
077 * This example will exclude all methods from the hydration, that starts with 'getService'
078 * <code>
079 * $composite->addFilter('exclude',
080 * function ($method) {
081 * if (preg_match('/^getService/', $method) {
082 * return false;
083 * }
084 * return true;
085 * }, FilterComposite::CONDITION_AND
086 * );
087 * </code>
088 *
089 * @param string $name
090 * @param callable|FilterInterface $filter
091 * @param int $condition Can be either FilterComposite::CONDITION_OR or FilterComposite::CONDITION_AND
092 * @throws InvalidArgumentException
093 * @return FilterComposite
094 */
095 public function addFilter($name, $filter, $condition = self::CONDITION_OR)
096 {
097 if (!is_callable($filter) && !($filter instanceof FilterInterface)) {
098 throw new InvalidArgumentException(
099 '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 }
197