Passed
Push — master ( aebc3a...07e708 )
by Irfaq
14:17
created

AbstractModel::enableCasting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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