1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Bookings\Traits; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Rinvex\Bookings\Models\BookableBooking; |
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 availability model name. |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
abstract public static function getAvailabilityModel(): string; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Boot the Bookable trait for the model. |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
public static function bootBookable() |
73
|
|
|
{ |
74
|
|
|
static::deleted(function (self $model) { |
75
|
|
|
$model->bookings()->delete(); |
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Attach the given bookings to the model. |
81
|
|
|
* |
82
|
|
|
* @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids |
|
|
|
|
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public function setBookingsAttribute($bookings): void |
87
|
|
|
{ |
88
|
|
|
static::saved(function (self $model) use ($bookings) { |
|
|
|
|
89
|
|
|
$this->bookings()->sync($bookings); |
90
|
|
|
}); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Attach the given rates to the model. |
95
|
|
|
* |
96
|
|
|
* @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids |
|
|
|
|
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
public function setRatesAttribute($rates): void |
101
|
|
|
{ |
102
|
|
|
static::saved(function (self $model) use ($rates) { |
|
|
|
|
103
|
|
|
$this->rates()->sync($rates); |
104
|
|
|
}); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Attach the given availabilities to the model. |
109
|
|
|
* |
110
|
|
|
* @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids |
|
|
|
|
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function setAvailabilitiesAttribute($availabilities): void |
115
|
|
|
{ |
116
|
|
|
static::saved(function (self $model) use ($availabilities) { |
|
|
|
|
117
|
|
|
$this->availabilities()->sync($availabilities); |
118
|
|
|
}); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* The resource may have many bookings. |
123
|
|
|
* |
124
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
125
|
|
|
*/ |
126
|
|
|
public function bookings(): MorphMany |
127
|
|
|
{ |
128
|
|
|
return $this->morphMany(static::getBookingModel(), 'bookable'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get bookings by the given customer. |
133
|
|
|
* |
134
|
|
|
* @param \Illuminate\Database\Eloquent\Model $customer |
135
|
|
|
* |
136
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
137
|
|
|
*/ |
138
|
|
|
public function bookingsBy(Model $customer): MorphMany |
139
|
|
|
{ |
140
|
|
|
return $this->bookings()->where('customer_type', $customer->getMorphClass())->where('customer_id', $customer->getKey()); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* The resource may have many availabilities. |
145
|
|
|
* |
146
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
147
|
|
|
*/ |
148
|
|
|
public function availabilities(): MorphMany |
149
|
|
|
{ |
150
|
|
|
return $this->morphMany(static::getAvailabilityModel(), 'bookable'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* The resource may have many rates. |
155
|
|
|
* |
156
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
157
|
|
|
*/ |
158
|
|
|
public function rates(): MorphMany |
159
|
|
|
{ |
160
|
|
|
return $this->morphMany(static::getRateModel(), 'bookable'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Book the model for the given customer at the given dates with the given price. |
165
|
|
|
* |
166
|
|
|
* @param \Illuminate\Database\Eloquent\Model $customer |
167
|
|
|
* @param string $startsAt |
168
|
|
|
* @param string $endsAt |
169
|
|
|
* |
170
|
|
|
* @return \Rinvex\Bookings\Models\BookableBooking |
171
|
|
|
*/ |
172
|
|
|
public function newBooking(Model $customer, string $startsAt, string $endsAt): BookableBooking |
173
|
|
|
{ |
174
|
|
|
return $this->bookings()->create([ |
175
|
|
|
'bookable_id' => static::getKey(), |
176
|
|
|
'bookable_type' => static::getMorphClass(), |
177
|
|
|
'customer_id' => $customer->getKey(), |
178
|
|
|
'customer_type' => $customer->getMorphClass(), |
179
|
|
|
'starts_at' => (new Carbon($startsAt))->toDateTimeString(), |
180
|
|
|
'ends_at' => (new Carbon($endsAt))->toDateTimeString(), |
181
|
|
|
]); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
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.