ActiveModel   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 83
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A fromSerialize() 0 3 1
A asSerialize() 0 3 1
A castAttribute() 0 11 3
A setAttribute() 0 7 3
A isSerializeCastable() 0 3 1
A getLocaled() 0 12 4
1
<?php
2
3
namespace Ffcms\Core\Arch;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Serialize;
7
use Ffcms\Core\Helper\Type\Any;
8
use Ffcms\Core\Helper\Type\Str;
9
use Illuminate\Database\Eloquent\Model as LaravelModel;
10
use Illuminate\Support\Collection;
11
12
/**
13
 * Class ActiveModel. Basic implementation of laravel active records model with predefined settings
14
 * @package Ffcms\Core\Arch
15
 * @method static ActiveModel where($field = null, $compare = null, $value = null)
16
 * @method static ActiveModel whereIn($field = null, array $values = [])
17
 * @method static ActiveModel orWhere($field = null, $compare = null, $value = null)
18
 * @method static ActiveModel whereNull($field)
19
 * @method static ActiveModel whereBetween($field = null, array $values = [])
20
 * @method static ActiveModel whereNotIn($field = null, array $values = [])
21
 * @method static ActiveModel orderBy($field, $sortType)
22
 * @method static ActiveModel search($query = null, $trashhold = 4)
23
 * @method ActiveModel groupBy($field)
24
 * @method static self|null first()
25
 * @method static self|null last()
26
 * @method static self|null firstWhere($field = null, $compare = null, $value = null)
27
 * @method static self|null find($id)
28
 * @method static self|null findOrNew($id)
29
 * @method static ActiveModel|null whereNotNull($field)
30
 * @method static ActiveModel|null orWhereNotNull($field)
31
 * @method ActiveModel skip($count)
32
 * @method ActiveModel take($count)
33
 * @method static ActiveModel pluck($column, $key = null)
34
 * @method ActiveModel whereYear($field = null, $compare = null, $value = null)
35
 * @method ActiveModel whereMonth($field = null, $compare = null, $value = null)
36
 * @method ActiveModel whereDay($field = null, $compare = null, $value = null)
37
 * @method static ActiveModel select($columns = null)
38
 * @method \Illuminate\Database\Eloquent\Collection get($columns = ['*'])
39
 * @method ActiveModel each(callable $items)
40
 * @method self|null forPage($pageNumber, $itemCount)
41
 * @method bool contains($field, $value)
42
 * @method bool isEmpty
43
 * @method bool isNotEmpty
44
 * @method ActiveModel map(callable &$item)
45
 * @method int count()
46
 * @inheritdoc
47
 */
48
class ActiveModel extends LaravelModel
49
{
50
    /**
51
     * Special function for locale stored attributes under serialization.
52
     * @param string $attribute
53
     * @return array|null|string
54
     */
55
    public function getLocaled(string $attribute)
56
    {
57
        // if always decoded
58
        if (Any::isArray($this->{$attribute})) {
59
            return $this->{$attribute}[App::$Request->getLanguage()];
60
        }
61
62
        if (!Any::isStr($attribute) || Str::likeEmpty($this->{$attribute})) {
63
            return null;
64
        }
65
66
        return Serialize::getDecodeLocale($this->{$attribute});
67
    }
68
69
    /**
70
     * Set model attribute. Extend laravel attribute casting mutators by serialized array
71
     * @param string $key
72
     * @param mixed $value
73
     * @return LaravelModel
74
     */
75
    public function setAttribute($key, $value)
76
    {
77
        if ($value !== null && $this->isSerializeCastable($key)) {
78
            $value = $this->asSerialize($value);
79
        }
80
81
        return parent::setAttribute($key, $value);
82
    }
83
84
    /**
85
     * Cast model attribute. Extend laravel attribute casting mutators by serialized array
86
     * @param string $key
87
     * @param mixed $value
88
     * @return mixed
89
     */
90
    protected function castAttribute($key, $value)
91
    {
92
        if ($value === null) {
93
            return $value;
94
        }
95
96
        if ($this->getCastType($key) === 'serialize') {
97
            return $this->fromSerialize($value);
98
        }
99
100
        return parent::castAttribute($key, $value);
101
    }
102
103
    /**
104
     * Check if key is castable to be serialized
105
     * @param string $key
106
     * @return bool
107
     */
108
    public function isSerializeCastable(string $key): bool
109
    {
110
        return $this->hasCast($key, 'serialize');
111
    }
112
113
    /**
114
     * Serialize value
115
     * @param $value
116
     * @return Serialize
117
     */
118
    public function asSerialize($value)
119
    {
120
        return serialize($value);
0 ignored issues
show
Bug Best Practice introduced by
The expression return serialize($value) returns the type string which is incompatible with the documented return type Ffcms\Core\Helper\Serialize.
Loading history...
121
    }
122
123
    /**
124
     * Unserialize value
125
     * @param string $value
126
     * @return mixed
127
     */
128
    public function fromSerialize(string $value)
129
    {
130
        return unserialize($value, ['allowed_classes' => false]);
131
    }
132
}
133