Completed
Pull Request — master (#327)
by Claus
01:54
created

ViewHelperVariableContainer::addAll()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 2
nop 2
dl 0
loc 14
rs 8.8571
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; if null, all keys and values are returned.
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 = null, $default = null)
99
    {
100
        if (!$key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $key of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
101
            return array_key_exists($viewHelperName, $this->objects) ? $this->objects[$viewHelperName] : $default;
102
        }
103
        return $this->exists($viewHelperName, $key) ? $this->objects[$viewHelperName][$key] : $default;
104
    }
105
106
    /**
107
     * Determine whether there is a variable stored for the given key
108
     *
109
     * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper")
110
     * @param string $key Key of the data
111
     * @return boolean TRUE if a value for the given ViewHelperName / Key is stored, FALSE otherwise.
112
     * @api
113
     */
114
    public function exists($viewHelperName, $key)
115
    {
116
        return isset($this->objects[$viewHelperName]) && array_key_exists($key, $this->objects[$viewHelperName]);
117
    }
118
119
    /**
120
     * Remove a value from the variable container
121
     *
122
     * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper")
123
     * @param string $key Key of the data to remove
124
     * @return void
125
     * @api
126
     */
127
    public function remove($viewHelperName, $key)
128
    {
129
        unset($this->objects[$viewHelperName][$key]);
130
    }
131
132
    /**
133
     * Set the view to pass it to ViewHelpers.
134
     *
135
     * @param ViewInterface $view View to set
136
     * @return void
137
     */
138
    public function setView(ViewInterface $view)
139
    {
140
        $this->view = $view;
141
    }
142
143
    /**
144
     * Get the view.
145
     *
146
     * !!! This is NOT a public API and might still change!!!
147
     *
148
     * @return ViewInterface The View
149
     */
150
    public function getView()
151
    {
152
        return $this->view;
153
    }
154
155
    /**
156
     * Clean up for serializing.
157
     *
158
     * @return array
159
     */
160
    public function __sleep()
161
    {
162
        return ['objects'];
163
    }
164
}
165