BasicObject::__call()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 10
Ratio 66.67 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 10
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Asymptix\core;
4
5
/**
6
 * Basic Object class.
7
 *
8
 * @category Asymptix PHP Framework
9
 * @author Dmytro Zarezenko <[email protected]>
10
 * @copyright (c) 2009 - 2018, Dmytro Zarezenko
11
 *
12
 * @git https://github.com/Asymptix/Framework
13
 * @license http://opensource.org/licenses/MIT
14
 */
15
abstract class BasicObject {
16
17
    /**
18
     * List of the database entity fields.
19
     *
20
     * @var array
21
     */
22
    protected $fieldsList;
23
24
    /**
25
     * List of fields aliases.
26
     *
27
     * @var array
28
     */
29
    protected $fieldsAliases = [];
30
31
    /**
32
     * Sets values of the object fields.
33
     *
34
     * @param array<string> $valuesList List of the values.
35
     *
36
     * @return mixed Number of added values or FALSE.
37
     */
38
    public function setFieldsValues($valuesList) {
39
        if (is_array($valuesList)) {
40
            $count = 0;
41
            foreach ($this->fieldsList as $fieldName => &$fieldValue) {
42
                if (isset($valuesList[$fieldName])) {
43
                    $newValue = $valuesList[$fieldName];
44
                    if (empty($newValue)) {
45
                        $fieldValue = self::getEmptyValue($fieldValue, $newValue);
46
                    } else {
47
                        $fieldValue = $newValue;
48
                    }
49
                    $count ++;
50
                } elseif (!empty($this->fieldsAliases)) { // look up for the field aliases
51
                    $fieldAliases = array_keys($this->fieldsAliases, $fieldName);
52
                    if (!empty($fieldAliases)) {
53
                        foreach ($fieldAliases as $alias) {
54
                            if (isset($valuesList[$alias])) {
55
                                $newValue = $valuesList[$alias];
56
                                if (empty($newValue)) {
57
                                    $fieldValue = self::getEmptyValue($fieldValue, $newValue);
58
                                } else {
59
                                    $fieldValue = $newValue;
60
                                }
61
                                $count ++;
62
63
                                break;
64
                            }
65
                        }
66
                    }
67
                }
68
            }
69
70
            return $count;
71
        } else {
72
            return false;
73
        }
74
    }
75
76
    /**
77
     * Sets value to the object's field.
78
     *
79
     * @param string $fieldName Name of the field.
80
     * @param mixed $fieldValue Value of the field.
81
     *
82
     * @return object Object itself on success (for the method chaining support).
83
     * @throws \Exception If object has no field with such name.
84
     */
85 View Code Duplication
    public function setFieldValue($fieldName, $fieldValue) {
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...
86
        if (isset($this->fieldsAliases[$fieldName])) {
87
            $fieldName = $this->fieldsAliases[$fieldName];
88
        }
89
90
        if (array_key_exists($fieldName, $this->fieldsList)) {
91
            $this->fieldsList[$fieldName] = $fieldValue;
92
93
            return $this;
94
        } else {
95
            throw new \Exception("Object '" . get_class($this) . "' hasn't field '" . $fieldName . "'");
96
        }
97
    }
98
99
    /**
100
     * Returns fields list array.
101
     *
102
     * @param bool $withAliases If this flag is `true` then we will have fields
103
     *           aliases in the result array as well.
104
     *
105
     * @return array
106
     */
107
    public function getFieldsList($withAliases = false) {
108
        if ($withAliases && !empty($this->fieldsAliases)) {
109
            $fieldsList = $this->fieldsList;
110
            foreach (array_keys($this->fieldsAliases) as $alias) {
111
                $fieldsList[$alias] = $this->getFieldValue($alias);
112
            }
113
114
            return $fieldsList;
115
        }
116
117
        return array_map(function ($e) {
118
            if (is_string($e) && !is_null($e)) {
119
                return stripslashes($e);
120
            }
121
122
            return $e;
123
        }, $this->fieldsList);
124
    }
125
126
    /**
127
     * Returns value of the field by it's name or alias.
128
     *
129
     * @param string $fieldName Field name or alias.
130
     *
131
     * @return mixed
132
     * @throws \Exception If object doesn't have this field or alias.
133
     */
134 View Code Duplication
    public function getFieldValue($fieldName) {
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...
135
        if (isset($this->fieldsAliases[$fieldName])) {
136
            $fieldName = $this->fieldsAliases[$fieldName];
137
        }
138
139
        if (array_key_exists($fieldName, $this->fieldsList)) {
140
            return stripslashes($this->fieldsList[$fieldName]);
141
        } else {
142
            throw new \Exception("Object '" . get_class($this) . "' hasn't field '" . $fieldName . "'");
143
        }
144
    }
145
146
    /**
147
     * Returns type custed new empty field value.
148
     *
149
     * @param mixed $fieldValue Current field value.
150
     * @param mixed $newValue New value.
151
     *
152
     * @return mixed
153
     */
154
    private static function getEmptyValue($fieldValue, $newValue) {
155
        if (is_int($fieldValue)) {
156
            return 0;
157
        } elseif (is_string($fieldValue)) {
158
            return "";
159
        } elseif (is_null($fieldValue)) {
160
            return null;
161
        }
162
163
        return $newValue;
164
    }
165
166
    /**
167
     * Shows current object in structure view in the browser.
168
     */
169
    public function show() {
170
        print("<pre>");
171
        print_r($this);
172
        print("</pre>");
173
    }
174
175
    /**
176
     * Returns object's field name by getter/setter method name.
177
     *
178
     * @param string $methodNameFragment Method name fragment without 'get' or
179
     *            'set' prefix.
180
     * @return string Corresponded field name.
181
     */
182
    protected function getFieldName($methodNameFragment) {
183
        return lcfirst($methodNameFragment);
184
    }
185
186
    /**
187
     * Magic method to wrap getters and setters with own methods.
188
     *
189
     * @param string $methodName Name of the method.
190
     * @param array $methodParams Array of method parameters.
191
     *
192
     * @return mixed
193
     * @throws \Exception If some method is invalid or not exists.
194
     */
195
    public function __call($methodName, $methodParams) {
196
        $method = substr($methodName, 0, 3);
197
        $fieldName = $this->getFieldName(substr($methodName, 3));
198
199 View Code Duplication
        switch ($method) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
200
            case ("set"):
201
                $fieldValue = $methodParams[0];
202
203
                return $this->setFieldValue($fieldName, $fieldValue);
204
            case ("get"):
205
                return $this->getFieldValue($fieldName);
206
            default:
207
                throw new \Exception("No such method in the Object class.");
208
        }
209
    }
210
211
    /**
212
     * Magic method to wrap setters as fields values assignment.
213
     *
214
     * @param string $fieldName Name of the field.
215
     * @param mixed $fieldValue Value of the field.
216
     *
217
     * @return mixed The return value of the callback, or FALSE on error.
218
     */
219
    public function __set($fieldName, $fieldValue) {
220
        return call_user_func_array([$this, "set" . ucfirst($fieldName)], [$fieldValue]);
221
    }
222
223
    /**
224
     * Magic method to wrap getters as fields values calls.
225
     *
226
     * @param string $fieldName Name of the field.
227
     *
228
     * @return mixed The return value of the callback, or FALSE on error.
229
     */
230
    public function __get($fieldName) {
231
        return call_user_func_array([$this, "get" . ucfirst($fieldName)], []);
232
    }
233
234
}
235