1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CarbonFramework\Routing\Conditions; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
use Exception; |
8
|
|
|
use CarbonFramework\Framework; |
9
|
|
|
use CarbonFramework\Routing\Conditions\Url as UrlCondition; |
10
|
|
|
use CarbonFramework\Routing\Conditions\Custom as CustomCondition; |
11
|
|
|
use CarbonFramework\Routing\Conditions\Multiple as MultipleCondition; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Check against the current url |
15
|
|
|
*/ |
16
|
|
|
class Factory { |
17
|
|
|
/** |
18
|
|
|
* Create a new condition |
19
|
|
|
* |
20
|
|
|
* @param string|Condition|array $options |
21
|
|
|
* @return ConditionInterface |
22
|
|
|
*/ |
23
|
|
|
public static function make( $options ) { |
24
|
|
|
if ( is_string( $options ) ) { |
25
|
|
|
return static::makeFromUrl( $options ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if ( is_a( $options, Closure::class ) ) { |
29
|
|
|
return static::makeFromClosure( $options ); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ( is_array( $options ) ) { |
33
|
|
|
return static::makeFromArray( $options ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
throw new InvalidRouteConditionException( 'Invalid condition options supplied.' ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a new condition from a url |
41
|
|
|
* |
42
|
|
|
* @param string $url |
43
|
|
|
* @return ConditionInterface |
44
|
|
|
*/ |
45
|
|
|
protected static function makeFromUrl( $url ) { |
46
|
|
|
return new UrlCondition( $url ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create a new condition from a closure |
51
|
|
|
* |
52
|
|
|
* @param Closure $closure |
53
|
|
|
* @return ConditionInterface |
54
|
|
|
*/ |
55
|
|
|
protected static function makeFromClosure( Closure $closure ) { |
56
|
|
|
return new CustomCondition( $closure ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Create a new condition from an array |
61
|
|
|
* |
62
|
|
|
* @param array $options |
63
|
|
|
* @return ConditionInterface |
64
|
|
|
*/ |
65
|
|
|
protected static function makeFromArray( $options ) { |
66
|
|
|
if ( count( $options ) === 0 ) { |
67
|
|
|
throw new Exception( 'No condition type specified.' ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ( is_array( $options[0] ) ) { |
71
|
|
|
return static::makeFromArrayOfConditions( $options ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$condition_type = $options[0]; |
75
|
|
|
$arguments = array_slice( $options, 1 ); |
76
|
|
|
|
77
|
|
|
$condition_class = Framework::resolve( 'framework.routing.conditions.' . $condition_type ); |
78
|
|
|
if ( $condition_class === null ) { |
79
|
|
|
throw new Exception( 'Unknown condition type specified: ' . $condition_type ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$reflection = new ReflectionClass( $condition_class ); |
83
|
|
|
$condition = $reflection->newInstanceArgs( $arguments ); |
84
|
|
|
return $condition; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Create a new condition from an array of conditions |
89
|
|
|
* |
90
|
|
|
* @param array $options |
91
|
|
|
* @return ConditionInterface |
92
|
|
|
*/ |
93
|
|
|
protected static function makeFromArrayOfConditions( $options ) { |
94
|
|
|
return new MultipleCondition( $options ); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.