|
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 HasBookings |
|
12
|
|
|
{ |
|
13
|
|
|
use BookingScopes; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Define a polymorphic one-to-many relationship. |
|
17
|
|
|
* |
|
18
|
|
|
* @param string $related |
|
19
|
|
|
* @param string $name |
|
20
|
|
|
* @param string $type |
|
|
|
|
|
|
21
|
|
|
* @param string $id |
|
|
|
|
|
|
22
|
|
|
* @param string $localKey |
|
|
|
|
|
|
23
|
|
|
* |
|
24
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null); |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Get the booking model name. |
|
30
|
|
|
* |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
abstract public static function getBookingModel(): string; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Boot the HasBookings trait for the model. |
|
37
|
|
|
* |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function bootHasBookings() |
|
41
|
|
|
{ |
|
42
|
|
|
static::deleted(function (self $model) { |
|
43
|
|
|
$model->bookings()->delete(); |
|
44
|
|
|
}); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* The customer may have many bookings. |
|
49
|
|
|
* |
|
50
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
51
|
|
|
*/ |
|
52
|
|
|
public function bookings(): MorphMany |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->morphMany(static::getBookingModel(), 'customer'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get bookings of the given resource. |
|
59
|
|
|
* |
|
60
|
|
|
* @param \Illuminate\Database\Eloquent\Model $bookable |
|
61
|
|
|
* |
|
62
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
63
|
|
|
*/ |
|
64
|
|
|
public function bookingsOf(Model $bookable): MorphMany |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->bookings()->where('bookable_type', $bookable->getMorphClass())->where('bookable_id', $bookable->getKey()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Check if the person booked the given model. |
|
71
|
|
|
* |
|
72
|
|
|
* @param \Illuminate\Database\Eloquent\Model $bookable |
|
73
|
|
|
* |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public function isBooked(Model $bookable): bool |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->bookings()->where('bookable_id', $bookable->getKey())->exists(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Book the given model at the given dates with the given price. |
|
83
|
|
|
* |
|
84
|
|
|
* @param \Illuminate\Database\Eloquent\Model $bookable |
|
85
|
|
|
* @param string $startsAt |
|
86
|
|
|
* @param string $endsAt |
|
87
|
|
|
* |
|
88
|
|
|
* @return \Rinvex\Bookings\Models\BookableBooking |
|
89
|
|
|
*/ |
|
90
|
|
|
public function newBooking(Model $bookable, string $startsAt, string $endsAt): BookableBooking |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->bookings()->create([ |
|
93
|
|
|
'bookable_id' => $bookable->getKey(), |
|
94
|
|
|
'bookable_type' => $bookable->getMorphClass(), |
|
95
|
|
|
'customer_id' => $this->getKey(), |
|
|
|
|
|
|
96
|
|
|
'customer_type' => $this->getMorphClass(), |
|
|
|
|
|
|
97
|
|
|
'starts_at' => $startsAt, |
|
98
|
|
|
'ends_at' => $endsAt, |
|
99
|
|
|
]); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check looks for
@paramannotations 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.