1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Arch; |
4
|
|
|
|
5
|
|
|
use Ffcms\Core\Helper\Serialize; |
6
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
7
|
|
|
use Ffcms\Core\Helper\Type\Str; |
8
|
|
|
use \Illuminate\Database\Eloquent\Model as LaravelModel; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ActiveModel. Basic implementation of laravel active records model with predefined settings |
12
|
|
|
* @package Ffcms\Core\Arch |
13
|
|
|
* @method static ActiveModel where($field = null, $compare = null, $value = null) |
14
|
|
|
* @method static ActiveModel whereIn($field = null, array $values = []) |
15
|
|
|
* @method static ActiveModel orWhere($field = null, $compare = null, $value = null) |
16
|
|
|
* @method static ActiveModel orderBy($field, $sortType) |
17
|
|
|
* @method static ActiveModel|null first() |
18
|
|
|
* @method static ActiveModel|null find($id) |
19
|
|
|
* @method static ActiveModel|null whereNotNull($field) |
20
|
|
|
* @method static ActiveModel|null orWhereNotNull($field) |
21
|
|
|
* @method ActiveModel skip($count) |
22
|
|
|
* @method ActiveModel take($count) |
23
|
|
|
* @method int count() |
24
|
|
|
* @inheritdoc |
25
|
|
|
*/ |
26
|
|
|
class ActiveModel extends LaravelModel |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Special function for locale stored attributes under serialization. |
30
|
|
|
* @param string $attribute |
31
|
|
|
* @return array|null|string |
32
|
|
|
*/ |
33
|
|
|
public function getLocaled($attribute) |
34
|
|
|
{ |
35
|
|
|
if (!Obj::isString($attribute) || Str::likeEmpty($this->{$attribute})) { |
36
|
|
|
return null; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return Serialize::getDecodeLocale($this->{$attribute}); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set model attribute. Extend laravel attribute casting mutators by serialized array |
44
|
|
|
* @param string $key |
45
|
|
|
* @param mixed $value |
46
|
|
|
* @return LaravelModel |
47
|
|
|
*/ |
48
|
|
|
public function setAttribute($key, $value) |
49
|
|
|
{ |
50
|
|
|
if ($value !== null && $this->isSerializeCastable($key)) { |
51
|
|
|
$value = $this->asSerialize($value); |
52
|
|
|
} |
53
|
|
|
return parent::setAttribute($key, $value); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Cast model attribute. Extend laravel attribute casting mutators by serialized array |
58
|
|
|
* @param string $key |
59
|
|
|
* @param mixed $value |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
|
|
protected function castAttribute($key, $value) |
63
|
|
|
{ |
64
|
|
|
if ($value === null) { |
65
|
|
|
return $value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($this->getCastType($key) === 'serialize') { |
69
|
|
|
return $this->fromSerialize($value); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return parent::castAttribute($key, $value); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Check if key is castable to be serialized |
77
|
|
|
* @param string $key |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function isSerializeCastable($key) |
81
|
|
|
{ |
82
|
|
|
return $this->hasCast($key, 'serialize'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Serialize value |
87
|
|
|
* @param $value |
88
|
|
|
* @return Serialize |
89
|
|
|
*/ |
90
|
|
|
public function asSerialize($value) |
91
|
|
|
{ |
92
|
|
|
return serialize($value); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Unserialize value |
97
|
|
|
* @param $value |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
public function fromSerialize($value) |
101
|
|
|
{ |
102
|
|
|
return unserialize($value); |
103
|
|
|
} |
104
|
|
|
} |