AbstractCriteria::canSetProperty()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/hubspot/blob/master/LICENSE.md
6
 * @link       https://github.com/flipbox/hubspot
7
 */
8
9
namespace Flipbox\HubSpot\Criteria;
10
11
use Flipbox\Skeleton\Exceptions\InvalidCallException;
12
use Flipbox\Skeleton\Exceptions\UnknownMethodException;
13
use Flipbox\Skeleton\Exceptions\UnknownPropertyException;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 2.0.0
18
 */
19
abstract class AbstractCriteria
20
{
21
    use LoggerTrait;
22
23
    /**
24
     * Constructor
25
     *
26
     * @param array $config
27
     */
28
    public function __construct(array $config = [])
29
    {
30
        $this->populate($config);
31
    }
32
33
    /**
34
     * @param array $properties
35
     * @return static
36
     */
37
    public function populate(array $properties = [])
38
    {
39
        if (!empty($properties)) {
40
            foreach ($properties as $name => $value) {
41
                if ($this->canSetProperty($name)) {
42
                    $this->$name = $value;
43
                }
44
            }
45
        }
46
47
        return $this;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function hasProperty($name, $checkVars = true): bool
54
    {
55
        return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function canGetProperty($name, $checkVars = true): bool
62
    {
63
        return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function canSetProperty($name, $checkVars = true): bool
70
    {
71
        return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function hasMethod($name): bool
78
    {
79
        return method_exists($this, $name);
80
    }
81
82
    /**
83
     * Returns the value of an object property.
84
     *
85
     * @param $name
86
     * @return mixed
87
     * @throws InvalidCallException
88
     * @throws UnknownPropertyException
89
     */
90
    public function __get($name)
91
    {
92
        $getter = 'get' . $name;
93
        if (method_exists($this, $getter)) {
94
            return $this->$getter();
95
        } elseif (method_exists($this, 'set' . $name)) {
96
            throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
97
        } else {
98
            throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
99
        }
100
    }
101
102
    /**
103
     * Sets value of an object property.
104
     *
105
     * @param $name
106
     * @param $value
107
     * @throws InvalidCallException
108
     * @throws UnknownPropertyException
109
     */
110
    public function __set($name, $value)
111
    {
112
        $setter = 'set' . $name;
113
        if (method_exists($this, $setter)) {
114
            $this->$setter($value);
115
        } elseif (method_exists($this, 'get' . $name)) {
116
            throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
117
        } else {
118
            throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
119
        }
120
    }
121
122
    /**
123
     * Checks if a property is set, i.e. defined and not null.
124
     *
125
     * @param $name
126
     * @return bool
127
     */
128
    public function __isset($name)
129
    {
130
        $getter = 'get' . $name;
131
        if (method_exists($this, $getter)) {
132
            return $this->$getter() !== null;
133
        } else {
134
            return false;
135
        }
136
    }
137
138
    /**
139
     * Sets an object property to null.
140
     *
141
     * @param $name
142
     * @throws InvalidCallException
143
     */
144
    public function __unset($name)
145
    {
146
        $setter = 'set' . $name;
147
        if (method_exists($this, $setter)) {
148
            $this->$setter(null);
149
        } elseif (method_exists($this, 'get' . $name)) {
150
            throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
151
        }
152
    }
153
154
    /**
155
     * Calls the named method which is not a class method.
156
     *
157
     * @param $name
158
     * @param $params
159
     * @throws UnknownMethodException
160
     */
161
    public function __call($name, $params)
162
    {
163
        throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
164
    }
165
}
166