TestsLing /
easy-im
| 1 | <?php |
||
| 2 | |||
| 3 | namespace EasyIM\Kernel\Traits; |
||
| 4 | |||
| 5 | use EasyIM\Kernel\Exceptions\InvalidArgumentException; |
||
| 6 | use EasyIM\Kernel\Support\Arr; |
||
| 7 | use EasyIM\Kernel\Support\Str; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Trait Attributes. |
||
| 11 | */ |
||
| 12 | trait HasAttributes |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | protected $attributes = []; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var bool |
||
| 21 | */ |
||
| 22 | protected $snakeable = true; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Set Attributes. |
||
| 26 | * |
||
| 27 | * @param array $attributes |
||
| 28 | * |
||
| 29 | * @return $this |
||
| 30 | */ |
||
| 31 | 19 | public function setAttributes(array $attributes = []) |
|
| 32 | { |
||
| 33 | 19 | $this->attributes = $attributes; |
|
| 34 | |||
| 35 | 19 | return $this; |
|
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Set attribute. |
||
| 40 | * |
||
| 41 | * @param string $attribute |
||
| 42 | * @param mixed $value |
||
| 43 | * |
||
| 44 | * @return $this |
||
| 45 | */ |
||
| 46 | 7 | public function setAttribute($attribute, $value) |
|
| 47 | { |
||
| 48 | 7 | Arr::set($this->attributes, $attribute, $value); |
|
| 49 | |||
| 50 | 7 | return $this; |
|
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get attribute. |
||
| 55 | * |
||
| 56 | * @param string $attribute |
||
| 57 | * @param mixed $default |
||
| 58 | * |
||
| 59 | * @return mixed |
||
| 60 | */ |
||
| 61 | 17 | public function getAttribute($attribute, $default = null) |
|
| 62 | { |
||
| 63 | 17 | return Arr::get($this->attributes, $attribute, $default); |
|
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $attribute |
||
| 68 | * |
||
| 69 | * @return bool |
||
| 70 | */ |
||
| 71 | 1 | public function isRequired($attribute) |
|
| 72 | { |
||
| 73 | 1 | return in_array($attribute, $this->getRequired(), true); |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return array|mixed |
||
| 78 | */ |
||
| 79 | 10 | public function getRequired() |
|
| 80 | { |
||
| 81 | 10 | return property_exists($this, 'required') ? $this->required : []; |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Set attribute. |
||
| 86 | * |
||
| 87 | * @param string $attribute |
||
| 88 | * @param mixed $value |
||
| 89 | * |
||
| 90 | * @return $this |
||
| 91 | */ |
||
| 92 | 2 | public function with($attribute, $value) |
|
| 93 | { |
||
| 94 | 2 | $this->snakeable && $attribute = Str::snake($attribute); |
|
| 95 | |||
| 96 | 2 | $this->setAttribute($attribute, $value); |
|
| 97 | |||
| 98 | 2 | return $this; |
|
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Override parent set() method. |
||
| 103 | * |
||
| 104 | * @param string $attribute |
||
| 105 | * @param mixed $value |
||
| 106 | * |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | 1 | public function set($attribute, $value) |
|
| 110 | { |
||
| 111 | 1 | $this->setAttribute($attribute, $value); |
|
| 112 | |||
| 113 | 1 | return $this; |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Override parent get() method. |
||
| 118 | * |
||
| 119 | * @param string $attribute |
||
| 120 | * @param mixed $default |
||
| 121 | * |
||
| 122 | * @return mixed |
||
| 123 | */ |
||
| 124 | 13 | public function get($attribute, $default = null) |
|
| 125 | { |
||
| 126 | 13 | return $this->getAttribute($attribute, $default); |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $key |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | 1 | public function has(string $key) |
|
| 135 | { |
||
| 136 | 1 | return Arr::has($this->attributes, $key); |
|
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param array $attributes |
||
| 141 | * |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | 1 | public function merge(array $attributes) |
|
| 145 | { |
||
| 146 | 1 | $this->attributes = array_merge($this->attributes, $attributes); |
|
| 147 | |||
| 148 | 1 | return $this; |
|
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param array|string $keys |
||
| 153 | * |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | 1 | public function only($keys) |
|
| 157 | { |
||
| 158 | 1 | return Arr::only($this->attributes, $keys); |
|
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Return all items. |
||
| 163 | * |
||
| 164 | * @return array |
||
| 165 | * |
||
| 166 | * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException |
||
| 167 | */ |
||
| 168 | 2 | public function all() |
|
| 169 | { |
||
| 170 | 2 | $this->checkRequiredAttributes(); |
|
| 171 | |||
| 172 | 2 | return $this->attributes; |
|
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Magic call. |
||
| 177 | * |
||
| 178 | * @param string $method |
||
| 179 | * @param array $args |
||
| 180 | * |
||
| 181 | * @return $this |
||
| 182 | */ |
||
| 183 | 1 | public function __call($method, $args) |
|
| 184 | { |
||
| 185 | 1 | if (0 === stripos($method, 'with')) { |
|
| 186 | 1 | return $this->with(substr($method, 4), array_shift($args)); |
|
| 187 | } |
||
| 188 | |||
| 189 | 1 | throw new \BadMethodCallException(sprintf('Method "%s" does not exists.', $method)); |
|
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Magic get. |
||
| 194 | * |
||
| 195 | * @param string $property |
||
| 196 | * |
||
| 197 | * @return mixed |
||
| 198 | */ |
||
| 199 | 2 | public function __get($property) |
|
| 200 | { |
||
| 201 | 2 | return $this->get($property); |
|
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Magic set. |
||
| 206 | * |
||
| 207 | * @param string $property |
||
| 208 | * @param mixed $value |
||
| 209 | * |
||
| 210 | * @return $this |
||
| 211 | */ |
||
| 212 | 1 | public function __set($property, $value) |
|
| 213 | { |
||
| 214 | 1 | return $this->with($property, $value); |
|
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Whether or not an data exists by key. |
||
| 219 | * |
||
| 220 | * @param string $key |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | 1 | public function __isset($key) |
|
| 225 | { |
||
| 226 | 1 | return isset($this->attributes[$key]); |
|
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Check required attributes. |
||
| 231 | * |
||
| 232 | * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException |
||
| 233 | */ |
||
| 234 | 10 | protected function checkRequiredAttributes() |
|
| 235 | { |
||
| 236 | 10 | foreach ($this->getRequired() as $attribute) { |
|
| 237 | 6 | if (is_null($this->get($attribute))) { |
|
| 238 | 6 | throw new InvalidArgumentException(sprintf('"%s" cannot be empty.', $attribute)); |
|
| 239 | } |
||
| 240 | } |
||
| 241 | 9 | } |
|
| 242 | } |
||
| 243 |