|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Plates\Template; |
|
4
|
|
|
|
|
5
|
|
|
use League\Plates\Extension\ExtensionInterface; |
|
6
|
|
|
use LogicException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A template function. |
|
10
|
|
|
*/ |
|
11
|
|
|
class Func |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The function name. |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $name; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The function callback. |
|
21
|
|
|
* @var callable |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $callback; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Create new Func instance. |
|
27
|
|
|
* @param string $name |
|
28
|
|
|
* @param callable $callback |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($name, $callback) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->setName($name); |
|
33
|
|
|
$this->setCallback($callback); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Set the function name. |
|
38
|
|
|
* @param string $name |
|
39
|
|
|
* @return Func |
|
40
|
|
|
*/ |
|
41
|
|
|
public function setName($name) |
|
42
|
|
|
{ |
|
43
|
|
|
if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $name) !== 1) { |
|
44
|
|
|
throw new LogicException( |
|
45
|
|
|
'Not a valid function name.' |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->name = $name; |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the function name. |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getName() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->name; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Set the function callback |
|
65
|
|
|
* @param callable $callback |
|
66
|
|
|
* @return Func |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setCallback($callback) |
|
69
|
|
|
{ |
|
70
|
|
|
if (!is_callable($callback, true)) { |
|
71
|
|
|
throw new LogicException( |
|
72
|
|
|
'Not a valid function callback.' |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$this->callback = $callback; |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the function callback. |
|
83
|
|
|
* @return callable |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getCallback() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->callback; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Call the function. |
|
92
|
|
|
* @param Template $template |
|
93
|
|
|
* @param array $arguments |
|
94
|
|
|
* @return mixed |
|
95
|
|
|
*/ |
|
96
|
|
|
public function call(Template $template = null, $arguments = array()) |
|
97
|
|
|
{ |
|
98
|
|
|
if (is_array($this->callback) and |
|
99
|
|
|
isset($this->callback[0]) and |
|
100
|
|
|
$this->callback[0] instanceof ExtensionInterface |
|
101
|
|
|
) { |
|
102
|
|
|
$this->callback[0]->template = $template; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return call_user_func_array($this->callback, $arguments); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|