Attribute::validate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EntWeChat\Support;
4
5
use EntWeChat\Core\Exceptions\InvalidArgumentException;
6
7
/**
8
 * Class Attributes.
9
 */
10
abstract class Attribute extends Collection
11
{
12
    /**
13
     * Attributes alias.
14
     *
15
     * @var array
16
     */
17
    protected $aliases = [];
18
19
    /**
20
     * Auto snake attribute name.
21
     *
22
     * @var bool
23
     */
24
    protected $snakeable = true;
25
26
    /**
27
     * Required attributes.
28
     *
29
     * @var array
30
     */
31
    protected $requirements = [];
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param array $attributes
37
     */
38
    public function __construct(array $attributes = [])
39
    {
40
        parent::__construct($attributes);
41
    }
42
43
    /**
44
     * Set attribute.
45
     *
46
     * @param string $attribute
47
     * @param string $value
48
     *
49
     * @return Attribute
50
     */
51
    public function setAttribute($attribute, $value)
52
    {
53
        $this->set($attribute, $value);
54
55
        return $this;
56
    }
57
58
    /**
59
     * Get attribute.
60
     *
61
     * @param string $attribute
62
     * @param mixed  $default
63
     *
64
     * @return mixed
65
     */
66
    public function getAttribute($attribute, $default)
67
    {
68
        return $this->get($attribute, $default);
69
    }
70
71
    /**
72
     * Set attribute.
73
     *
74
     * @param string $attribute
75
     * @param mixed  $value
76
     *
77
     * @throws \EntWeChat\Core\Exceptions\InvalidArgumentException
78
     *
79
     * @return Attribute
80
     */
81
    public function with($attribute, $value)
82
    {
83
        $this->snakeable && $attribute = Str::snake($attribute);
84
85
        if (!$this->validate($attribute, $value)) {
86
            throw new InvalidArgumentException("Invalid attribute '{$attribute}'.");
87
        }
88
89
        $this->set($attribute, $value);
90
91
        return $this;
92
    }
93
94
    /**
95
     * Attribute validation.
96
     *
97
     * @param string $attribute
98
     * @param mixed  $value
99
     *
100
     * @return bool
101
     */
102
    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...
103
    {
104
        return true;
105
    }
106
107
    /**
108
     * Override parent set() method.
109
     *
110
     * @param string $attribute
111
     * @param mixed  $value
112
     */
113
    public function set($attribute, $value = null)
114
    {
115
        parent::set($this->getRealKey($attribute), $value);
116
    }
117
118
    /**
119
     * Override parent get() method.
120
     *
121
     * @param string $attribute
122
     * @param mixed  $default
123
     *
124
     * @return mixed
125
     */
126
    public function get($attribute, $default = null)
127
    {
128
        return parent::get($this->getRealKey($attribute), $default);
129
    }
130
131
    /**
132
     * Magic call.
133
     *
134
     * @param string $method
135
     * @param array  $args
136
     *
137
     * @return Attribute
138
     */
139
    public function __call($method, $args)
140
    {
141
        if (stripos($method, 'with') === 0) {
142
            $method = substr($method, 4);
143
        }
144
145
        return $this->with($method, array_shift($args));
146
    }
147
148
    /**
149
     * Magic set.
150
     *
151
     * @param string $property
152
     * @param mixed  $value
153
     *
154
     * @return Attribute
155
     */
156
    public function __set($property, $value)
157
    {
158
        return $this->with($property, $value);
159
    }
160
161
    /**
162
     * Whether or not an data exists by key.
163
     *
164
     * @param string $key
165
     *
166
     * @return bool
167
     */
168
    public function __isset($key)
169
    {
170
        return parent::__isset($this->getRealKey($key));
171
    }
172
173
    /**
174
     * Return the raw name of attribute.
175
     *
176
     * @param string $key
177
     *
178
     * @return string
179
     */
180
    protected function getRealKey($key)
181
    {
182
        if ($alias = array_search($key, $this->aliases, true)) {
183
            $key = $alias;
184
        }
185
186
        return $key;
187
    }
188
189
    /**
190
     * Check required attributes.
191
     *
192
     * @throws InvalidArgumentException
193
     */
194
    protected function checkRequiredAttributes()
195
    {
196
        foreach ($this->requirements as $attribute) {
197
            if (!isset($this->$attribute)) {
198
                throw new InvalidArgumentException(" '{$attribute}' cannot be empty.");
199
            }
200
        }
201
    }
202
203
    /**
204
     * Return all items.
205
     *
206
     * @throws InvalidArgumentException
207
     *
208
     * @return array
209
     */
210
    public function all()
211
    {
212
        $this->checkRequiredAttributes();
213
214
        return parent::all();
215
    }
216
}
217