Passed
Push — 2.2 ( 6618aa...1cb372 )
by Paweł
01:51 queued 19s
created

BaseObject::className()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\base;
9
10
use Yii;
11
12
/**
13
 * BaseObject is the base class that implements the *property* feature.
14
 *
15
 * A property is defined by a getter method (e.g. `getLabel`), and/or a setter method (e.g. `setLabel`). For example,
16
 * the following getter and setter methods define a property named `label`:
17
 *
18
 * ```php
19
 * private $_label;
20
 *
21
 * public function getLabel()
22
 * {
23
 *     return $this->_label;
24
 * }
25
 *
26
 * public function setLabel($value)
27
 * {
28
 *     $this->_label = $value;
29
 * }
30
 * ```
31
 *
32
 * Property names are *case-insensitive*.
33
 *
34
 * A property can be accessed like a member variable of an object. Reading or writing a property will cause the invocation
35
 * of the corresponding getter or setter method. For example,
36
 *
37
 * ```php
38
 * // equivalent to $label = $object->getLabel();
39
 * $label = $object->label;
40
 * // equivalent to $object->setLabel('abc');
41
 * $object->label = 'abc';
42
 * ```
43
 *
44
 * If a property has only a getter method and has no setter method, it is considered as *read-only*. In this case, trying
45
 * to modify the property value will cause an exception.
46
 *
47
 * One can call [[hasProperty()]], [[canGetProperty()]] and/or [[canSetProperty()]] to check the existence of a property.
48
 *
49
 * Besides the property feature, BaseObject also introduces an important object initialization life cycle. In particular,
50
 * creating an new instance of BaseObject or its derived class will involve the following life cycles sequentially:
51
 *
52
 * 1. the class constructor is invoked;
53
 * 2. object properties are initialized according to the given configuration;
54
 * 3. the `init()` method is invoked.
55
 *
56
 * In the above, both Step 2 and 3 occur at the end of the class constructor. It is recommended that
57
 * you perform object initialization in the `init()` method because at that stage, the object configuration
58
 * is already applied.
59
 *
60
 * In order to ensure the above life cycles, if a child class of BaseObject needs to override the constructor,
61
 * it should be done like the following:
62
 *
63
 * ```php
64
 * public function __construct($param1, $param2, ..., $config = [])
65
 * {
66
 *     ...
67
 *     parent::__construct($config);
68
 * }
69
 * ```
70
 *
71
 * That is, a `$config` parameter (defaults to `[]`) should be declared as the last parameter
72
 * of the constructor, and the parent implementation should be called at the end of the constructor.
73
 *
74
 * @author Qiang Xue <[email protected]>
75
 * @since 2.0.13
76
 */
