AbstractFormat   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 230
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 26
lcom 0
cbo 3
dl 0
loc 230
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A __get() 0 13 3
A __set() 0 11 3
A __isset() 0 9 2
A __unset() 0 9 3
A __call() 0 4 1
A __toString() 0 4 1
A init() 0 3 1
A hasProperty() 0 4 2
A canGetProperty() 0 4 3
A canSetProperty() 0 4 3
A hasMethod() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/qrcode-library project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\QrCode\Format;
13
14
use Da\QrCode\Contracts\FormatInterface;
15
use Da\QrCode\Exception\InvalidCallException;
16
use Da\QrCode\Exception\UnknownMethodException;
17
use Da\QrCode\Exception\UnknownPropertyException;
18
19
/**
20
 * Abstract Class FormatAbstract for all formats
21
 *
22
 * @author Antonio Ramirez <[email protected]>
23
 * @link https://2amigos.us/
24
 * @package Da\QrCode\Format
25
 */
26
abstract class AbstractFormat implements FormatInterface
27
{
28
    /**
29
     * Constructor.
30
     * The default implementation does two things:
31
     *
32
     * - Initializes the object with the given configuration `$config`.
33
     * - Call [[init()]].
34
     *
35
     * If this method is overridden in a child class, it is recommended that
36
     *
37
     * - the last parameter of the constructor is a configuration array, like `$config` here.
38
     * - call the parent implementation at the end of the constructor.
39
     *
40
     * @param array $config name-value pairs that will be used to initialize the object properties
41
     */
42
    public function __construct($config = [])
43
    {
44
        if (!empty($config)) {
45
            foreach ($config as $name => $value) {
46
                $this->$name = $value;
47
            }
48
        }
49
        $this->init();
50
    }
51
52
    /**
53
     * Returns the value of an object property.
54
     *
55
     * Do not call this method directly as it is a PHP magic method that
56
     * will be implicitly called when executing `$value = $object->property;`.
57
     *
58
     * @param  string $name the property name
59
     *
60
     * @throws UnknownPropertyException if the property is not defined
61
     * @throws InvalidCallException     if the property is write-only
62
     * @return mixed                    the property value
63
     * @see __set()
64
     */
65
    public function __get($name)
66
    {
67
        $getter = 'get' . $name;
68
        if (method_exists($this, $getter)) {
69
            return $this->$getter();
70
        }
71
72
        if (method_exists($this, 'set' . $name)) {
73
            throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
74
        }
75
76
        throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
77
    }
78
79
    /**
80
     * Sets value of an object property.
81
     *
82
     * Do not call this method directly as it is a PHP magic method that
83
     * will be implicitly called when executing `$object->property = $value;`.
84
     *
85
     * @param  string $name the property name or the event name
86
     * @param  mixed $value the property value
87
     *
88
     * @throws UnknownPropertyException if the property is not defined
89
     * @throws InvalidCallException     if the property is read-only
90
     * @see __get()
91
     */
92
    public function __set($name, $value)
93
    {
94
        $setter = 'set' . $name;
95
        if (method_exists($this, $setter)) {
96
            $this->$setter($value);
97
        } elseif (method_exists($this, 'get' . $name)) {
98
            throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
99
        } else {
100
            throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
101
        }
102
    }
103
104
    /**
105
     * Checks if a property is set, i.e. defined and not null.
106
     *
107
     * Do not call this method directly as it is a PHP magic method that
108
     * will be implicitly called when executing `isset($object->property)`.
109
     *
110
     * Note that if the property is not defined, false will be returned.
111
     *
112
     * @param  string $name the property name or the event name
113
     *
114
     * @return bool   whether the named property is set (not null).
115
     * @see http://php.net/manual/en/function.isset.php
116
     */
117
    public function __isset($name)
118
    {
119
        $getter = 'get' . $name;
120
        if (method_exists($this, $getter)) {
121
            return $this->$getter() !== null;
122
        }
123
124
        return false;
125
    }
126
127
    /**
128
     * Sets an object property to null.
129
     *
130
     * Do not call this method directly as it is a PHP magic method that
131
     * will be implicitly called when executing `unset($object->property)`.
132
     *
133
     * Note that if the property is not defined, this method will do nothing.
134
     * If the property is read-only, it will throw an exception.
135
     *
136
     * @param  string $name the property name
137
     *
138
     * @throws InvalidCallException if the property is read only.
139
     * @see http://php.net/manual/en/function.unset.php
140
     */
141
    public function __unset($name)
142
    {
143
        $setter = 'set' . $name;
144
        if (method_exists($this, $setter)) {
145
            $this->$setter(null);
146
        } elseif (method_exists($this, 'get' . $name)) {
147
            throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
148
        }
149
    }
150
151
    /**
152
     * Calls the named method which is not a class method.
153
     *
154
     * Do not call this method directly as it is a PHP magic method that
155
     * will be implicitly called when an unknown method is being invoked.
156
     *
157
     * @param  string $name the method name
158
     * @param  array $params method parameters
159
     *
160
     * @throws UnknownMethodException when calling unknown method
161
     * @return mixed                  the method return value
162
     */
163
    public function __call($name, $params)
164
    {
165
        throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
166
    }
167
168
    /**
169
     * @return string the string representation of the object
170
     */
171
    public function __toString()
172
    {
173
        return $this->getText();
174
    }
175
176
    /**
177
     * Initialization method
178
     */
179
    public function init(): void
180
    {
181
    }
182
183
    /**
184
     * Returns a value indicating whether a property is defined.
185
     * A property is defined if:
186
     *
187
     * - the class has a getter or setter method associated with the specified name
188
     *   (in this case, property name is case-insensitive);
189
     * - the class has a member variable with the specified name (when `$checkVars` is true);
190
     *
191
     * @param  string $name the property name
192
     * @param  bool $checkVars whether to treat member variables as properties
193
     *
194
     * @return bool   whether the property is defined
195
     * @see canGetProperty()
196
     * @see canSetProperty()
197
     */
198
    public function hasProperty($name, $checkVars = true): bool
199
    {
200
        return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
201
    }
202
203
    /**
204
     * Returns a value indicating whether a property can be read.
205
     * A property is readable if:
206
     *
207
     * - the class has a getter method associated with the specified name
208
     *   (in this case, property name is case-insensitive);
209
     * - the class has a member variable with the specified name (when `$checkVars` is true);
210
     *
211
     * @param  string $name the property name
212
     * @param  bool $checkVars whether to treat member variables as properties
213
     *
214
     * @return bool   whether the property can be read
215
     * @see canSetProperty()
216
     */
217
    public function canGetProperty($name, $checkVars = true): bool
218
    {
219
        return method_exists($this, 'get' . $name) || ($checkVars && property_exists($this, $name));
220
    }
221
222
    /**
223
     * Returns a value indicating whether a property can be set.
224
     * A property is writable if:
225
     *
226
     * - the class has a setter method associated with the specified name
227
     *   (in this case, property name is case-insensitive);
228
     * - the class has a member variable with the specified name (when `$checkVars` is true);
229
     *
230
     * @param  string $name the property name
231
     * @param  bool $checkVars whether to treat member variables as properties
232
     *
233
     * @return bool   whether the property can be written
234
     * @see canGetProperty()
235
     */
236
    public function canSetProperty($name, $checkVars = true): bool
237
    {
238
        return method_exists($this, 'set' . $name) || ($checkVars && property_exists($this, $name));
239
    }
240
241
    /**
242
     * Returns a value indicating whether a method is defined.
243
     *
244
     * The default implementation is a call to php function `method_exists()`.
245
     * You may override this method when you implemented the php magic method `__call()`.
246
     *
247
     * @param  string $name the method name
248
     *
249
     * @return bool   whether the method is defined
250
     */
251
    public function hasMethod($name): bool
252
    {
253
        return method_exists($this, $name);
254
    }
255
}
256