Completed
Push — master ( b89d9d...b4e7de )
by Marc
02:04
created

ViewHelperVariableContainer::getAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace TYPO3Fluid\Fluid\Core\ViewHelper;
3
4
/*
5
 * This file belongs to the package "TYPO3 Fluid".
6
 * See LICENSE.txt that was shipped with this package.
7
 */
8
9
use TYPO3Fluid\Fluid\View\ViewInterface;
10
11
/**
12
 * A key/value store that can be used by ViewHelpers to communicate between each other.
13
 *
14
 * @api
15
 */
16
class ViewHelperVariableContainer
17
{
18
19
    /**
20
     * Two-dimensional object array storing the values. The first dimension is the fully qualified ViewHelper name,
21
     * and the second dimension is the identifier for the data the ViewHelper wants to store.
22
     *
23
     * @var array
24
     */
25
    protected $objects = [];
26
27
    /**
28
     * @var ViewInterface
29
     */
30
    protected $view;
31
32
    /**
33
     * Add a variable to the Variable Container. Make sure that $viewHelperName is ALWAYS set
34
     * to your fully qualified ViewHelper Class Name
35
     *
36
     * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper")
37
     * @param string $key Key of the data
38
     * @param mixed $value The value to store
39
     * @return void
40
     * @api
41
     */
42
    public function add($viewHelperName, $key, $value)
43
    {
44
        $this->addOrUpdate($viewHelperName, $key, $value);
45
    }
46
47
    /**
48
     * Adds, or overrides recursively, all current variables defined in associative
49
     * array or Traversable (with string keys!).
50
     *
51
     * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper")
52
     * @param array|\Traversable $variables An associative array of all variables to add
53
     * @return void
54
     * @api
55
     */
56
    public function addAll($viewHelperName, $variables)
57
    {
58
        if (!is_array($variables) && !$variables instanceof \Traversable) {
59
            throw new \InvalidArgumentException(
60
                'Invalid argument type for $variables in ViewHelperVariableContainer->addAll(). Expects array/Traversable ' .
61
                'but received ' . (is_object($variables) ? get_class($variables) : gettype($variables)),
62
                1501425195
63
            );
64
        }
65
        $this->objects[$viewHelperName] = array_replace_recursive(
66
            isset($this->objects[$viewHelperName]) ? $this->objects[$viewHelperName] : [],
67
            $variables instanceof \Traversable ? iterator_to_array($variables) : $variables
68
        );
69
    }
70
71
    /**
72
     * Add a variable to the Variable Container. Make sure that $viewHelperName is ALWAYS set
73
     * to your fully qualified ViewHelper Class Name.
74
     * In case the value is already inside, it is silently overridden.
75
     *
76
     * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper")
77
     * @param string $key Key of the data
78
     * @param mixed $value The value to store
79
     * @return void
80
     */
81
    public function addOrUpdate($viewHelperName, $key, $value)
82
    {
83
        if (!isset($this->objects[$viewHelperName])) {
84
            $this->objects[$viewHelperName] = [];
85
        }
86
        $this->objects[$viewHelperName][$key] = $value;
87
    }
88
89
    /**
90
     * Gets a variable which is stored
91
     *
92
     * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper")
93
     * @param string $key Key of the data
94
     * @param mixed $default Default value to use if no value is found.
95
     * @return mixed The object stored
96
     * @api
97
     */
98
    public function get($viewHelperName, $key, $default = null)
99
    {
100
        return $this->exists($viewHelperName, $key) ? $this->objects[$viewHelperName][$key] : $default;
101
    }
102
103
    /**
104
     * Gets all variables stored for a particular ViewHelper
105
     *
106
     * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper")
107
     * @param mixed $default
108
     */
109
    public function getAll($viewHelperName, $default = null)
110
    {
111
        return array_key_exists($viewHelperName, $this->objects) ? $this->objects[$viewHelperName] : $default;
112
    }
113
114
    /**
115
     * Determine whether there is a variable stored for the given key
116
     *
117
     * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper")
118
     * @param string $key Key of the data
119
     * @return boolean TRUE if a value for the given ViewHelperName / Key is stored, FALSE otherwise.
120
     * @api
121
     */
122
    public function exists($viewHelperName, $key)
123
    {
124
        return isset($this->objects[$viewHelperName]) && array_key_exists($key, $this->objects[$viewHelperName]);
125
    }
126
127
    /**
128
     * Remove a value from the variable container
129
     *
130
     * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper")
131
     * @param string $key Key of the data to remove
132
     * @return void
133
     * @api
134
     */
135
    public function remove($viewHelperName, $key)
136
    {
137
        unset($this->objects[$viewHelperName][$key]);
138
    }
139
140
    /**
141
     * Set the view to pass it to ViewHelpers.
142
     *
143
     * @param ViewInterface $view View to set
144
     * @return void
145
     */
146
    public function setView(ViewInterface $view)
147
    {
148
        $this->view = $view;
149
    }
150
151
    /**
152
     * Get the view.
153
     *
154
     * !!! This is NOT a public API and might still change!!!
155
     *
156
     * @return ViewInterface The View
157
     */
158
    public function getView()
159
    {
160
        return $this->view;
161
    }
162
163
    /**
164
     * Clean up for serializing.
165
     *
166
     * @return array
167
     */
168
    public function __sleep()
169
    {
170
        return ['objects'];
171
    }
172
}
173