Completed
Pull Request — master (#429)
by Claus
02:05
created

pushDelegateVariableContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 7
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\Core\Variables\VariableProviderInterface;
10
use TYPO3Fluid\Fluid\View\ViewInterface;
11
12
/**
13
 * A key/value store that can be used by ViewHelpers to communicate between each other.
14
 *
15
 * @api
16
 */
17
class ViewHelperVariableContainer
18
{
19
20
    /**
21
     * Two-dimensional object array storing the values. The first dimension is the fully qualified ViewHelper name,
22
     * and the second dimension is the identifier for the data the ViewHelper wants to store.
23
     *
24
     * @var array
25
     */
26
    protected $objects = [];
27
28
    /**
29
     * @var ViewInterface
30
     */
31
    protected $view;
32
33
    public function pushDelegateVariableContainer($viewHelperClassName, VariableProviderInterface $variableProvider)
34
    {
35
        if (!isset($this->objects[$viewHelperClassName]['delegateVariableProviderStack'])) {
36
            $this->objects[$viewHelperClassName]['delegateVariableProviderStack'] = [];
37
        }
38
        $this->objects[$viewHelperClassName]['delegateVariableProviderStack'][] = $variableProvider;
39
    }
40
41 View Code Duplication
    public function getTopmostDelegateVariableContainer($viewHelperClassName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        if (!isset($this->objects[$viewHelperClassName]['delegateVariableProviderStack'])) {
44
            return null;
45
        }
46
        return end($this->objects[$viewHelperClassName]['delegateVariableProviderStack']);
47
    }
48
49 View Code Duplication
    public function popDelegateVariableContainer($viewHelperClassName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        if (isset($this->objects[$viewHelperClassName]['delegateVariableProviderStack'])) {
52
            return array_pop($this->objects[$viewHelperClassName]['delegateVariableProviderStack']);
53
        }
54
        return null;
55
    }
56
57
    /**
58
     * Add a variable to the Variable Container. Make sure that $viewHelperName is ALWAYS set
59
     * to your fully qualified ViewHelper Class Name
60
     *
61
     * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper")
62
     * @param string $key Key of the data
63
     * @param mixed $value The value to store
64
     * @return void
65
     * @api
66
     */
67
    public function add($viewHelperName, $key, $value)
68
    {
69
        $this->addOrUpdate($viewHelperName, $key, $value);
70
    }
71
72
    /**
73
     * Adds, or overrides recursively, all current variables defined in associative
74
     * array or Traversable (with string keys!).
75
     *
76
     * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper")
77
     * @param array|\Traversable $variables An associative array of all variables to add
78
     * @return void
79
     * @api
80
     */
81
    public function addAll($viewHelperName, $variables)
82
    {
83
        if (!is_array($variables) && !$variables instanceof \Traversable) {
84
            throw new \InvalidArgumentException(
85
                'Invalid argument type for $variables in ViewHelperVariableContainer->addAll(). Expects array/Traversable ' .
86
                'but received ' . (is_object($variables) ? get_class($variables) : gettype($variables)),
87
                1501425195
88
            );
89
        }
90
        $this->objects[$viewHelperName] = array_replace_recursive(
91
            isset($this->objects[$viewHelperName]) ? $this->objects[$viewHelperName] : [],
92
            $variables instanceof \Traversable ? iterator_to_array($variables) : $variables
93
        );
94
    }
95
96
    /**
97
     * Add a variable to the Variable Container. Make sure that $viewHelperName is ALWAYS set
98
     * to your fully qualified ViewHelper Class Name.
99
     * In case the value is already inside, it is silently overridden.
100
     *
101
     * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper")
102
     * @param string $key Key of the data
103
     * @param mixed $value The value to store
104
     * @return void
105
     */
106
    public function addOrUpdate($viewHelperName, $key, $value)
107
    {
108
        if (!isset($this->objects[$viewHelperName])) {
109
            $this->objects[$viewHelperName] = [];
110
        }
111
        $this->objects[$viewHelperName][$key] = $value;
112
    }
113
114
    /**
115
     * Gets a variable which is stored
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
     * @param mixed $default Default value to use if no value is found.
120
     * @return mixed The object stored
121
     * @api
122
     */
123
    public function get($viewHelperName, $key, $default = null)
124
    {
125
        return $this->exists($viewHelperName, $key) ? $this->objects[$viewHelperName][$key] : $default;
126
    }
127
128
    /**
129
     * Gets all variables stored for a particular ViewHelper
130
     *
131
     * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper")
132
     * @param mixed $default
133
     * @return array
134
     */
135
    public function getAll($viewHelperName, $default = null)
136
    {
137
        return array_key_exists($viewHelperName, $this->objects) ? $this->objects[$viewHelperName] : $default;
138
    }
139
140
    /**
141
     * Determine whether there is a variable stored for the given key
142
     *
143
     * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper")
144
     * @param string $key Key of the data
145
     * @return boolean TRUE if a value for the given ViewHelperName / Key is stored, FALSE otherwise.
146
     * @api
147
     */
148
    public function exists($viewHelperName, $key)
149
    {
150
        return isset($this->objects[$viewHelperName]) && array_key_exists($key, $this->objects[$viewHelperName]);
151
    }
152
153
    /**
154
     * Remove a value from the variable container
155
     *
156
     * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper")
157
     * @param string $key Key of the data to remove
158
     * @return void
159
     * @api
160
     */
161
    public function remove($viewHelperName, $key)
162
    {
163
        unset($this->objects[$viewHelperName][$key]);
164
    }
165
166
    /**
167
     * Set the view to pass it to ViewHelpers.
168
     *
169
     * @param ViewInterface $view View to set
170
     * @return void
171
     */
172
    public function setView(ViewInterface $view)
173
    {
174
        $this->view = $view;
175
    }
176
177
    /**
178
     * Get the view.
179
     *
180
     * !!! This is NOT a public API and might still change!!!
181
     *
182
     * @return ViewInterface The View
183
     */
184
    public function getView()
185
    {
186
        return $this->view;
187
    }
188
189
    /**
190
     * Clean up for serializing.
191
     *
192
     * @return array
193
     */
194
    public function __sleep()
195
    {
196
        return ['objects'];
197
    }
198
}
199