77
class BaseObject implements Configurable
78
{
79
    /**
80
     * Constructor.
81
     *
82
     * The default implementation does two things:
83
     *
84
     * - Initializes the object with the given configuration `$config`.
85
     * - Call [[init()]].
86
     *
87
     * If this method is overridden in a child class, it is recommended that
88
     *
89
     * - the last parameter of the constructor is a configuration array, like `$config` here.
90
     * - call the parent implementation at the end of the constructor.
91
     *
92
     * @param array $config name-value pairs that will be used to initialize the object properties
93
     */
94 2839
    public function __construct($config = [])
95
    {
96 2839
        if (!empty($config)) {
97 2532
            Yii::configure($this, $config);
98
        }
99 2839
        $this->init();
100
    }
101
102
    /**
103
     * Initializes the object.
104
     * This method is invoked at the end of the constructor after the object is initialized with the
105
     * given configuration.
106
     */
107 2350
    public function init()
108
    {
109 2350
    }
110
111
    /**
112
     * Returns the value of an object property.
113
     *
114
     * Do not call this method directly as it is a PHP magic method that
115
     * will be implicitly called when executing `$value = $object->property;`.
116
     * @param string $name the property name
117
     * @return mixed the property value
118
     * @throws UnknownPropertyException if the property is not defined
119
     * @throws InvalidCallException if the property is write-only
120
     * @see __set()
121
     */
122 50
    public function __get($name)
123
    {
124 50
        $getter = 'get' . $name;
125 50
        if (method_exists($this, $getter)) {
126 49
            return $this->$getter();
127 2
        } elseif (method_exists($this, 'set' . $name)) {
128 1
            throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
129
        }
130
131 1
        throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
132
    }
133
134
    /**
135
     * Sets value of an object property.
136
     *
137
     * Do not call this method directly as it is a PHP magic method that
138
     * will be implicitly called when executing `$object->property = $value;`.
139
     * @param string $name the property name or the event name
140
     * @param mixed $value the property value
141
     * @throws UnknownPropertyException if the property is not defined
142
     * @throws InvalidCallException if the property is read-only
143
     * @see __get()
144
     */
145 12
    public function __set($name, $value)
146
    {
147 12
        $setter = 'set' . $name;
148 12
        if (method_exists($this, $setter)) {
149 11
            $this->$setter($value);
150 2
        } elseif (method_exists($this, 'get' . $name)) {
151 1
            throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
152
        } else {
153 1
            throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
154
        }
155
    }
156
157
    /**
158
     * Checks if a property is set, i.e. defined and not null.
159
     *
160
     * Do not call this method directly as it is a PHP magic method that
161
     * will be implicitly called when executing `isset($object->property)`.
162
     *
163
     * Note that if the property is not defined, false will be returned.
164
     * @param string $name the property name or the event name
165
     * @return bool whether the named property is set (not null).
166
     * @see https://www.php.net/manual/en/function.isset.php
167
     */
168 2
    public function __isset($name)
169
    {
170 2
        $getter = 'get' . $name;
171 2
        if (method_exists($this, $getter)) {
172 2
            return $this->$getter() !== null;
173
        }
174
175 1
        return false;
176
    }
177
178
    /**
179
     * Sets an object property to null.
180
     *
181
     * Do not call this method directly as it is a PHP magic method that
182
     * will be implicitly called when executing `unset($object->property)`.
183
     *
184
     * Note that if the property is not defined, this method will do nothing.
185
     * If the property is read-only, it will throw an exception.
186
     * @param string $name the property name
187
     * @throws InvalidCallException if the property is read only.
188
     * @see https://www.php.net/manual/en/function.unset.php
189
     */
190 2
    public function __unset($name)
191
    {
192 2
        $setter = 'set' . $name;
193 2
        if (method_exists($this, $setter)) {
194 1
            $this->$setter(null);
195 1
        } elseif (method_exists($this, 'get' . $name)) {
196 1
            throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
197
        }
198
    }
199
200
    /**
201
     * Calls the named method which is not a class method.
202
     *
203
     * Do not call this method directly as it is a PHP magic method that
204
     * will be implicitly called when an unknown method is being invoked.
205
     * @param string $name the method name
206
     * @param array $params method parameters
207
     * @throws UnknownMethodException when calling unknown method
208
     * @return mixed the method return value
209
     */
210 1
    public function __call($name, $params)
211
    {
212 1
        throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
213
    }
214
215
    /**
216
     * Returns a value indicating whether a property is defined.
217
     *
218
     * A property is defined if:
219
     *
220
     * - the class has a getter or setter method associated with the specified name
221
     *   (in this case, property name is case-insensitive);
222
     * - the class has a member variable with the specified name (when `$checkVars` is true);
223
     *
224
     * @param string $name the property name
225
     * @param bool $checkVars whether to treat member variables as properties
226
     * @return bool whether the property is defined
227
     * @see canGetProperty()
228
     * @see canSetProperty()
229
     */
230 1
    public function hasProperty($name, $checkVars = true)
231
    {
232 1
        return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
233
    }
234
235
    /**
236
     * Returns a value indicating whether a property can be read.
237
     *
238
     * A property is readable if:
239
     *
240
     * - the class has a getter method associated with the specified name
241
     *   (in this case, property name is case-insensitive);
242
     * - the class has a member variable with the specified name (when `$checkVars` is true);
243
     *
244
     * @param string $name the property name
245
     * @param bool $checkVars whether to treat member variables as properties
246
     * @return bool whether the property can be read
247
     * @see canSetProperty()
248
     */
249 8
    public function canGetProperty($name, $checkVars = true)
250
    {
251 8
        return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
252
    }
253
254
    /**
255
     * Returns a value indicating whether a property can be set.
256
     *
257
     * A property is writable if:
258
     *
259
     * - the class has a setter method associated with the specified name
260
     *   (in this case, property name is case-insensitive);
261
     * - the class has a member variable with the specified name (when `$checkVars` is true);
262
     *
263
     * @param string $name the property name
264
     * @param bool $checkVars whether to treat member variables as properties
265
     * @return bool whether the property can be written
266
     * @see canGetProperty()
267
     */
268 7
    public function canSetProperty($name, $checkVars = true)
269
    {
270 7
        return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
271
    }
272
273
    /**
274
     * Returns a value indicating whether a method is defined.
275
     *
276
     * The default implementation is a call to php function `method_exists()`.
277
     * You may override this method when you implemented the php magic method `__call()`.
278
     * @param string $name the method name
279
     * @return bool whether the method is defined
280
     */
281 12
    public function hasMethod($name)
282
    {
283 12
        return method_exists($this, $name);
284
    }
285
286
    /**
287
     * Returns the fully qualified name of this class.
288
     *
289
     * @return string the fully qualified name of this class.
290
     */
291 1
    public static function className(): string
292
    {
293 1
        return static::class;
294
    }
295
}
296