ViewHelpers   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 11 2
A get() 0 7 2
A exists() 0 3 1
A add() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tebe\Pvc\View;
6
7
use LogicException;
8
9
/**
10
 * A collection of helper functions.
11
 */
12
class ViewHelpers
13
{
14
    /**
15
     * Array of template functions.
16
     * @var callable[]
17
     */
18
    private $helpers = [];
19
20
    /**
21
     * Add a new template function.
22
     * @param  string $name
23
     * @param  callback $callback
24
     * @return ViewHelpers
25
     */
26
    public function add(string $name, callable $callback): self
27
    {
28
        if ($this->exists($name)) {
29
            throw new LogicException(
30
                'The helper function name "' . $name . '" is already registered.'
31
            );
32
        }
33
34
        $this->helpers[$name] = $callback;
35
36
        return $this;
37
    }
38
39
    /**
40
     * Remove a template function.
41
     * @param  string $name
42
     * @return ViewHelpers
43
     */
44
    public function remove(string $name): self
45
    {
46
        if (!$this->exists($name)) {
47
            throw new LogicException(
48
                'The template function "' . $name . '" was not found.'
49
            );
50
        }
51
52
        unset($this->helpers[$name]);
53
54
        return $this;
55
    }
56
57
    /**
58
     * Get a template function.
59
     * @param  string $name
60
     * @return callable
61
     */
62
    public function get(string $name): callable
63
    {
64
        if (!$this->exists($name)) {
65
            throw new LogicException('The template function "' . $name . '" was not found.');
66
        }
67
68
        return $this->helpers[$name];
69
    }
70
71
    /**
72
     * Check if a template function exists.
73
     * @param  string $name
74
     * @return boolean
75
     */
76
    public function exists(string $name): bool
77
    {
78
        return isset($this->helpers[$name]);
79
    }
80
}
81