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

WithCasts::getCastType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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