Completed
Push — master ( 7a5aed...4799e1 )
by Kacper
06:25
created

Accessors::applyOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
c 1
b 0
f 1
1
<?php
2
3
namespace Kadet\Xmpp\Utils;
4
5
use Kadet\Xmpp\Exception\ReadOnlyException;
6
use Kadet\Xmpp\Exception\WriteOnlyException;
7
8
trait Accessors
9
{
10
    private $_magic = [];
11
12 26
    public function __get($property)
13
    {
14 26
        $getter = 'get' . ucfirst($property);
15
16 26
        if (method_exists($this, $getter)) {
17 26
            return $this->$getter();
18
        } elseif (method_exists($this, 'set' . ucfirst($property))) {
19
            throw new WriteOnlyException("Property \$$property is write-only, which is rather strange.");
20
        } else {
21
            return $this->_get($property);
22
        }
23
    }
24
25 12
    public function __set($property, $value)
26
    {
27 12
        $setter = 'set' . ucfirst($property);
28
29 12
        if (method_exists($this, $setter)) {
30 12
            $this->$setter($value);
31
        } elseif (method_exists($this, 'get' . ucfirst($property))) {
32
            throw new ReadOnlyException("Property \$$property is read-only.");
33
        } else {
34
            $this->_magic[$property] = $value;
35
        }
36 12
    }
37
38
    public function __isset($property)
39
    {
40
        return $this->$property !== null;
41
    }
42
43
    public function __unset($property)
44
    {
45
        $this->$property = null;
46
    }
47
48
    public function _get($property)
49
    {
50
        return isset($this->_magic[$property]) ? $this->_magic[$property] : null;
51
    }
52
53
    public function _set($property, $value)
54
    {
55
        $this->_magic[$property] = $value;
56
    }
57
58
    public function applyOptions(array $options)
59
    {
60
        foreach ($options as $key => $value) {
61
            $this->$key = $value;
62
        }
63
    }
64
}
65