1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Igorsgm\Ghost\Models; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
abstract class BaseModel |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array|string[] |
13
|
|
|
*/ |
14
|
|
|
private array $modelCollectionProperties = [ |
15
|
|
|
'authors' => \Igorsgm\Ghost\Models\Resources\Author::class, |
16
|
|
|
'roles' => \Igorsgm\Ghost\Models\Role::class, |
17
|
|
|
'labels' => \Igorsgm\Ghost\Models\Label::class, |
18
|
|
|
'subscriptions' => \Igorsgm\Ghost\Models\Subscription::class, |
19
|
|
|
'tags' => \Igorsgm\Ghost\Models\Resources\Tag::class, |
20
|
|
|
'navigation' => \Igorsgm\Ghost\Models\Navigation::class, |
21
|
|
|
'benefits' => \Igorsgm\Ghost\Models\Benefit::class, |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array|string[] |
26
|
|
|
*/ |
27
|
|
|
private array $modelProperties = [ |
28
|
|
|
'tier' => \Igorsgm\Ghost\Models\Resources\Tier::class, |
29
|
|
|
'primaryAuthor' => \Igorsgm\Ghost\Models\Resources\Author::class, |
30
|
|
|
'primaryTag' => \Igorsgm\Ghost\Models\Resources\Tag::class, |
31
|
|
|
'monthlyPrice' => \Igorsgm\Ghost\Models\Price::class, |
32
|
|
|
'yearlyPrice' => \Igorsgm\Ghost\Models\Price::class, |
33
|
|
|
'stripePrices' => \Igorsgm\Ghost\Models\Price::class, |
34
|
|
|
'price' => \Igorsgm\Ghost\Models\Price::class, |
35
|
|
|
'offer' => \Igorsgm\Ghost\Models\Resources\Offer::class, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The attributes that should be mutated to Carbon dates. |
40
|
|
|
*/ |
41
|
|
|
private array $dates = [ |
42
|
|
|
'createdAt', |
43
|
|
|
'updatedAt', |
44
|
|
|
'publishedAt', |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Seo |
49
|
|
|
*/ |
50
|
|
|
protected $seo; |
51
|
|
|
|
52
|
123 |
|
public function __construct(array $data = []) |
53
|
|
|
{ |
54
|
123 |
|
if (! empty($data)) { |
55
|
94 |
|
$this->fill($data); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
94 |
|
private function fill(array $data) |
63
|
|
|
{ |
64
|
94 |
|
if (in_array(get_class($this), config('ghost.seo.models-with'))) { |
65
|
56 |
|
$seoProperties = Arr::only($data, config('ghost.seo.properties')); |
66
|
56 |
|
$data = Arr::except($data, config('ghost.seo.properties')); |
67
|
|
|
} |
68
|
|
|
|
69
|
94 |
|
foreach ($data as $key => $value) { |
70
|
94 |
|
$property = Str::camel($key); |
71
|
94 |
|
$this->{$property} = $this->getCastedValue($property, $value) ?? null; |
72
|
|
|
} |
73
|
|
|
|
74
|
94 |
|
if (! empty($seoProperties)) { |
75
|
56 |
|
$this->seo = new Seo($seoProperties); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Fill a model property with a collection of models, when $property is inside $modelCollectionProperties keys |
81
|
|
|
* |
82
|
|
|
* @param string $property |
83
|
|
|
* @param mixed $value |
84
|
|
|
* @return \Illuminate\Support\Collection |
85
|
|
|
*/ |
86
|
43 |
|
private function castModelCollectionProperty($property, $value) |
87
|
|
|
{ |
88
|
43 |
|
$propertyModel = $this->modelCollectionProperties[$property]; |
89
|
|
|
|
90
|
43 |
|
return collect($value)->map(function ($item) use (&$propertyModel) { |
91
|
41 |
|
return new $propertyModel($item); |
92
|
43 |
|
}); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Fill a model property with a specific model, when $property is inside $modelProperties keys |
97
|
|
|
* |
98
|
|
|
* @param string $property |
99
|
|
|
* @param mixed $value |
100
|
|
|
* @return BaseModel|null |
101
|
|
|
*/ |
102
|
41 |
|
private function castModelProperty($property, $value) |
103
|
|
|
{ |
104
|
41 |
|
$propertyModel = $this->modelProperties[$property]; |
105
|
|
|
|
106
|
41 |
|
return ! empty($value) ? new $propertyModel($value) : null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string|array $value |
111
|
|
|
* @return Carbon|BaseModel|\Illuminate\Support\Collection|mixed|null |
112
|
|
|
*/ |
113
|
94 |
|
private function getCastedValue(string $property, $value) |
114
|
|
|
{ |
115
|
94 |
|
if (array_key_exists($property, $this->modelCollectionProperties)) { |
116
|
43 |
|
$value = $this->castModelCollectionProperty($property, $value); |
117
|
|
|
} |
118
|
|
|
|
119
|
94 |
|
if (array_key_exists($property, $this->modelProperties)) { |
120
|
41 |
|
$value = $this->castModelProperty($property, $value); |
121
|
|
|
} |
122
|
|
|
|
123
|
94 |
|
if (in_array($property, $this->dates)) { |
124
|
63 |
|
$value = Carbon::parse($value); |
125
|
|
|
} |
126
|
|
|
|
127
|
94 |
|
return $value; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|