Passed
Pull Request — master (#18)
by Ruben
03:40
created

AccessorTrait::set()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
namespace MovingImage\Client\VMPro\Util;
4
5
/**
6
 * AccessorTrait for general purposes.
7
 *
8
 * @author Omid Rad <[email protected]>
9
 */
10
trait AccessorTrait
11
{
12
    public static $typeSnakeCase = 0;
13
    public static $typeCamelCase = 1;
14
15
    /**
16
     * @var int Set default type to snake case
17
     */
18
    private $type = 0;
19
20
    private $container = [];
21
22
    /**
23
     * @param $methodName string Key
24
     * @param $args       array  Method Arguments
25
     *
26
     * @return mixed
27
     */
28
    public function __call($methodName, $args)
29
    {
30
        // are we getting or setting?
31
        if (preg_match('~^(set|get|is)([A-Z])(.*)$~', $methodName, $matches)) {
32
            $property = strtolower($matches[2]).$matches[3];
33
34
            if ($this->type === self::$typeSnakeCase) {
35
                $property = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $property));
36
            }
37
38
            switch ($matches[1]) {
39
                case 'set':
40
                    $this->checkArguments($args, 1, 1, $methodName);
41
42
                    return $this->set($property, $args[0]);
43
                case 'is':
44
                    $this->checkArguments($args, 0, 0, $methodName);
45
46
                    return $this->is($property);
47
                case 'get':
48
                    $this->checkArguments($args, 0, 0, $methodName);
49
50
                    return $this->get($property);
51
                case 'default':
52
                    throw new \BadMethodCallException("Method $methodName is not exist");
53
            }
54
        }
55
56
        return null;
57
    }
58
59
    /**
60
     * @param $property string Key
61
     *
62
     * @return mixed
63
     */
64
    public function get($property)
65
    {
66
        if (isset($this->container[$property])) {
67
            return $this->container[$property];
68
        }
69
70
        return null;
71
    }
72
73
    /**
74
     * @param $property
75
     *
76
     * @return bool|null
77
     */
78
    public function is($property)
79
    {
80
        if (isset($this->container[$property])) {
81
            return $this->container[$property] === 'true';
82
        }
83
84
        return null;
85
    }
86
87
    /**
88
     * @param $property string Key
89
     * @param $value    string Value
90
     *
91
     * @return self
92
     */
93
    public function set($property, $value)
94
    {
95
        // we need to convert booleans into string, because these are query parameters
96
        if (is_bool($value)) {
97
            $value = $value
98
                ? 'true'
99
                : 'false';
100
        }
101
102
        $this->container[$property] = $value;
103
104
        return $this;
105
    }
106
107
    /**
108
     * Check if args are valid or not.
109
     *
110
     * @param array  $args       List of arguments
111
     * @param int    $min        integer Minimum valid params
112
     * @param int    $max        Maximum valid params
113
     * @param string $methodName Method name
114
     */
115
    protected function checkArguments(array $args, $min, $max, $methodName)
116
    {
117
        $argc = count($args);
118
        if ($argc < $min || $argc > $max) {
119
            throw new \BadMethodCallException("Method $methodName is not exist");
120
        }
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    public function getContainer()
127
    {
128
        return $this->container;
129
    }
130
131
    /**
132
     * @return int
133
     */
134
    public function getType()
135
    {
136
        return $this->type;
137
    }
138
139
    /**
140
     * @param int $type
141
     *
142
     * @return $this
143
     */
144
    public function setType($type)
145
    {
146
        $this->type = $type;
147
148
        return $this;
149
    }
150
}
151