lightsaml2 /
lightSAML
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the LightSAML-Core package. |
||
| 5 | * |
||
| 6 | * (c) Milos Tomic <[email protected]> |
||
| 7 | * |
||
| 8 | * This source file is subject to the MIT license that is bundled |
||
| 9 | * with this source code in the file LICENSE. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace LightSaml\Criteria; |
||
| 13 | |||
| 14 | class CriteriaSet |
||
| 15 | { |
||
| 16 | /** @var array|CriteriaInterface[] */ |
||
| 17 | protected $criterions = []; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @param CriteriaInterface[] $criterions |
||
| 21 | */ |
||
| 22 | public function __construct(array $criterions = []) |
||
| 23 | { |
||
| 24 | foreach ($criterions as $criterion) { |
||
| 25 | $this->add($criterion); |
||
| 26 | } |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @return CriteriaSet |
||
| 31 | */ |
||
| 32 | public function add(CriteriaInterface $criteria) |
||
| 33 | { |
||
| 34 | $this->criterions[] = $criteria; |
||
| 35 | |||
| 36 | return $this; |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @return CriteriaSet |
||
| 41 | */ |
||
| 42 | public function addIfNone(CriteriaInterface $criteria) |
||
| 43 | { |
||
| 44 | if (false == $this->has(get_class($criteria))) { |
||
|
0 ignored issues
–
show
|
|||
| 45 | $this->add($criteria); |
||
| 46 | } |
||
| 47 | |||
| 48 | return $this; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @return CriteriaSet |
||
| 53 | */ |
||
| 54 | public function addAll(CriteriaSet $criteriaSet) |
||
| 55 | { |
||
| 56 | foreach ($criteriaSet->all() as $criteria) { |
||
| 57 | $this->add($criteria); |
||
| 58 | } |
||
| 59 | |||
| 60 | return $this; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param mixed $condition |
||
| 65 | * @param callable $callback |
||
| 66 | * |
||
| 67 | * @return CriteriaSet |
||
| 68 | */ |
||
| 69 | public function addIf($condition, $callback) |
||
| 70 | { |
||
| 71 | if ($condition) { |
||
| 72 | $criteria = call_user_func($callback); |
||
| 73 | if ($criteria) { |
||
| 74 | $this->add($criteria); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | return $this; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return CriteriaInterface[]|array |
||
| 83 | */ |
||
| 84 | public function all() |
||
| 85 | { |
||
| 86 | return $this->criterions; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param string $class |
||
| 91 | * |
||
| 92 | * @return array|CriteriaInterface[] |
||
| 93 | */ |
||
| 94 | public function get($class) |
||
| 95 | { |
||
| 96 | $result = []; |
||
| 97 | foreach ($this->criterions as $criteria) { |
||
| 98 | if ($criteria instanceof $class) { |
||
| 99 | $result[] = $criteria; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | return $result; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $class |
||
| 108 | * |
||
| 109 | * @return CriteriaInterface|null |
||
| 110 | */ |
||
| 111 | public function getSingle($class) |
||
| 112 | { |
||
| 113 | foreach ($this->criterions as $criteria) { |
||
| 114 | if ($criteria instanceof $class) { |
||
| 115 | return $criteria; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | return null; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $class |
||
| 124 | * |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | public function has($class) |
||
| 128 | { |
||
| 129 | foreach ($this->criterions as $criteria) { |
||
| 130 | if ($criteria instanceof $class) { |
||
| 131 | return true; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | return false; |
||
| 136 | } |
||
| 137 | } |
||
| 138 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.