Completed
Push — master ( 5dbfda...a73b65 )
by Kacper
03:37
created

Accessors::__isset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Nucleus - XMPP Library for PHP
4
 *
5
 * Copyright (C) 2016, Some rights reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Xmpp\Utils;
17
18
use Kadet\Xmpp\Exception\ReadOnlyException;
19
use Kadet\Xmpp\Exception\WriteOnlyException;
20
21
trait Accessors
22
{
23
    private $_magic = [];
24
25 14
    public function __get($property)
26
    {
27 14
        $getter = 'get' . ucfirst($property);
28
29 14
        if (method_exists($this, $getter)) {
30 14
            return $this->$getter();
31
        } elseif (method_exists($this, 'set' . ucfirst($property))) {
32
            throw new WriteOnlyException("Property \$$property is write-only, which is rather strange. Maybe you should write custom getter with proper explanation?");
33
        } else {
34
            return $this->_get($property);
35
        }
36
    }
37
38 15
    public function __set($property, $value)
39
    {
40 15
        $setter = 'set' . ucfirst($property);
41
42 15
        if (method_exists($this, $setter)) {
43 15
            $this->$setter($value);
44 8
        } elseif (method_exists($this, 'get' . ucfirst($property))) {
45
            throw new ReadOnlyException("Property \$$property is read-only.");
46
        } else {
47 8
            $this->_magic[$property] = $value;
48
        }
49 15
    }
50
51 1
    public function __isset($property)
52
    {
53 1
        return $this->$property !== null;
54
    }
55
56
    public function __unset($property)
57
    {
58
        $this->$property = null;
59
    }
60
61
    public function _get($property)
62
    {
63
        return isset($this->_magic[$property]) ? $this->_magic[$property] : null;
64
    }
65
66
    public function _set($property, $value)
67
    {
68
        $this->_magic[$property] = $value;
69
    }
70
71 14
    public function applyOptions(array $options)
72
    {
73 14
        foreach ($options as $key => $value) {
74 13
            $this->$key = $value;
75
        }
76 14
    }
77
}
78