GetSetTrait::updateAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php namespace Stevenmaguire\Uber;
2
3
trait GetSetTrait
4
{
5
    /**
6
     * Attempts to magically handle getter and setter methods.
7
     *
8
     * @param    string   $method
9
     * @param    array    $parameters
10
     *
11
     * @return   mixed
12
     * @throws   Exception
13
     */
14 52
    public function __call($method, $parameters)
15
    {
16 52
        $property = $this->convertMethodToProperty($method);
17
18 52
        if ($this->isGetMethod($method)) {
19 18
            return $this->getAttribute($property);
20 38
        } elseif ($this->isSetMethod($method)) {
21 34
            return $this->updateAttribute($property, $parameters[0]);
22
        } // @codeCoverageIgnore
23
24 4
        throw new Exception($method . ' method not implemented');
25
    }
26
27
    /**
28
     * Attempts to parse a method name and format its related property name.
29
     *
30
     * @param    string   $method
31
     *
32
     * @return   string
33
     */
34 52
    public function convertMethodToProperty($method)
35
    {
36 52
        $property = preg_replace("/[g|s]{1}et(.*)/", "$1", $method);
37
38 52
        return strtolower(preg_replace('/(.)(?=[A-Z])/', '$1'.'_', $property));
39
    }
40
41
    /**
42
     * Fetches a specific attribute of the current object.
43
     *
44
     * @param    string   $attribute
45
     *
46
     * @return   mixed|null
47
     */
48 18
    private function getAttribute($attribute)
49
    {
50 18
        if (property_exists($this, $attribute)) {
51 16
            return $this->$attribute;
52
        }
53
54 2
        throw new Exception($attribute . ' attribute not defined');
55
    }
56
57
    /**
58
     * Checks if given method name is a valid getter method.
59
     *
60
     * @param    string   $method
61
     *
62
     * @return   boolean
63
     */
64 52
    public function isGetMethod($method)
65
    {
66 52
        return preg_match("/^get[A-Za-z]+$/", $method);
67
    }
68
69
    /**
70
     * Checks if given method name is a valid setter method.
71
     *
72
     * @param    string   $method
73
     *
74
     * @return   boolean
75
     */
76 38
    public function isSetMethod($method)
77
    {
78 38
        return preg_match("/^set[A-Za-z]+$/", $method);
79
    }
80
81
    /**
82
     * Updates a specific attribute of the current object.
83
     *
84
     * @param    string                   $attribute
85
     * @param    string|boolean|integer   $value
86
     *
87
     * @return   object
88
     */
89 100
    private function updateAttribute($attribute, $value)
90
    {
91 100
        if (property_exists($this, $attribute)) {
92 100
            $this->$attribute = $value;
93 50
        }
94
95 100
        return $this;
96
    }
97
}
98