1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Subscriptions\Models; |
6
|
|
|
|
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
use Spatie\Sluggable\SlugOptions; |
9
|
|
|
use Rinvex\Support\Traits\HasSlug; |
10
|
|
|
use Spatie\EloquentSortable\Sortable; |
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
12
|
|
|
use Rinvex\Cacheable\CacheableEloquent; |
13
|
|
|
use Rinvex\Subscriptions\Services\Period; |
14
|
|
|
use Rinvex\Support\Traits\HasTranslations; |
15
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
16
|
|
|
use Spatie\EloquentSortable\SortableTrait; |
17
|
|
|
use Rinvex\Subscriptions\Traits\BelongsToPlan; |
18
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
19
|
|
|
use Rinvex\Subscriptions\Contracts\PlanFeatureContract; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Rinvex\Subscriptions\Models\PlanFeature. |
23
|
|
|
* |
24
|
|
|
* @property int $id |
25
|
|
|
* @property int $plan_id |
26
|
|
|
* @property string $slug |
27
|
|
|
* @property array $name |
28
|
|
|
* @property array $description |
|
|
|
|
29
|
|
|
* @property string $value |
30
|
|
|
* @property int $resettable_period |
|
|
|
|
31
|
|
|
* @property string $resettable_interval |
|
|
|
|
32
|
|
|
* @property int $sort_order |
|
|
|
|
33
|
|
|
* @property \Carbon\Carbon $created_at |
|
|
|
|
34
|
|
|
* @property \Carbon\Carbon $updated_at |
|
|
|
|
35
|
|
|
* @property \Carbon\Carbon $deleted_at |
|
|
|
|
36
|
|
|
* @property-read \Rinvex\Subscriptions\Models\Plan $plan |
37
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Rinvex\Subscriptions\Models\PlanSubscriptionUsage[] $usage |
38
|
|
|
* |
39
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature byPlanId($planId) |
40
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature ordered($direction = 'asc') |
|
|
|
|
41
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereCreatedAt($value) |
42
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereDeletedAt($value) |
43
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereDescription($value) |
|
|
|
|
44
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereId($value) |
45
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereName($value) |
46
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature wherePlanId($value) |
47
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereResettableInterval($value) |
|
|
|
|
48
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereResettablePeriod($value) |
|
|
|
|
49
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereSlug($value) |
50
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereSortOrder($value) |
51
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereUpdatedAt($value) |
52
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Subscriptions\Models\PlanFeature whereValue($value) |
53
|
|
|
* @mixin \Eloquent |
54
|
|
|
*/ |
55
|
|
|
class PlanFeature extends Model implements PlanFeatureContract, Sortable |
56
|
|
|
{ |
57
|
|
|
use HasSlug; |
58
|
|
|
use BelongsToPlan; |
59
|
|
|
use SortableTrait; |
60
|
|
|
use HasTranslations; |
61
|
|
|
use ValidatingTrait; |
62
|
|
|
use CacheableEloquent; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
protected $fillable = [ |
68
|
|
|
'plan_id', |
69
|
|
|
'slug', |
70
|
|
|
'name', |
71
|
|
|
'description', |
72
|
|
|
'value', |
73
|
|
|
'resettable_period', |
74
|
|
|
'resettable_interval', |
75
|
|
|
'sort_order', |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
protected $casts = [ |
82
|
|
|
'plan_id' => 'integer', |
83
|
|
|
'slug' => 'string', |
84
|
|
|
'value' => 'string', |
85
|
|
|
'resettable_period' => 'integer', |
86
|
|
|
'resettable_interval' => 'string', |
87
|
|
|
'sort_order' => 'integer', |
88
|
|
|
'deleted_at' => 'datetime', |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
protected $observables = [ |
95
|
|
|
'validating', |
96
|
|
|
'validated', |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* The attributes that are translatable. |
101
|
|
|
* |
102
|
|
|
* @var array |
103
|
|
|
*/ |
104
|
|
|
public $translatable = [ |
105
|
|
|
'name', |
106
|
|
|
'description', |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* The sortable settings. |
111
|
|
|
* |
112
|
|
|
* @var array |
113
|
|
|
*/ |
114
|
|
|
public $sortable = [ |
115
|
|
|
'order_column_name' => 'sort_order', |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* The default rules that the model will validate against. |
120
|
|
|
* |
121
|
|
|
* @var array |
122
|
|
|
*/ |
123
|
|
|
protected $rules = []; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Whether the model should throw a |
127
|
|
|
* ValidationException if it fails validation. |
128
|
|
|
* |
129
|
|
|
* @var bool |
130
|
|
|
*/ |
131
|
|
|
protected $throwValidationExceptions = true; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Create a new Eloquent model instance. |
135
|
|
|
* |
136
|
|
|
* @param array $attributes |
137
|
|
|
*/ |
138
|
|
|
public function __construct(array $attributes = []) |
139
|
|
|
{ |
140
|
|
|
parent::__construct($attributes); |
141
|
|
|
|
142
|
|
|
$this->setTable(config('rinvex.subscriptions.tables.plan_features')); |
143
|
|
|
$this->setRules([ |
144
|
|
|
'plan_id' => 'required|integer|exists:'.config('rinvex.subscriptions.tables.plans').',id', |
145
|
|
|
'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.subscriptions.tables.plan_features').',slug', |
146
|
|
|
'name' => 'required|string|max:150', |
147
|
|
|
'description' => 'nullable|string|max:10000', |
148
|
|
|
'value' => 'required|string', |
149
|
|
|
'resettable_period' => 'sometimes|integer', |
150
|
|
|
'resettable_interval' => 'sometimes|string|in:h,d,w,m', |
151
|
|
|
'sort_order' => 'nullable|integer|max:10000000', |
152
|
|
|
]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get the options for generating the slug. |
157
|
|
|
* |
158
|
|
|
* @return \Spatie\Sluggable\SlugOptions |
159
|
|
|
*/ |
160
|
|
|
public function getSlugOptions(): SlugOptions |
161
|
|
|
{ |
162
|
|
|
return SlugOptions::create() |
163
|
|
|
->doNotGenerateSlugsOnUpdate() |
164
|
|
|
->generateSlugsFrom('name') |
165
|
|
|
->saveSlugsTo('slug'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* The plan feature may have many subscription usage. |
170
|
|
|
* |
171
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
172
|
|
|
*/ |
173
|
|
|
public function usage(): HasMany |
174
|
|
|
{ |
175
|
|
|
return $this->hasMany(config('rinvex.subscriptions.models.plan_subscription_usage'), 'feature_id', 'id'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get feature's reset date. |
180
|
|
|
* |
181
|
|
|
* @param string $dateFrom |
|
|
|
|
182
|
|
|
* |
183
|
|
|
* @return \Carbon\Carbon |
184
|
|
|
*/ |
185
|
|
|
public function getResetDate(Carbon $dateFrom): Carbon |
186
|
|
|
{ |
187
|
|
|
$period = new Period($this->resettable_interval, $this->resettable_period, $dateFrom ?? new Carbon()); |
188
|
|
|
|
189
|
|
|
return $period->getEndDate(); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.