Completed
Pull Request — master (#292)
by Carlos
03:42
created

Attribute   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 20
c 3
b 0
f 1
lcom 1
cbo 3
dl 0
loc 207
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A setAttribute() 0 6 1
A getAttribute() 0 4 1
A with() 0 12 3
A validate() 0 4 1
A set() 0 4 1
A get() 0 4 1
A __call() 0 8 2
A __set() 0 4 1
A __isset() 0 4 1
A getRealKey() 0 8 2
A checkRequiredAttributes() 0 8 3
A all() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
/**
13
 * Attributes.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\Support;
22
23
use EasyWeChat\Core\Exceptions\InvalidArgumentException;
24
25
/**
26
 * Class Attributes.
27
 */
28
abstract class Attribute extends Collection
29
{
30
    /**
31
     * Attributes alias.
32
     *
33
     * @var array
34
     */
35
    protected $aliases = [];
36
37
    /**
38
     * Auto snake attribute name.
39
     *
40
     * @var bool
41
     */
42
    protected $snakeable = true;
43
44
    /**
45
     * Required attributes.
46
     *
47
     * @var array
48
     */
49
    protected $requireds = [];
50
51
    /**
52
     * Constructor.
53
     *
54
     * @param array $attributes
55
     */
56
    public function __construct($attributes = [])
57
    {
58
        foreach ($attributes as $key => $value) {
59
            $this->set($key, $value);
60
        }
61
    }
62
63
    /**
64
     * Set attribute.
65
     *
66
     * @param string $attribute
67
     * @param string $value
68
     *
69
     * @return Attribute
70
     */
71
    public function setAttribute($attribute, $value)
72
    {
73
        $this->set($attribute, $value);
74
75
        return $this;
76
    }
77
78
    /**
79
     * Get attribute.
80
     *
81
     * @param string $attribute
82
     * @param mixed  $default
83
     *
84
     * @return mixed
85
     */
86
    public function getAttribute($attribute, $default)
87
    {
88
        return $this->get($attribute, $default);
89
    }
90
91
    /**
92
     * Set attribute.
93
     *
94
     * @param string $attribute
95
     * @param mixed  $value
96
     *
97
     * @return Attribute
98
     */
99
    public function with($attribute, $value)
100
    {
101
        $this->snakeable && $attribute = Str::snake($attribute);
102
103
        if (!$this->validate($attribute, $value)) {
104
            throw new InvalidArgumentException("Invalid attribute '{$attribute}'.");
105
        }
106
107
        $this->set($attribute, $value);
108
109
        return $this;
110
    }
111
112
    /**
113
     * Attribute validation.
114
     *
115
     * @param string $attribute
116
     * @param mixed  $value
117
     *
118
     * @return bool
119
     */
120
    protected function validate($attribute, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
    {
122
        return true;
123
    }
124
125
    /**
126
     * Override parent set() method.
127
     *
128
     * @param string $attribute
129
     * @param mixed  $value
130
     */
131
    public function set($attribute, $value = null)
132
    {
133
        return parent::set($this->getRealKey($attribute), $value);
134
    }
135
136
    /**
137
     * Override parent get() method.
138
     *
139
     * @param string $attribute
140
     * @param mixed  $default
141
     *
142
     * @return mixed
143
     */
144
    public function get($attribute, $default = null)
145
    {
146
        return parent::get($this->getRealKey($attribute), $default);
147
    }
148
149
    /**
150
     * Magic call.
151
     *
152
     * @param string $method
153
     * @param array  $args
154
     *
155
     * @return Attribute
156
     */
157
    public function __call($method, $args)
158
    {
159
        if (stripos($method, 'with') === 0) {
160
            $method = substr($method, 4);
161
        }
162
163
        return $this->with($method, array_shift($args));
164
    }
165
166
    /**
167
     * Magic set.
168
     *
169
     * @param string $property
170
     * @param mixed  $value
171
     *
172
     * @return Attribute
173
     */
174
    public function __set($property, $value)
175
    {
176
        return $this->with($property, $value);
177
    }
178
179
    /**
180
     * Whether or not an data exists by key.
181
     *
182
     * @param string $key
183
     *
184
     * @return bool
185
     */
186
    public function __isset($key)
187
    {
188
        return parent::__isset($this->getRealKey($key));
189
    }
190
191
    /**
192
     * Return the raw name of attribute.
193
     *
194
     * @param string $key
195
     *
196
     * @return string
197
     */
198
    protected function getRealKey($key)
199
    {
200
        if ($alias = array_search($key, $this->aliases, true)) {
201
            $key = $alias;
202
        }
203
204
        return $key;
205
    }
206
207
    /**
208
     * Check requireds attributes.
209
     *
210
     * @throws InvalidArgumentException
211
     */
212
    protected function checkRequiredAttributes()
213
    {
214
        foreach ($this->requireds as $attribute) {
215
            if (!isset($this->$attribute)) {
216
                throw new InvalidArgumentException(" '{$attribute}' cannot be empty.");
217
            }
218
        }
219
    }
220
221
    /**
222
     * Return all items.
223
     *
224
     * @return array
225
     *
226
     * @throws InvalidArgumentException
227
     */
228
    public function all()
229
    {
230
        $this->checkRequiredAttributes();
231
232
        return parent::all();
233
    }
234
}
235