| Total Complexity | 7 |
| Total Lines | 61 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 5 | trait WithCasts |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * The attributes that should be cast to native types. |
||
| 9 | * |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | protected $casts = []; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Determine whether an attribute should be cast to a native type. |
||
| 16 | * |
||
| 17 | * @param string $key |
||
| 18 | * @param array|string|null $types |
||
| 19 | * |
||
| 20 | * @return bool |
||
| 21 | */ |
||
| 22 | 9 | public function hasCast($key, $types = null) |
|
| 23 | { |
||
| 24 | 9 | if (array_key_exists($key, $this->getCasts())) { |
|
| 25 | 1 | return $types ? in_array($this->getCastType($key), (array) $types, true) : true; |
|
| 26 | } |
||
| 27 | |||
| 28 | 8 | return false; |
|
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Get the casts array. |
||
| 33 | * |
||
| 34 | * @return array |
||
| 35 | */ |
||
| 36 | 9 | public function getCasts() |
|
| 37 | { |
||
| 38 | 9 | return $this->casts; |
|
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Get the type of cast for a model attribute. |
||
| 43 | * |
||
| 44 | * @param string $key |
||
| 45 | * |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | 1 | protected function getCastType($key) |
|
| 49 | { |
||
| 50 | 1 | return $this->getCasts()[$key]; |
|
| 51 | } |
||
| 52 | |||
| 53 | 1 | public function mergeCasts($casts) |
|
| 54 | { |
||
| 55 | 1 | $this->casts = array_merge($this->casts, $casts); |
|
| 56 | 1 | } |
|
| 57 | |||
| 58 | 1 | public function withCasts($casts) |
|
| 63 | } |
||
| 64 | |||
| 65 | abstract protected function castAttribute($key, $value); |
||
| 66 | } |
||
| 67 |