Completed
Pull Request — master (#10)
by Bernhard
02:07 queued 12s
created

PropertyTrait::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace GloBee\PaymentApi\Models;
4
5
use GloBee\PaymentApi\Exceptions\LockedPropertyException;
6
use GloBee\PaymentApi\Exceptions\UnknownPropertyException;
7
8
trait PropertyTrait
9
{
10
    /**
11
     * @param $name
12
     *
13
     * @return mixed
14
     * @throws UnknownPropertyException
15
     */
16 22
    public function getProperty($name)
17
    {
18 22
        $methodName = 'get'.$this->strToStudlyCase($name);
19 22
        if (method_exists($this, $methodName)) {
20
            return $this->{$methodName}();
21
        }
22
23 22
        if (array_key_exists($name, $this->properties)) {
24 17
            return $this->properties[$name];
25
        }
26
27 9
        if (array_key_exists($name, $this->readonlyProperties)) {
0 ignored issues
show
Bug Best Practice introduced by
The property readonlyProperties does not exist on GloBee\PaymentApi\Models\PropertyTrait. Since you implemented __get, consider adding a @property annotation.
Loading history...
28 8
            return $this->readonlyProperties[$name];
29
        }
30
31 1
        throw new UnknownPropertyException($name);
32
    }
33
34
    /**
35
     * @param $name
36
     * @param $value
37
     *
38
     * @throws LockedPropertyException
39
     * @throws UnknownPropertyException
40
     */
41 21
    protected function setProperty($name, $value)
42
    {
43 21
        $methodName = 'set'.$this->strToStudlyCase($name);
44 21
        if (method_exists($this, $methodName)) {
45 18
            $this->{$methodName}($value);
46
47 8
            return;
48
        }
49
50 6
        if (array_key_exists($name, $this->properties)) {
51 4
            $this->properties[$name] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property properties does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
52
53 4
            return;
54
        }
55
56 2
        if (array_key_exists($name, $this->readonlyProperties)) {
0 ignored issues
show
Bug Best Practice introduced by
The property readonlyProperties does not exist on GloBee\PaymentApi\Models\PropertyTrait. Since you implemented __get, consider adding a @property annotation.
Loading history...
57 1
            throw new LockedPropertyException($name);
58
        }
59
60 1
        throw new UnknownPropertyException($name);
61
    }
62
63
    /**
64
     * @param $name
65
     *
66
     * @return mixed
67
     * @throws UnknownPropertyException
68
     */
69 22
    public function __get($name)
70
    {
71 22
        return $this->getProperty($name);
72
    }
73
74
    /**
75
     * @param $name
76
     * @param $value
77
     *
78
     * @throws LockedPropertyException
79
     * @throws UnknownPropertyException
80
     */
81 21
    public function __set($name, $value)
82
    {
83 21
        $this->setProperty($name, $value);
84 9
    }
85
86
    /**
87
     * @param $key
88
     *
89
     * @return mixed
90
     */
91 34
    protected function strToStudlyCase($key)
92
    {
93 34
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
94
    }
95
}
96