Parameters::extend()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace XoopsModules\Oledrion;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * oledrion
17
 *
18
 * @copyright   {@link https://xoops.org/ XOOPS Project}
19
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
20
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
21
 */
22
23
/**
24
 * Class used for parameters passing to classes methods
25
 *
26
 * @copyright          Hervé Thouzard (http://www.herve-thouzard.com/)
27
 * @license            http://www.fsf.org/copyleft/gpl.html GNU public license
28
 * @author             Hervé Thouzard (http://www.herve-thouzard.com/)
29
 *
30
 * Example :
31
 *
32
 * // Instantiate it like this
33
 * $param = new Oledrion\Parameters();
34
 *
35
 * // Create several parameters in one time:
36
 * $param->setLimit(10)->setSort('manu_name');
37
 *
38
 * // Set a parameter with the array convention:
39
 * $param['sort'] = 'first_name';
40
 *
41
 * // Set a parameter as a class property:
42
 * $param->order = 'DESC';
43
 *
44
 * // Display a parameter, first way:
45
 * echo "<br>value=".$param['sort'];    // DESC
46
 *
47
 * // Another method to show it, as a class method:
48
 * echo $param->limit();    // 10
49
 *
50
 * // Set the default values
51
 * $newParameters = $param->extend(new Oledrion\Parameters(array('sort' => 'firstName', 'start' => 0, 'limit' => 15, 'showAll' => true)));
52
 */
53
54
use XoopsModules\Oledrion;
55
56
/**
57
 * Class Parameters
58
 */
59
class Parameters extends \ArrayObject
60
{
61
    /**
62
     * Allows you to value an index of the class as if it were a property of the class
63
     *
64
     * @example $record->field_name = 'my channel'
65
     *
66
     * @param  string $key   The name of the field to be treated
67
     * @param  mixed  $value The value to assign
68
     * @return \XoopsModules\Oledrion\Parameters
69
     */
70
    public function __set($key, $value)
71
    {
72
        parent::offsetSet($key, $value);
73
74
        return $this;
75
    }
76
77
    /**
78
     * Valuation of an index of the class using a function call based on the following principle:
79
     *         $maClasse->setLimit(10);
80
     * It is possible to chain it like this : $maClasse->setStart(0)->setLimit(10);
81
     *
82
     * @param  string $method
83
     * @param  mixed  $args
84
     * @return Parameters|\ArrayObject
85
     */
86
    public function __call($method, $args)
87
    {
88
        if (0 === mb_strpos($method, 'set')) {
89
            parent::offsetSet(mb_strtolower($method[3]) . mb_substr($method, 4), $args[0]);
90
91
            return $this;
92
        }
93
94
        // Value display
95
96
        return parent::offsetGet($method);
97
    }
98
99
    /**
100
     * Method that tries to do the same thing as jQuery's extend() method
101
     *
102
     * We pass the default values ​​that we expect and the method compares them with the current values
103
     * If values ​​are missing, they are added
104
     *
105
     * @param self $defaultValues
106
     * @return Parameters
107
     */
108
    public function extend(self $defaultValues)
109
    {
110
        $result = new self();
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
111
        $result = $this;
112
        foreach ($defaultValues as $key => $value) {
113
            if (!isset($result[$key])) {
114
                $result[$key] = $value;
115
            }
116
        }
117
118
        return $result;
119
    }
120
}
121