|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WPEmerge\Routing\Conditions; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use WPEmerge; |
|
8
|
|
|
use WPEmerge\Routing\Conditions\Custom as CustomCondition; |
|
9
|
|
|
use WPEmerge\Routing\Conditions\Multiple as MultipleCondition; |
|
10
|
|
|
use WPEmerge\Routing\Conditions\Url as UrlCondition; |
|
11
|
|
|
use ReflectionClass; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Check against the current url |
|
15
|
|
|
*/ |
|
16
|
|
|
class Factory { |
|
17
|
|
|
/** |
|
18
|
|
|
* Create a new condition |
|
19
|
|
|
* |
|
20
|
|
|
* @throws InvalidRouteConditionException |
|
21
|
|
|
* @param string|array|Closure $options |
|
22
|
|
|
* @return ConditionInterface |
|
23
|
|
|
*/ |
|
24
|
12 |
|
public static function make( $options ) { |
|
25
|
12 |
|
if ( is_string( $options ) ) { |
|
26
|
2 |
|
return static::makeFromUrl( $options ); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
10 |
|
if ( is_array( $options ) ) { |
|
30
|
8 |
|
return static::makeFromArray( $options ); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
if ( is_a( $options, Closure::class ) ) { |
|
34
|
1 |
|
return static::makeFromClosure( $options ); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
throw new InvalidRouteConditionException( 'Invalid condition options supplied.' ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Check if the passed argument is a registered condition type |
|
42
|
|
|
* |
|
43
|
|
|
* @param mixed $condition_type |
|
44
|
|
|
* @return boolean |
|
45
|
|
|
*/ |
|
46
|
6 |
|
protected static function conditionTypeRegistered( $condition_type ) { |
|
47
|
6 |
|
if ( ! is_string( $condition_type ) ) { |
|
48
|
1 |
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
5 |
|
$condition_class = WPEmerge::resolve( WP_EMERGE_ROUTING_CONDITIONS_KEY . $condition_type ); |
|
52
|
5 |
|
return ( $condition_class !== null ); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Resolve the condition type and its arguments from an options array |
|
57
|
|
|
* |
|
58
|
|
|
* @throws Exception |
|
59
|
|
|
* @param array $options |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
6 |
|
protected static function getConditionTypeAndArguments( $options ) { |
|
63
|
6 |
|
$type = $options[0]; |
|
64
|
6 |
|
$arguments = array_slice( $options, 1 ); |
|
65
|
|
|
|
|
66
|
6 |
|
if ( ! static::conditionTypeRegistered( $type ) ) { |
|
67
|
3 |
|
if ( is_callable( $type ) ) { |
|
68
|
2 |
|
$type = 'custom'; |
|
69
|
2 |
|
$arguments = $options; |
|
70
|
2 |
|
} else { |
|
71
|
1 |
|
throw new Exception( 'Unknown condition type specified: ' . $type ); |
|
72
|
|
|
} |
|
73
|
2 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
return array( |
|
76
|
5 |
|
'type' => $type, |
|
77
|
5 |
|
'arguments' => $arguments, |
|
78
|
5 |
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Create a new condition from a url |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $url |
|
85
|
|
|
* @return ConditionInterface |
|
86
|
|
|
*/ |
|
87
|
1 |
|
protected static function makeFromUrl( $url ) { |
|
88
|
1 |
|
return new UrlCondition( $url ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Create a new condition from an array of conditions |
|
93
|
|
|
* |
|
94
|
|
|
* @param array $options |
|
95
|
|
|
* @return ConditionInterface |
|
96
|
|
|
*/ |
|
97
|
1 |
|
protected static function makeFromArrayOfConditions( $options ) { |
|
98
|
1 |
|
return new MultipleCondition( $options ); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Create a new condition from an array |
|
103
|
|
|
* |
|
104
|
|
|
* @throws Exception |
|
105
|
|
|
* @param array $options |
|
106
|
|
|
* @return ConditionInterface |
|
107
|
|
|
*/ |
|
108
|
8 |
|
protected static function makeFromArray( $options ) { |
|
109
|
8 |
|
if ( count( $options ) === 0 ) { |
|
110
|
1 |
|
throw new Exception( 'No condition type specified.' ); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
7 |
|
if ( is_array( $options[0] ) ) { |
|
114
|
1 |
|
return static::makeFromArrayOfConditions( $options ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
7 |
|
$condition_options = static::getConditionTypeAndArguments( $options ); |
|
118
|
6 |
|
$condition_class = WPEmerge::resolve( WP_EMERGE_ROUTING_CONDITIONS_KEY . $condition_options['type'] ); |
|
119
|
|
|
|
|
120
|
6 |
|
$reflection = new ReflectionClass( $condition_class ); |
|
121
|
6 |
|
$condition = $reflection->newInstanceArgs( $condition_options['arguments'] ); |
|
122
|
6 |
|
return $condition; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Create a new condition from a closure |
|
127
|
|
|
* |
|
128
|
|
|
* @param Closure $closure |
|
129
|
|
|
* @return ConditionInterface |
|
130
|
|
|
*/ |
|
131
|
1 |
|
protected static function makeFromClosure( Closure $closure ) { |
|
132
|
1 |
|
return new CustomCondition( $closure ); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|