Completed
Push — xmfsync ( d2ecf4...a4059d )
by Richard
05:48
created

XoopsObject::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * XOOPS Kernel Object
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
namespace Xoops\Core\Kernel;
14
15
use Xoops\Core\Kernel\Dtype;
16
17
/**
18
 * Establish Xoops object datatype legacy defines
19
 * New code should use Dtype::TYPE_* constants
20
 *
21
 * These will eventually be removed. See Xoops\Core\Kernel\Dtype for more.
22
 */
23
define('XOBJ_DTYPE_TXTBOX',  Dtype::TYPE_TEXT_BOX);
24
define('XOBJ_DTYPE_TXTAREA', Dtype::TYPE_TEXT_AREA);
25
define('XOBJ_DTYPE_INT',     Dtype::TYPE_INTEGER);
26
define('XOBJ_DTYPE_URL',     Dtype::TYPE_URL);
27
define('XOBJ_DTYPE_EMAIL',   Dtype::TYPE_EMAIL);
28
define('XOBJ_DTYPE_ARRAY',   Dtype::TYPE_ARRAY);
29
define('XOBJ_DTYPE_OTHER',   Dtype::TYPE_OTHER);
30
define('XOBJ_DTYPE_SOURCE',  Dtype::TYPE_SOURCE);
31
define('XOBJ_DTYPE_STIME',   Dtype::TYPE_SHORT_TIME);
32
define('XOBJ_DTYPE_MTIME',   Dtype::TYPE_MEDIUM_TIME);
33
define('XOBJ_DTYPE_LTIME',   Dtype::TYPE_LONG_TIME);
34
define('XOBJ_DTYPE_FLOAT',   Dtype::TYPE_FLOAT);
35
define('XOBJ_DTYPE_DECIMAL', Dtype::TYPE_DECIMAL);
36
define('XOBJ_DTYPE_ENUM',    Dtype::TYPE_ENUM);
37
38
/**
39
 * Base class for all objects in the Xoops kernel (and beyond)
40
 *
41
 * @category  Xoops\Core\Kernel\XoopsObject
42
 * @package   Xoops\Core\Kernel
43
 * @author    Kazumi Ono (AKA onokazu) <http://www.myweb.ne.jp/, http://jp.xoops.org/>
44
 * @author    Taiwen Jiang <[email protected]>
45
 * @copyright 2000-2015 XOOPS Project (http://xoops.org)
46
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
47
 * @link      http://xoops.org
48
 * @since     2.0.0
49
 */
