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