AbstractModel   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 23
c 4
b 0
f 0
dl 0
loc 103
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 5 1
A enableCasting() 0 5 1
A castAllAttributes() 0 4 1
A castAttribute() 0 7 3
A get() 0 5 1
A __call() 0 15 3
1
<?php
2
3
namespace Transmission\Models;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Transmission\Formatter;
8
9
/**
10
 * AbstractModel.
11
 */
12
class AbstractModel extends Collection
13
{
14
    /**
15
     * Cast attributes to appropriate types.
16
     *
17
     * @var bool
18
     */
19
    protected $castingEnabled = false;
20
21
    /**
22
     * The attributes that should be cast to native or other supported types.
23
     *
24
     * Casts only when enabled and attributes provided.
25
     *
26
     * @var array
27
     */
28
    protected $casts = [];
29
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * @param null|bool $castingEnabled
34
     */
35
    public function get($key, $default = null, $castingEnabled = null)
36
    {
37
        $value = parent::get($key, $default);
38
39
        return $this->castAttribute($key, $value, $castingEnabled ?? $this->castingEnabled);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function toArray()
46
    {
47
        $this->castAllAttributes();
48
49
        return parent::toArray();
50
    }
51
52
    /**
53
     * Enable casting attributes globally.
54
     *
55
     * @param bool $castingEnabled Default enable casting.
56
     *
57
     * @return $this
58
     */
59
    public function enableCasting($castingEnabled = true): self
60
    {
61
        $this->castingEnabled = $castingEnabled;
62
63
        return $this;
64
    }
65
66
    /**
67
     * Cast All Attributes.
68
     */
69
    protected function castAllAttributes(): void
70
    {
71
        $this->transform(function ($value, $key) {
72
            return $this->castAttribute($key, $value, $this->castingEnabled);
73
        });
74
    }
75
76
    /**
77
     * @param string $key
78
     * @param mixed  $value
79
     * @param bool   $castingEnabled
80
     *
81
     * @return mixed
82
     */
83
    protected function castAttribute(string $key, $value, bool $castingEnabled = false)
84
    {
85
        if ($castingEnabled && array_key_exists($key, $this->casts)) {
86
            return Formatter::castAttribute($this->casts[$key], $value);
87
        }
88
89
        return $value;
90
    }
91
92
    /**
93
     * Magic method to get attributes dynamically.
94
     *
95
     * @param $method
96
     * @param $arguments
97
     *
98
     * @return mixed
99
     */
100
    public function __call($method, $arguments)
101
    {
102
        $attribute = Str::camel(Str::after($method, 'get'));
103
        if (!Str::startsWith($method, 'get') || !$this->has($attribute)) {
104
            throw new \BadMethodCallException(sprintf(
105
                'Method %s::%s does not exist.',
106
                static::class,
107
                $method
108
            ));
109
        }
110
111
        $castingEnabled = $arguments[0] ?? $this->castingEnabled;
112
        $value = parent::get($attribute);
113
114
        return $this->castAttribute($attribute, $value, $castingEnabled);
115
    }
116
}
117