| @@ 17-46 (lines=30) @@ | ||
| 14 | /** |
|
| 15 | * A predicate that is true if all of its subpredicates are true. |
|
| 16 | */ |
|
| 17 | class _And extends _Combined { |
|
| 18 | /** |
|
| 19 | * @inheritdocs |
|
| 20 | */ |
|
| 21 | public function compile() { |
|
| 22 | $compiled = array_map(function($p) { |
|
| 23 | return $p->compile(); |
|
| 24 | }, $this->predicates); |
|
| 25 | ||
| 26 | return function(Entity $e) use ($compiled) { |
|
| 27 | foreach ($compiled as $predicate) { |
|
| 28 | if (!$predicate($e)) { |
|
| 29 | return false; |
|
| 30 | } |
|
| 31 | } |
|
| 32 | return true; |
|
| 33 | }; |
|
| 34 | } |
|
| 35 | ||
| 36 | /** |
|
| 37 | * @inheritdocs |
|
| 38 | */ |
|
| 39 | public function for_types($existing_types) { |
|
| 40 | $tss = array_map(function($p) use ($existing_types) { |
|
| 41 | return $p->for_types($existing_types); |
|
| 42 | }, $this->predicates); |
|
| 43 | ||
| 44 | return array_values(call_user_func_array("array_intersect", $tss)); |
|
| 45 | } |
|
| 46 | } |
|
| 47 | ||
| @@ 18-47 (lines=30) @@ | ||
| 15 | /** |
|
| 16 | * A predicate that is true if any of its subpredicates are true. |
|
| 17 | */ |
|
| 18 | class _Or extends _Combined { |
|
| 19 | /** |
|
| 20 | * @inheritdocs |
|
| 21 | */ |
|
| 22 | public function compile() { |
|
| 23 | $compiled = array_map(function($p) { |
|
| 24 | return $p->compile(); |
|
| 25 | }, $this->predicates); |
|
| 26 | ||
| 27 | return function(Entity $e) use ($compiled) { |
|
| 28 | foreach ($compiled as $predicate) { |
|
| 29 | if ($predicate($e)) { |
|
| 30 | return true; |
|
| 31 | } |
|
| 32 | } |
|
| 33 | return false; |
|
| 34 | }; |
|
| 35 | } |
|
| 36 | ||
| 37 | /** |
|
| 38 | * @inheritdocs |
|
| 39 | */ |
|
| 40 | public function for_types($existing_types) { |
|
| 41 | $tss = array_map(function($p) use ($existing_types) { |
|
| 42 | return $p->for_types($existing_types); |
|
| 43 | }, $this->predicates); |
|
| 44 | ||
| 45 | return array_values(array_unique(call_user_func_array("array_merge", $tss))); |
|
| 46 | } |
|
| 47 | } |
|
| 48 | ||