Completed
Pull Request — master (#8)
by Bernhard
02:32
created

PropertyTrait::__call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
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 14
    public function getProperty($name)
13
    {
14 14
        $methodName = 'get'.$this->strToStudlyCase($name);
15 14
        if (method_exists($this, $methodName)) {
16
            return $this->{$methodName}();
17
        }
18
19 14
        if (property_exists($this, $name)) {
20 14
            return $this->{$name};
21
        }
22
    }
23
24
    /**
25
     * @param $name
26
     * @param $value
27
     */
28 19
    protected function setProperty($name, $value)
29
    {
30 19
        $methodName = 'set'.$this->strToStudlyCase($name);
31 19
        if (method_exists($this, $methodName)) {
32 18
            $this->{$methodName}($value);
33
34 8
            return;
35
        }
36
37 4
        if (property_exists($this, $name)) {
38 4
            $this->{$name} = $value;
39
40 4
            return;
41
        }
42
    }
43
44 14
    public function __get($name)
45
    {
46 14
        return $this->getProperty($name);
47
    }
48
49 19
    public function __set($name, $value)
50
    {
51 19
        $this->setProperty($name, $value);
52 9
    }
53
54
    /**
55
     * @param $key
56
     *
57
     * @return mixed
58
     */
59 27
    protected function strToStudlyCase($key)
60
    {
61 27
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
62
    }
63
}
64