Passed
Pull Request — master (#2)
by Bernhard
02:11
created

PropertyTrait::setProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3
1
<?php
2
3
namespace GloBee\PaymentApi\Models;
4
5
trait PropertyTrait
6
{
7
    /**
8
     * @param $name
9
     *
10
     * @return mixed
11
     */
12 10
    public function getProperty($name)
13
    {
14 10
        $methodName = 'get'.$this->strToStudlyCase($name);
15 10
        if (method_exists($this, $methodName)) {
16
            return $this->{$methodName}();
17
        }
18
19 10
        if (property_exists($this, $name)) {
20 10
            return $this->{$name};
21
        }
22
    }
23
24
    /**
25
     * @param $name
26
     * @param $value
27
     */
28 4
    protected function setProperty($name, $value)
29
    {
30 4
        $methodName = 'set'.$this->strToStudlyCase($name);
31 4
        if (method_exists($this, $methodName)) {
32 2
            $this->{$methodName}($value);
33
34 2
            return;
35
        }
36
37 4
        if (property_exists($this, $name)) {
38 4
            $this->{$name} = $value;
39
40 4
            return;
41
        }
42
    }
43
44
    /**
45
     * @param $name
46
     * @param $arguments
47
     *
48
     * @return mixed|void
49
     */
50 6
    public function __call($name, $arguments)
51
    {
52 6
        $mutator = substr($name, 0, 3);
53 6
        if ($mutator === 'get') {
54 6
            return $this->getProperty(lcfirst(substr($name, 3)));
55
        }
56
57 1
        if ($mutator === 'set') {
58 1
            $this->setProperty(lcfirst(substr($name, 3)), $arguments[0]);
59
60 1
            return;
61
        }
62
    }
63
64 4
    public function __get($name)
65
    {
66 4
        return $this->getProperty($name);
67
    }
68
69 3
    public function __set($name, $value)
70
    {
71 3
        $this->setProperty($name, $value);
72 3
    }
73
74
    /**
75
     * @param $key
76
     *
77
     * @return mixed
78
     */
79 10
    protected function strToStudlyCase($key)
80
    {
81 10
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
82
    }
83
}
84