Completed
Pull Request — master (#153)
by Harry
02:35
created

Functions::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 4
cts 7
cp 0.5714
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2.3149
1
<?php
2
3
namespace League\Plates\Template;
4
5
use LogicException;
6
7
/**
8
 * A collection of template functions.
9
 */
10
class Functions
11
{
12
    /**
13
     * Array of template functions.
14
     * @var array
15
     */
16
    protected $functions = array();
17
18
    /**
19
     * Add a new template function.
20
     * @param  string    $name;
21
     * @param  callback  $callback;
22
     * @return Functions
23
     */
24 86
    public function add($name, $callback)
25
    {
26 86
        if ($this->exists($name)) {
27 2
            throw new LogicException(
28 2
                'The template function name "' . $name . '" is already registered.'
29 2
            );
30
        }
31
32 86
        $this->functions[$name] = new Func($name, $callback);
33
34 86
        return $this;
35
    }
36
37
    /**
38
     * Remove a template function.
39
     * @param  string    $name;
40
     * @return Functions
41
     */
42 8
    public function remove($name)
43
    {
44 8
        if (!$this->exists($name)) {
45 4
            throw new LogicException(
46 4
                'The template function "' . $name . '" was not found.'
47 4
            );
48
        }
49
50 4
        unset($this->functions[$name]);
51
52 4
        return $this;
53
    }
54
55
    /**
56
     * Get a template function.
57
     * @param  string $name
58
     * @return Func
59
     */
60 14
    public function get($name)
61
    {
62 14
        if (!$this->exists($name)) {
63 4
            throw new LogicException('The template function "' . $name . '" was not found.');
64
        }
65
66 10
        return $this->functions[$name];
67
    }
68
69
    /**
70
     * Check if a template function exists.
71
     * @param  string  $name
72
     * @return boolean
73
     */
74 96
    public function exists($name)
75
    {
76 96
        return isset($this->functions[$name]);
77
    }
78
}
79