50
abstract class XoopsObject implements \ArrayAccess
51
{
52
    /**
53
     * holds all variables(properties) of an object
54
     *
55
     * @var array
56
     */
57
    public $vars = array();
58
59
    /**
60
     * variables cleaned for store in DB
61
     *
62
     * @var array
63
     */
64
    public $cleanVars = array();
65
66
    /**
67
     * is it a newly created object?
68
     *
69
     * @var bool
70
     */
71
    private $isNew = false;
72
73
    /**
74
     * has any of the values been modified?
75
     *
76
     * @var bool
77
     */
78
    private $isDirty = false;
79
80
    /**
81
     * errors
82
     *
83
     * @var array
84
     */
85
    private $errors = array();
86
87
    /**
88
     * @var string
89
     */
90
    public $plugin_path;
91
92
    /**
93
     * used for new/clone objects
94
     *
95
     * @return void
96
     */
97
    public function setNew()
98
    {
99
        $this->isNew = true;
100
    }
101
102
    /**
103
     * clear new flag
104 13
     *
105
     * @return void
106 13
     */
107 13
    public function unsetNew()
108
    {
109
        $this->isNew = false;
110
    }
111
112
    /**
113
     * check new flag
114 8
     *
115
     * @return bool
116 8
     */
117 8
    public function isNew()
118
    {
119
        return $this->isNew;
120
    }
121
122
    /**
123
     * mark modified objects as dirty
124 12
     *
125
     * used for modified objects only
126 12
     *
127
     * @return void
128
     */
129
    public function setDirty()
130
    {
131
        $this->isDirty = true;
132
    }
133
134
    /**
135
     * cleaar dirty flag
136 29
     *
137
     * @return void
138 29
     */
139 29
    public function unsetDirty()
140
    {
141
        $this->isDirty = false;
142
    }
143
144
    /**
145
     * check dirty flag
146 14
     *
147
     * @return bool
148 14
     */
149 14
    public function isDirty()
150
    {
151
        return $this->isDirty;
152
    }
153
154
    /**
155
     * initialize variables for the object
156 14
     *
157
     * @param string $key       key
158 14
     * @param int    $data_type set to one of Dtype::TYPE_XXX constants (set to Dtype::TYPE_OTHER
159
     *                           if no data type checking nor text sanitizing is required)
160
     * @param mixed  $value     value
161
     * @param bool   $required  require html form input?
162
     * @param mixed  $maxlength for Dtype::TYPE_TEXT_BOX type only
163
     * @param string $options   does this data have any select options?
164
     *
165
     * @return void
166
     */
167
    public function initVar($key, $data_type, $value = null, $required = false, $maxlength = null, $options = '')
168
    {
169
        $this->vars[$key] = array(
170
            'value' => $value,
171
            'required' => $required,
172
            'data_type' => $data_type,
173
            'maxlength' => $maxlength,
174 273
            'changed' => false,
175
            'options' => $options
176 273
        );
177 273
    }
178 273
179 273
    /**
180 273
     * assign a value to a variable
181
     *
182 273
     * @param string $key   name of the variable to assign
183
     * @param mixed  $value value to assign
184 273
     *
185
     * @return void
186
     */
187
    public function assignVar($key, $value)
188
    {
189 View Code Duplication
        if (isset($key) && isset($this->vars[$key])) {
190
            $this->vars[$key]['value'] = $value;
191
        }
192
    }
193
194 49
    /**
195
     * assign values to multiple variables in a batch
196 49
     *
197 49
     * @param array $var_arr associative array of values to assign
198
     *
199 49
     * @return void
200
     */
201
    public function assignVars($var_arr)
202
    {
203
        if (is_array($var_arr)) {
204
            foreach ($var_arr as $key => $value) {
205
                $this->assignVar($key, $value);
206
            }
207
        }
208 40
    }
209
210 40
    /**
211 40
     * assign a value to a variable
212 40
     *
213
     * @param string $key     name of the variable to assign
214
     * @param mixed  $value   value to assign
215 40
     *
216
     * @return void
217
     */
218
    public function setVar($key, $value)
219
    {
220
        if (!empty($key) && isset($value) && isset($this->vars[$key])) {
221
            $this->vars[$key]['value'] = $value;
222
            $this->vars[$key]['changed'] = true;
223
            $this->setDirty();
224
        }
225 27
    }
226
227 27
    /**
228 27
     * assign values to multiple variables in a batch
229 27
     *
230 27
     * @param array $var_arr associative array of values to assign
231
     *
232 27
     * @return void
233
     */
234
    public function setVars($var_arr)
235
    {
236
        if (is_array($var_arr)) {
237
            foreach ($var_arr as $key => $value) {
238
                $this->setVar($key, $value);
239
            }
240
        }
241 1
    }
242
243 1
    /**
244 1
     * unset variable(s) for the object
245 1
     *
246
     * @param mixed $var variable(s)
247
     *
248 1
     * @return bool
249
     */
250
    public function destroyVars($var)
251
    {
252
        if (empty($var)) {
253
            return true;
254
        }
255
        $var = !is_array($var) ? array($var) : $var;
256
        foreach ($var as $key) {
257 1
            if (!isset($this->vars[$key])) {
258
                continue;
259 1
            }
260 1
            $this->vars[$key]['changed'] = null;
261
        }
262
        return true;
263
    }
264
265
    /**
266
     * Assign values to multiple variables in a batch
267
     *
268
     * Meant for a CGI context:
269
     * - prefixed CGI args are considered save
270
     * - avoids polluting of namespace with CGI args
271
     *
272
     * @param mixed  $var_arr associative array of values to assign
273
     * @param string $pref    prefix (only keys starting with the prefix will be set)
274
     *
275
     * @return void
276
     */
277
    public function setFormVars($var_arr = null, $pref = 'xo_')
278
    {
279
        $len = strlen($pref);
280
        if (is_array($var_arr)) {
281
            foreach ($var_arr as $key => $value) {
282
                if ($pref == substr($key, 0, $len)) {
283
                    $this->setVar(substr($key, $len), $value);
284 1
                }
285
            }
286 1
        }
287 1
    }
288 1
289 1
    /**
290 1
     * returns all variables for the object
291
     *
292
     * @return array associative array of key->value pairs
293
     */
294 1
    public function getVars()
295
    {
296
        return $this->vars;
297
    }
298
299
    /**
300
     * Returns the values of the specified variables
301 26
     *
302
     * @param mixed  $keys     An array containing the names of the keys to retrieve, or null to get all of them
303 26
     * @param string $format   Format to use (see getVar)
304
     * @param int    $maxDepth Maximum level of recursion to use if some vars are objects themselves
305
     *
306
     * @return array associative array of key->value pairs
307
     */
308
    public function getValues($keys = null, $format = Dtype::FORMAT_SHOW, $maxDepth = 1)
309
    {
310
        if (!isset($keys)) {
311
            $keys = array_keys($this->vars);
312
        }
313
        $vars = array();
314
        if (is_array($keys)) {
315 3
            foreach ($keys as $key) {
316
                if (isset($this->vars[$key])) {
317 3
                    if (is_object($this->vars[$key]) && is_a($this->vars[$key], 'Xoops\Core\Kernel\XoopsObject')) {
318 3
                        if ($maxDepth) {
319
                            /* @var $obj XoopsObject */
320 3
                            $obj = $this->vars[$key];
321 3
                            $vars[$key] = $obj->getValues(null, $format, $maxDepth - 1);
322 3
                        }
323 3
                    } else {
324 3
                        $vars[$key] = $this->getVar($key, $format);
325
                    }
326
                }
327
            }
328
        }
329
        return $vars;
330
    }
331 3
332
    /**
333
     * returns a specific variable for the object in a proper format
334
     *
335
     * @param string $key    key of the object's variable to be returned
336 3
     * @param string $format format to use for the output
337
     *
338
     * @return mixed formatted value of the variable
339
     */
340
    public function getVar($key, $format = Dtype::FORMAT_SHOW)
341
    {
342
        $ret = null;
343
        if (!isset($this->vars[$key])) {
344
            return $ret;
345
        }
346
        $ret = Dtype::getVar($this, $key, $format);
347 181
        return $ret;
348
    }
349 181
350 181
    /**
351 1
     * clean values of all variables of the object for storage.
352
     *
353 181
     * @return bool true if successful
354 181
     */
355
    public function cleanVars()
356
    {
357
        $existing_errors = $this->getErrors();
358
        $this->errors = array();
359
        foreach ($this->vars as $k => $v) {
360
            if (!$v['changed']) {
361
            } else {
362 3
                $this->cleanVars[$k] = Dtype::cleanVar($this, $k);
363
            }
364 3
        }
365 3
        if (count($this->errors) > 0) {
366 3
            $this->errors = array_merge($existing_errors, $this->errors);
367 3
            return false;
368
        }
369 3
        // $this->_errors = array_merge($existing_errors, $this->_errors);
370
        $this->unsetDirty();
371
        return true;
372 3
    }
373
374
    /**
375
     * create a clone(copy) of the current object
376
     *
377 3
     * @return object clone
378 3
     */
379
    public function xoopsClone()
380
    {
381
        $clone = clone $this;
382
        return $clone;
383
    }
384
385
    /**
386
     * Adjust a newly cloned object
387
     */
388
    public function __clone()
389
    {
390
        // need this to notify the handler class that this is a newly created object
391
        $this->setNew();
392
    }
393
394
    /**
395
     * add an error
396
     *
397
     * @param string $err_str to add
398
     *
399
     * @return void
400
     */
401
    public function setErrors($err_str)
402
    {
403
        if (is_array($err_str)) {
404
            $this->errors = array_merge($this->errors, $err_str);
405
        } else {
406
            $this->errors[] = trim($err_str);
407
        }
408
    }
409
410
    /**
411
     * return the errors for this object as an array
412
     *
413
     * @return array an array of errors
414
     */
415
    public function getErrors()
416
    {
417
        return $this->errors;
418
    }
419
420
    /**
421
     * return the errors for this object as html
422
     *
423
     * @return string html listing the errors
424
     * @todo remove hardcoded HTML strings
425
     */
426
    public function getHtmlErrors()
427
    {
428
        $ret = '<h4>Errors</h4>';
429
        if (!empty($this->errors)) {
430
            foreach ($this->errors as $error) {
431
                $ret .= $error . '<br />';
432
            }
433
        } else {
434
            $ret .= 'None<br />';
435
        }
436
        return $ret;
437
    }
438
439
    /**
440
     * ArrayAccess methods
441
     */
442
443
    /**
444
     * offsetExists
445
     *
446
     * @param mixed $offset array key
447
     *
448
     * @return bool true if offset exists
449
     */
450
    public function offsetExists($offset)
451
    {
452
        return isset($this->vars[$offset]);
453
    }
454
455
    /**
456
     * offsetGet
457
     *
458 1
     * @param mixed $offset array key
459
     *
460 1
     * @return mixed value
461 1
     */
462
    public function offsetGet($offset)
463
    {
464
        return $this->getVar($offset);
465
    }
466
467 1
    /**
468
     * offsetSet
469
     *
470 1
     * @param mixed $offset array key
471 1
     * @param mixed $value
472
     *
473
     * @return void
474
     */
475
    public function offsetSet($offset, $value)
476
    {
477
        $this->setVar($offset, $value);
478
    }
479
480 2
    /**
481
     * offsetUnset
482 2
     *
483 1
     * @param mixed $offset array key
484
     *
485 2
     * @return void
486
     */
487 2
    public function offsetUnset($offset)
488
    {
489
        $this->vars[$offset]['value'] = null;
490
        $this->vars[$offset]['changed'] = true;
491
        $this->setDirty();
492
    }
493
}
494