1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Bookings\Traits; |
6
|
|
|
|
7
|
|
|
use Rinvex\Bookings\Models\BookableBooking; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
10
|
|
|
|
11
|
|
|
trait Bookable |
12
|
|
|
{ |
13
|
|
|
use BookingScopes; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Register a saved model event with the dispatcher. |
17
|
|
|
* |
18
|
|
|
* @param \Closure|string $callback |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
abstract public static function saved($callback); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Register a deleted model event with the dispatcher. |
26
|
|
|
* |
27
|
|
|
* @param \Closure|string $callback |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
abstract public static function deleted($callback); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Define a polymorphic one-to-many relationship. |
35
|
|
|
* |
36
|
|
|
* @param string $related |
37
|
|
|
* @param string $name |
38
|
|
|
* @param string $type |
|
|
|
|
39
|
|
|
* @param string $id |
|
|
|
|
40
|
|
|
* @param string $localKey |
|
|
|
|
41
|
|
|
* |
42
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
43
|
|
|
*/ |
44
|
|
|
abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the booking model name. |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
abstract public static function getBookingModel(): string; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the rate model name. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
abstract public static function getRateModel(): string; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the addon model name. |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
abstract public static function getAddonModel(): string; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the availability model name. |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
abstract public static function getAvailabilityModel(): string; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Boot the Bookable trait for the model. |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public static function bootBookable() |
80
|
|
|
{ |
81
|
|
|
static::deleted(function (self $model) { |
82
|
|
|
$model->bookings()->delete(); |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Attach the given bookings to the model. |
88
|
|
|
* |
89
|
|
|
* @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids |
|
|
|
|
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function setBookingsAttribute($bookings): void |
94
|
|
|
{ |
95
|
|
|
static::saved(function (self $model) use ($bookings) { |
|
|
|
|
96
|
|
|
$this->bookings()->sync($bookings); |
97
|
|
|
}); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Attach the given rates to the model. |
102
|
|
|
* |
103
|
|
|
* @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids |
|
|
|
|
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function setRatesAttribute($rates): void |
108
|
|
|
{ |
109
|
|
|
static::saved(function (self $model) use ($rates) { |
|
|
|
|
110
|
|
|
$this->rates()->sync($rates); |
111
|
|
|
}); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Attach the given addons to the model. |
116
|
|
|
* |
117
|
|
|
* @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids |
|
|
|
|
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
public function setAddonsAttribute($addons): void |
122
|
|
|
{ |
123
|
|
|
static::saved(function (self $model) use ($addons) { |
|
|
|
|
124
|
|
|
$this->addons()->sync($addons); |
125
|
|
|
}); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Attach the given availabilities to the model. |
130
|
|
|
* |
131
|
|
|
* @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids |
|
|
|
|
132
|
|
|
* |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
|
|
public function setAvailabilitiesAttribute($availabilities): void |
136
|
|
|
{ |
137
|
|
|
static::saved(function (self $model) use ($availabilities) { |
|
|
|
|
138
|
|
|
$this->availabilities()->sync($availabilities); |
139
|
|
|
}); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* The resource may have many bookings. |
144
|
|
|
* |
145
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
146
|
|
|
*/ |
147
|
|
|
public function bookings(): MorphMany |
148
|
|
|
{ |
149
|
|
|
return $this->morphMany(static::getBookingModel(), 'bookable'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get bookings by the given customer. |
154
|
|
|
* |
155
|
|
|
* @param \Illuminate\Database\Eloquent\Model $customer |
156
|
|
|
* |
157
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
158
|
|
|
*/ |
159
|
|
|
public function bookingsBy(Model $customer): MorphMany |
160
|
|
|
{ |
161
|
|
|
return $this->bookings()->where('customer_type', $customer->getMorphClass())->where('customer_id', $customer->getKey()); |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* The resource may have many addons. |
166
|
|
|
* |
167
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
168
|
|
|
*/ |
169
|
|
|
public function addons(): MorphMany |
170
|
|
|
{ |
171
|
|
|
return $this->morphMany(static::getAddonModel(), 'bookable'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* The resource may have many availabilities. |
176
|
|
|
* |
177
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
178
|
|
|
*/ |
179
|
|
|
public function availabilities(): MorphMany |
180
|
|
|
{ |
181
|
|
|
return $this->morphMany(static::getAvailabilityModel(), 'bookable'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* The resource may have many rates. |
186
|
|
|
* |
187
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
188
|
|
|
*/ |
189
|
|
|
public function rates(): MorphMany |
190
|
|
|
{ |
191
|
|
|
return $this->morphMany(static::getRateModel(), 'bookable'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Book the model for the given customer at the given dates with the given price. |
196
|
|
|
* |
197
|
|
|
* @param \Illuminate\Database\Eloquent\Model $customer |
198
|
|
|
* @param string $startsAt |
199
|
|
|
* @param string $endsAt |
200
|
|
|
* |
201
|
|
|
* @return \Rinvex\Bookings\Models\BookableBooking |
202
|
|
|
*/ |
203
|
|
|
public function newBooking(Model $customer, string $startsAt, string $endsAt): BookableBooking |
204
|
|
|
{ |
205
|
|
|
return $this->bookings()->create([ |
206
|
|
|
'bookable_id' => static::getKey(), |
207
|
|
|
'bookable_type' => static::getMorphClass(), |
208
|
|
|
'customer_id' => $customer->getKey(), |
209
|
|
|
'customer_type' => $customer->getMorphClass(), |
210
|
|
|
'starts_at' => (new Carbon($startsAt))->toDateTimeString(), |
211
|
|
|
'ends_at' => (new Carbon($endsAt))->toDateTimeString(), |
212
|
|
|
]); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.