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
|
100 |
|
public function __construct($name, $callback) |
31
|
|
|
{ |
32
|
100 |
|
$this->setName($name); |
33
|
100 |
|
$this->setCallback($callback); |
34
|
100 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set the function name. |
38
|
|
|
* @param string $name |
39
|
|
|
* @return Func |
40
|
|
|
*/ |
41
|
100 |
|
public function setName($name) |
42
|
|
|
{ |
43
|
100 |
|
if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $name) !== 1) { |
44
|
2 |
|
throw new LogicException( |
45
|
|
|
'Not a valid function name.' |
46
|
2 |
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
100 |
|
$this->name = $name; |
50
|
|
|
|
51
|
100 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the function name. |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
4 |
|
public function getName() |
59
|
|
|
{ |
60
|
4 |
|
return $this->name; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set the function callback |
65
|
|
|
* @param callable $callback |
66
|
|
|
* @return Func |
67
|
|
|
*/ |
68
|
100 |
|
public function setCallback($callback) |
69
|
|
|
{ |
70
|
100 |
|
if (!is_callable($callback, true)) { |
71
|
2 |
|
throw new LogicException( |
72
|
|
|
'Not a valid function callback.' |
73
|
2 |
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
100 |
|
$this->callback = $callback; |
77
|
|
|
|
78
|
100 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the function callback. |
83
|
|
|
* @return callable |
84
|
|
|
*/ |
85
|
8 |
|
public function getCallback() |
86
|
|
|
{ |
87
|
8 |
|
return $this->callback; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Call the function. |
92
|
|
|
* @param Template $template |
93
|
|
|
* @param array $arguments |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
8 |
|
public function call(Template $template = null, $arguments = array()) |
97
|
|
|
{ |
98
|
8 |
|
if (is_array($this->callback) and |
99
|
8 |
|
isset($this->callback[0]) and |
100
|
2 |
|
$this->callback[0] instanceof ExtensionInterface |
101
|
8 |
|
) { |
102
|
2 |
|
$this->callback[0]->template = $template; |
103
|
2 |
|
} |
104
|
|
|
|
105
|
8 |
|
return call_user_func_array($this->callback, $arguments); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|