Data   A
last analyzed

↳ Parent: Project

Coupling/Cohesion

Components 1
Dependencies 0

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 84
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 18 4
A shareWithAll() 0 6 1
A shareWithSome() 0 12 3
A get() 0 8 2
1
<?php
2
3
namespace League\Plates\Template;
4
5
use LogicException;
6
7
/**
8
 * Preassigned template data.
9
 */
10
class Data
11
{
12
    /**
13
     * Variables shared by all templates.
14
     * @var array
15
     */
16
    protected $sharedVariables = array();
17
18
    /**
19
     * Specific template variables.
20
     * @var array
21
     */
22
    protected $templateVariables = array();
23
24
    /**
25
     * Add template data.
26
     * @param  array             $data;
27
     * @param  null|string|array $templates;
28
     * @return Data
29
     */
30 32
    public function add(array $data, $templates = null)
31
    {
32 32
        if (is_null($templates)) {
33 8
            return $this->shareWithAll($data);
34
        }
35
36 24
        if (is_array($templates)) {
37 8
            return $this->shareWithSome($data, $templates);
38
        }
39
40 16
        if (is_string($templates)) {
41 12
            return $this->shareWithSome($data, array($templates));
42
        }
43
44 4
        throw new LogicException(
45 4
            'The templates variable must be null, an array or a string, ' . gettype($templates) . ' given.'
46 2
        );
47
    }
48
49
    /**
50
     * Add data shared with all templates.
51
     * @param  array $data;
52
     * @return Data
53
     */
54 8
    public function shareWithAll($data)
55
    {
56 8
        $this->sharedVariables = array_merge($this->sharedVariables, $data);
57
58 8
        return $this;
59
    }
60
61
    /**
62
     * Add data shared with some templates.
63
     * @param  array $data;
64
     * @param  array $templates;
65
     * @return Data
66
     */
67 20
    public function shareWithSome($data, array $templates)
68
    {
69 20
        foreach ($templates as $template) {
70 20
            if (isset($this->templateVariables[$template])) {
71 4
                $this->templateVariables[$template] = array_merge($this->templateVariables[$template], $data);
72 2
            } else {
73 20
                $this->templateVariables[$template] = $data;
74
            }
75 10
        }
76
77 20
        return $this;
78
    }
79
80
    /**
81
     * Get template data.
82
     * @param  null|string $template;
83
     * @return array
84
     */
85 152
    public function get($template = null)
86
    {
87 152
        if (isset($template, $this->templateVariables[$template])) {
88 20
            return array_merge($this->sharedVariables, $this->templateVariables[$template]);
89
        }
90
91 132
        return $this->sharedVariables;
92
    }
93
}
94