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

PropertyTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 86.96%

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 57
ccs 20
cts 23
cp 0.8696
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setProperty() 0 13 3
A getProperty() 0 9 3
A __set() 0 3 1
A __get() 0 3 1
A strToStudlyCase() 0 3 1
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