1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Bookings\Traits; |
6
|
|
|
|
7
|
|
|
use Rinvex\Bookings\Models\Booking; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
10
|
|
|
|
11
|
|
|
trait Bookable |
12
|
|
|
{ |
13
|
|
|
use BookingScopes; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Boot the Bookable trait for the model. |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public static function bootBookable() |
21
|
|
|
{ |
22
|
|
|
static::deleted(function (self $model) { |
23
|
|
|
$model->bookings()->delete(); |
24
|
|
|
}); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The resource may have many bookings. |
29
|
|
|
* |
30
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
31
|
|
|
*/ |
32
|
|
|
public function bookings(): MorphMany |
33
|
|
|
{ |
34
|
|
|
return $this->morphMany(config('rinvex.bookings.models.booking'), 'bookable'); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get bookings of the given customer. |
39
|
|
|
* |
40
|
|
|
* @param \Illuminate\Database\Eloquent\Model $customer |
41
|
|
|
* |
42
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
43
|
|
|
*/ |
44
|
|
|
public function bookingsOf(Model $customer): MorphMany |
45
|
|
|
{ |
46
|
|
|
return $this->bookings()->where('customer_type', $customer->getMorphClass())->where('customer_id', $customer->getKey()); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The resource may have many addons. |
51
|
|
|
* |
52
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
53
|
|
|
*/ |
54
|
|
|
public function addons(): MorphMany |
55
|
|
|
{ |
56
|
|
|
return $this->morphMany(config('rinvex.bookings.models.addon'), 'bookable'); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The resource may have many availabilities. |
61
|
|
|
* |
62
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
63
|
|
|
*/ |
64
|
|
|
public function availabilities(): MorphMany |
65
|
|
|
{ |
66
|
|
|
return $this->morphMany(config('rinvex.bookings.models.availability'), 'bookable'); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The resource may have many rates. |
71
|
|
|
* |
72
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
73
|
|
|
*/ |
74
|
|
|
public function rates(): MorphMany |
75
|
|
|
{ |
76
|
|
|
return $this->morphMany(config('rinvex.bookings.models.rate'), 'bookable'); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Book the model for the given customer at the given dates with the given price. |
81
|
|
|
* |
82
|
|
|
* @param \Illuminate\Database\Eloquent\Model $customer |
83
|
|
|
* @param string $startsAt |
84
|
|
|
* @param string $endsAt |
85
|
|
|
* |
86
|
|
|
* @return \Rinvex\Bookings\Models\Booking |
87
|
|
|
*/ |
88
|
|
|
public function newBooking(Model $customer, string $startsAt, string $endsAt): Booking |
89
|
|
|
{ |
90
|
|
|
return $this->bookings()->create([ |
91
|
|
|
'bookable_id' => static::getKey(), |
92
|
|
|
'bookable_type' => static::getMorphClass(), |
93
|
|
|
'customer_id' => $customer->getKey(), |
94
|
|
|
'customer_type' => $customer->getMorphClass(), |
95
|
|
|
'starts_at' => (new Carbon($startsAt))->toDateTimeString(), |
96
|
|
|
'ends_at' => (new Carbon($endsAt))->toDateTimeString(), |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.