Completed
Push — master ( 30592b...7a2823 )
by David
10s
created

lib/Dwoo/Data.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Copyright (c) 2013-2017
4
 *
5
 * @category  Library
6
 * @package   Dwoo
7
 * @author    Jordi Boggiano <[email protected]>
8
 * @author    David Sanchez <[email protected]>
9
 * @copyright 2008-2013 Jordi Boggiano
10
 * @copyright 2013-2017 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.4
13
 * @date      2017-03-01
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo;
18
19
/**
20
 * Dwoo data object, use it for complex data assignments or if you want to easily pass it
21
 * around multiple functions to avoid passing an array by reference.
22
 * This software is provided 'as-is', without any express or implied warranty.
23
 * In no event will the authors be held liable for any damages arising from the use of this software.
24
 */
25
class Data implements IDataProvider
26
{
27
    /**
28
     * Data array.
29
     *
30
     * @var array
31
     */
32
    protected $data = array();
33
34
    /**
35
     * Returns the data array.
36
     *
37
     * @return array
38
     */
39
    public function getData()
40
    {
41
        return $this->data;
42
    }
43
44
    /**
45
     * Clears a the entire data or only the given key.
46
     *
47
     * @param array|string $name clears only one value if you give a name, multiple values if
48
     *                           you give an array of names, or the entire data if left null
49
     */
50
    public function clear($name = null)
51
    {
52
        if ($name === null) {
53
            $this->data = array();
54
        } elseif (is_array($name)) {
55
            foreach ($name as $index) {
56
                unset($this->data[$index]);
57
            }
58
        } else {
59
            unset($this->data[$name]);
60
        }
61
    }
62
63
    /**
64
     * Overwrites the entire data with the given array.
65
     *
66
     * @param array $data the new data array to use
67
     */
68
    public function setData(array $data)
69
    {
70
        $this->data = $data;
71
    }
72
73
    /**
74
     * merges the given array(s) with the current data with array_merge.
75
     *
76
     * @param array $data  the array to merge
77
     */
78
    public function mergeData(array $data)
0 ignored issues
show
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
        $args = func_get_args();
81
        foreach ($args as $key => $v) {
82
            if (is_array($v)) {
83
                $this->data = array_merge($this->data, $v);
84
            }
85
        }
86
    }
87
88
    /**
89
     * Assigns a value or an array of values to the data object.
90
     *
91
     * @param array|string $name an associative array of multiple (index=>value) or a string
92
     *                           that is the index to use, i.e. a value assigned to "foo" will be
93
     *                           accessible in the template through {$foo}
94
     * @param mixed        $val  the value to assign, or null if $name was an array
95
     */
96
    public function assign($name, $val = null)
97
    {
98
        if (is_array($name)) {
99
            reset($name);
100
            foreach ($name as $k => $v){
101
                $this->data[$k] = $v;
102
            }
103
        } else {
104
            $this->data[$name] = $val;
105
        }
106
    }
107
108
    /**
109
     * Allows to assign variables using the object syntax.
110
     *
111
     * @param string $name  the variable name
112
     * @param string $value the value to assign to it
113
     */
114
    public function __set($name, $value)
115
    {
116
        $this->assign($name, $value);
117
    }
118
119
    /**
120
     * Assigns a value by reference to the data object.
121
     *
122
     * @param string $name the index to use, i.e. a value assigned to "foo" will be
123
     *                     accessible in the template through {$foo}
124
     * @param mixed  $val  the value to assign by reference
125
     */
126
    public function assignByRef($name, &$val)
127
    {
128
        $this->data[$name] = &$val;
129
    }
130
131
    /**
132
     * Appends values or an array of values to the data object.
133
     *
134
     * @param array|string $name  an associative array of multiple (index=>value) or a string
135
     *                            that is the index to use, i.e. a value assigned to "foo" will be
136
     *                            accessible in the template through {$foo}
137
     * @param mixed        $val   the value to assign, or null if $name was an array
138
     * @param bool         $merge true to merge data or false to append, defaults to false
139
     */
140
    public function append($name, $val = null, $merge = false)
141
    {
142
        if (is_array($name)) {
143
            foreach ($name as $key => $val) {
144 View Code Duplication
                if (isset($this->data[$key]) && !is_array($this->data[$key])) {
145
                    settype($this->data[$key], 'array');
146
                }
147
148 View Code Duplication
                if ($merge === true && is_array($val)) {
149
                    $this->data[$key] = $val + $this->data[$key];
150
                } else {
151
                    $this->data[$key][] = $val;
152
                }
153
            }
154
        } elseif ($val !== null) {
155
            if (isset($this->data[$name]) && !is_array($this->data[$name])) {
156
                settype($this->data[$name], 'array');
157
            } elseif (!isset($this->data[$name])) {
158
                $this->data[$name] = array();
159
            }
160
161 View Code Duplication
            if ($merge === true && is_array($val)) {
162
                $this->data[$name] = $val + $this->data[$name];
163
            } else {
164
                $this->data[$name][] = $val;
165
            }
166
        }
167
    }
168
169
    /**
170
     * Appends a value by reference to the data object.
171
     *
172
     * @param string $name  the index to use, i.e. a value assigned to "foo" will be
173
     *                      accessible in the template through {$foo}
174
     * @param mixed  $val   the value to append by reference
175
     * @param bool   $merge true to merge data or false to append, defaults to false
176
     */
177
    public function appendByRef($name, &$val, $merge = false)
178
    {
179 View Code Duplication
        if (isset($this->data[$name]) && !is_array($this->data[$name])) {
180
            settype($this->data[$name], 'array');
181
        }
182
183
        if ($merge === true && is_array($val)) {
184
            foreach ($val as $key => &$value) {
185
                $this->data[$name][$key] = &$value;
186
            }
187
        } else {
188
            $this->data[$name][] = &$val;
189
        }
190
    }
191
192
    /**
193
     * Returns true if the variable has been assigned already, false otherwise.
194
     *
195
     * @param string $name the variable name
196
     *
197
     * @return bool
198
     */
199
    public function isAssigned($name)
200
    {
201
        return isset($this->data[$name]);
202
    }
203
204
    /**
205
     * Supports calls to isset($dwoo->var).
206
     *
207
     * @param string $name the variable name
208
     *
209
     * @return bool
210
     */
211
    public function __isset($name)
212
    {
213
        return isset($this->data[$name]);
214
    }
215
216
    /**
217
     * Unassigns/removes a variable.
218
     *
219
     * @param string $name the variable name
220
     */
221
    public function unassign($name)
222
    {
223
        unset($this->data[$name]);
224
    }
225
226
    /**
227
     * Supports unsetting variables using the object syntax.
228
     *
229
     * @param string $name the variable name
230
     */
231
    public function __unset($name)
232
    {
233
        unset($this->data[$name]);
234
    }
235
236
    /**
237
     * Returns a variable if it was assigned.
238
     *
239
     * @param string $name the variable name
240
     *
241
     * @return mixed
242
     */
243
    public function get($name)
244
    {
245
        return $this->__get($name);
246
    }
247
248
    /**
249
     * Allows to read variables using the object syntax.
250
     *
251
     * @param string $name the variable name
252
     *
253
     * @return mixed
254
     * @throws Exception
255
     */
256
    public function __get($name)
257
    {
258
        if (isset($this->data[$name])) {
259
            return $this->data[$name];
260
        } else {
261
            throw new Exception('Tried to read a value that was not assigned yet : "' . $name . '"');
262
        }
263
    }
264
}
265