1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package WPEmerge |
4
|
|
|
* @author Atanas Angelov <[email protected]> |
5
|
|
|
* @copyright 2018 Atanas Angelov |
6
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
7
|
|
|
* @link https://wpemerge.com/ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace WPEmerge\Routing\Conditions; |
11
|
|
|
|
12
|
|
|
use WPEmerge\Facades\RouteCondition; |
13
|
|
|
use WPEmerge\Requests\RequestInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Check against an array of conditions in an AND logical relationship. |
17
|
|
|
*/ |
18
|
|
|
class MultipleCondition implements ConditionInterface, HasUrlInterface { |
19
|
|
|
/** |
20
|
|
|
* Array of conditions to check. |
21
|
|
|
* |
22
|
|
|
* @var array<ConditionInterface> |
23
|
|
|
*/ |
24
|
|
|
protected $conditions = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor. |
28
|
|
|
* |
29
|
|
|
* @param array $conditions |
30
|
|
|
*/ |
31
|
|
|
public function __construct( $conditions ) { |
32
|
1 |
|
$this->conditions = array_map( function ( $condition ) { |
33
|
1 |
|
if ( $condition instanceof ConditionInterface ) { |
34
|
1 |
|
return $condition; |
35
|
|
|
} |
36
|
1 |
|
return RouteCondition::make( $condition ); |
37
|
1 |
|
}, $conditions ); |
38
|
1 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
1 |
|
public function isSatisfied( RequestInterface $request ) { |
44
|
1 |
|
foreach ( $this->conditions as $condition ) { |
45
|
1 |
|
if ( ! $condition->isSatisfied( $request ) ) { |
46
|
1 |
|
return false; |
47
|
|
|
} |
48
|
|
|
} |
49
|
1 |
|
return true; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function getArguments( RequestInterface $request ) { |
56
|
1 |
|
$arguments = []; |
57
|
|
|
|
58
|
1 |
|
foreach ( $this->conditions as $condition ) { |
59
|
1 |
|
$arguments = array_merge( $arguments, $condition->getArguments( $request ) ); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return $arguments; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get all assigned conditions |
67
|
|
|
* |
68
|
|
|
* @return \WPEmerge\Routing\Conditions\ConditionInterface[] |
69
|
|
|
*/ |
70
|
1 |
|
public function getConditions() { |
71
|
1 |
|
return $this->conditions; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
* @codeCoverageIgnore |
77
|
|
|
*/ |
78
|
|
|
public function getUrlWhere() { |
79
|
|
|
return []; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritDoc} |
84
|
|
|
* @codeCoverageIgnore |
85
|
|
|
*/ |
86
|
|
|
public function setUrlWhere( $where ) { |
87
|
|
|
foreach ( $this->conditions as $condition ) { |
88
|
|
|
if ( $condition instanceof HasUrlInterface ) { |
89
|
|
|
$condition->setUrlWhere( $where ); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|