Passed
Branch master (1144ec)
by Zing
04:54
created

WithCasts   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 10
c 0
b 0
f 0
dl 0
loc 61
ccs 14
cts 14
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A withCasts() 0 5 1
A getCasts() 0 3 1
A mergeCasts() 0 3 1
A getCastType() 0 3 1
A hasCast() 0 7 3
1
<?php
2
3
namespace Zing\QueryBuilder\Concerns;
4
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)
59
    {
60 1
        $this->mergeCasts($casts);
61
62 1
        return $this;
63
    }
64
65
    abstract protected function castAttribute($key, $value);
66
}
67