Completed
Branch develop (bbe4f8)
by Pavel
02:03
created

Caster   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 95
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B cast() 0 23 4
C resolveCast() 0 30 7
castRules() 0 1 ?
1
<?php
2
3
namespace Koch\Casters;
4
5
use Closure;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Collection;
8
use Koch\Casters\Behavior\BuildsCastQueries;
9
use Koch\Casters\Contracts\Caster as CasterContract;
10
11
abstract class Caster implements CasterContract
12
{
13
    use BuildsCastQueries;
14
15
    /**
16
     * Determines the function sign.
17
     *
18
     * @var string
19
     */
20
    private $functionSign = '@';
21
22
    /**
23
     * Determines the query sign.
24
     *
25
     * @var string
26
     */
27
    private $querySign = '!';
28
29
    /**
30
     * Casts collection fields.
31
     *
32
     * @param  mixed  $model
33
     * @return array
34
     */
35
    public function cast($model)
36
    {
37
        // First we determine if given parameter is a collection
38
        // if so, we call this method recursively for each of the
39
        // results.
40
        if ($model instanceof Collection) {
41
            return $model->map([$this, 'cast'])->toArray();
42
        }
43
44
        if (empty($model)) {
45
            return;
46
        }
47
48
        $transformed = [];
49
50
        // We go through each of the specified cast rules
51
        // and resolve them one after another.
52
        foreach ($this->castRules() as $old => $desired) {
53
            $this->resolveCast($old, $desired, $model, $transformed);
54
        }
55
56
        return $transformed;
57
    }
58
59
    /**
60
     * Resolves casts based on supplied array of arguments.
61
     *
62
     * @param  string  $old
63
     * @param  string|Closure  $desired
64
     * @param  \Illuminate\Database\Eloquent\Model  $model
65
     * @param  array  &$transformed
66
     * @return array
67
     */
68
    private function resolveCast($old, $desired, Model $model, &$transformed)
69
    {
70
        // If there was a closure provided as a value of the array
71
        // run that closure and return it as a result of casting.
72
        if ($desired instanceof Closure) {
73
            return $transformed[$old] = call_user_func($desired, $model);
74
        }
75
76
        // If the value was prefixed with the function sign, which
77
        // is '@' by default, call desired method on this class.
78
        if (is_string($desired) && strpos($desired, $this->functionSign) !== false) {
79
            return $transformed[$old] = call_user_func([$this, substr($desired, 1)], $model);
80
        }
81
82
        // If it was prefixed with the query sign, which
83
        // is '!' by default, make use of the CastBuilder instance
84
        // and resolve the cast there.
85
        if (is_string($desired) && strpos($desired, $this->querySign) !== false) {
86
            return $this->parse($old, substr($desired, 1), $model, $transformed);
87
        }
88
89
        // If they specified simple key - value string pairs, simply
90
        // rename the column, retaining the contents.
91
        if (is_string($old)) {
92
            return $transformed[$desired] = $model->$old;
93
        }
94
95
        // Otherwise keep unchanged.
96
        return $transformed[$desired] = $model->$desired;
97
    }
98
99
    /**
100
     * Returns the cast rules.
101
     *
102
     * @return array
103
     */
104
    abstract protected function castRules();
105
}
106