|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Plates\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use League\Plates\Engine; |
|
6
|
|
|
use League\Plates\Template\Template; |
|
7
|
|
|
use LogicException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Extension that adds a number of URI checks. |
|
11
|
|
|
*/ |
|
12
|
|
|
class URI implements ExtensionInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Instance of the current template. |
|
16
|
|
|
* @var Template |
|
17
|
|
|
*/ |
|
18
|
|
|
public $template; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The request URI. |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $uri; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The request URI as an array. |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $parts; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Create new URI instance. |
|
34
|
|
|
* @param string $uri |
|
35
|
|
|
*/ |
|
36
|
68 |
|
public function __construct($uri) |
|
37
|
|
|
{ |
|
38
|
68 |
|
$this->uri = $uri; |
|
39
|
68 |
|
$this->parts = explode('/', $this->uri); |
|
40
|
68 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Register extension functions. |
|
44
|
|
|
* @param Engine $engine |
|
45
|
|
|
* @return null |
|
46
|
|
|
*/ |
|
47
|
20 |
|
public function register(Engine $engine) |
|
48
|
|
|
{ |
|
49
|
20 |
|
$engine->registerFunction('uri', array($this, 'runUri')); |
|
50
|
20 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Perform URI check. |
|
54
|
|
|
* @param null|integer|string $var1 |
|
55
|
|
|
* @param mixed $var2 |
|
56
|
|
|
* @param mixed $var3 |
|
57
|
|
|
* @param mixed $var4 |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
52 |
|
public function runUri($var1 = null, $var2 = null, $var3 = null, $var4 = null) |
|
61
|
|
|
{ |
|
62
|
52 |
|
if (is_null($var1)) { |
|
63
|
4 |
|
return $this->uri; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
48 |
|
if (is_numeric($var1) and is_null($var2)) { |
|
67
|
8 |
|
return array_key_exists($var1, $this->parts) ? $this->parts[$var1] : null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
40 |
|
if (is_numeric($var1) and is_string($var2)) { |
|
71
|
20 |
|
return $this->checkUriSegmentMatch($var1, $var2, $var3, $var4); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
20 |
|
if (is_string($var1)) { |
|
75
|
16 |
|
return $this->checkUriRegexMatch($var1, $var2, $var3); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
4 |
|
throw new LogicException('Invalid use of the uri function.'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Perform a URI segment match. |
|
83
|
|
|
* @param integer $key |
|
84
|
|
|
* @param string $string |
|
85
|
|
|
* @param mixed $returnOnTrue |
|
86
|
|
|
* @param mixed $returnOnFalse |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
20 |
|
protected function checkUriSegmentMatch($key, $string, $returnOnTrue = null, $returnOnFalse = null) |
|
90
|
|
|
{ |
|
91
|
20 |
|
if (array_key_exists($key, $this->parts) && $this->parts[$key] === $string) { |
|
92
|
8 |
|
return is_null($returnOnTrue) ? true : $returnOnTrue; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
16 |
|
return is_null($returnOnFalse) ? false : $returnOnFalse; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Perform a regular express match. |
|
100
|
|
|
* @param string $regex |
|
101
|
|
|
* @param mixed $returnOnTrue |
|
102
|
|
|
* @param mixed $returnOnFalse |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
16 |
|
protected function checkUriRegexMatch($regex, $returnOnTrue = null, $returnOnFalse = null) |
|
106
|
|
|
{ |
|
107
|
16 |
|
if (preg_match('#^' . $regex . '$#', $this->uri) === 1) { |
|
108
|
8 |
|
return is_null($returnOnTrue) ? true : $returnOnTrue; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
8 |
|
return is_null($returnOnFalse) ? false : $returnOnFalse; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|