Passed
Push — master ( f5f55f...a1640c )
by Kanstantsin
03:13
created

BaseObject::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace tkanstantsin\fileupload\model;
4
5
use tkanstantsin\fileupload\config\InvalidConfigException;
6
7
/**
8
 * Class BaseObject
9
 */
10
class BaseObject implements IConfigurable
11
{
12
    /**
13
     * Util constructor.
14
     * @param array $config
15
     * @throws InvalidConfigException
16
     */
17
    public function __construct(array $config = [])
18
    {
19
        Container::configure($this, $config);
20
        $this->init();
21
    }
22
23
    /**
24
     * Initialize object
25
     * E.g. instantiate some variables or check correct data
26
     */
27
    public function init(): void
28
    {
29
    }
30
31
    /**
32
     * Returns the value of an object property.
33
     *
34
     * Do not call this method directly as it is a PHP magic method that
35
     * will be implicitly called when executing `$value = $object->property;`.
36
     * @param string $name the property name
37
     * @return mixed the property value
38
     * @throws \RuntimeException
39
     * @see __set()
40
     */
41
    public function __get($name)
42
    {
43
        $getter = 'get' . $name;
44
        if (method_exists($this, $getter)) {
45
            return $this->$getter();
46
        } elseif (method_exists($this, 'set' . $name)) {
47
            throw new \RuntimeException('Getting write-only property: ' . \get_class($this) . '::' . $name);
48
        }
49
50
        throw new \RuntimeException('Getting unknown property: ' . \get_class($this) . '::' . $name);
51
    }
52
53
    /**
54
     * Sets value of an object property.
55
     *
56
     * Do not call this method directly as it is a PHP magic method that
57
     * will be implicitly called when executing `$object->property = $value;`.
58
     * @param string $name the property name or the event name
59
     * @param mixed $value the property value
60
     * @throws \RuntimeException
61
     * @see __get()
62
     */
63
    public function __set($name, $value)
64
    {
65
        $setter = 'set' . $name;
66
        if (method_exists($this, $setter)) {
67
            $this->$setter($value);
68
        } elseif (method_exists($this, 'get' . $name)) {
69
            throw new \RuntimeException('Setting read-only property: ' . \get_class($this) . '::' . $name);
70
        } else {
71
            throw new \RuntimeException('Setting unknown property: ' . \get_class($this) . '::' . $name);
72
        }
73
    }
74
75
    /**
76
     * Checks if a property is set, i.e. defined and not null.
77
     *
78
     * Do not call this method directly as it is a PHP magic method that
79
     * will be implicitly called when executing `isset($object->property)`.
80
     *
81
     * Note that if the property is not defined, false will be returned.
82
     * @param string $name the property name or the event name
83
     * @return bool whether the named property is set (not null).
84
     * @see http://php.net/manual/en/function.isset.php
85
     */
86
    public function __isset($name): bool
87
    {
88
        $getter = 'get' . $name;
89
        if (method_exists($this, $getter)) {
90
            return $this->$getter() !== null;
91
        }
92
93
        return false;
94
    }
95
96
    /**
97
     * Sets an object property to null.
98
     *
99
     * Do not call this method directly as it is a PHP magic method that
100
     * will be implicitly called when executing `unset($object->property)`.
101
     *
102
     * Note that if the property is not defined, this method will do nothing.
103
     * If the property is read-only, it will throw an exception.
104
     * @param string $name the property name
105
     * @throws \RuntimeException
106
     * @see http://php.net/manual/en/function.unset.php
107
     */
108
    public function __unset($name)
109
    {
110
        $setter = 'set' . $name;
111
        if (method_exists($this, $setter)) {
112
            $this->$setter(null);
113
        } elseif (method_exists($this, 'get' . $name)) {
114
            throw new \RuntimeException('Unsetting read-only property: ' . \get_class($this) . '::' . $name);
115
        }
116
    }
117
}