|
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 BookingCustomer |
|
12
|
|
|
{ |
|
13
|
|
|
use BookingScopes; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The customer may have many bookings. |
|
17
|
|
|
* |
|
18
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
19
|
|
|
*/ |
|
20
|
|
|
public function bookings(): MorphMany |
|
21
|
|
|
{ |
|
22
|
|
|
return $this->morphMany(config('rinvex.bookings.models.booking'), 'customer'); |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Get bookings of the given bookable. |
|
27
|
|
|
* |
|
28
|
|
|
* @param \Illuminate\Database\Eloquent\Model $bookable |
|
|
|
|
|
|
29
|
|
|
* |
|
30
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
31
|
|
|
*/ |
|
32
|
|
|
public function bookingsOfBookable(string $bookable): MorphMany |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->bookings()->where('bookable_type', $bookable->getMorphClass())->where('bookable_id', $bookable->getKey()); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Check if the person booked the given model. |
|
39
|
|
|
* |
|
40
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
|
41
|
|
|
* |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
|
|
public function isBooked(Model $model): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->bookings()->where('bookable_id', $model->getKey())->where('bookable_type', get_class($model))->exists(); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Book the given model at the given dates with the given price. |
|
51
|
|
|
* |
|
52
|
|
|
* @param \Illuminate\Database\Eloquent\Model $bookable |
|
53
|
|
|
* @param string $starts |
|
54
|
|
|
* @param string $ends |
|
55
|
|
|
* @param float $price |
|
56
|
|
|
* |
|
57
|
|
|
* @return \Rinvex\Bookings\Models\Booking |
|
58
|
|
|
*/ |
|
59
|
|
|
public function newBooking(Model $bookable, string $starts, string $ends, float $price): Booking |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->bookings()->create([ |
|
62
|
|
|
'bookable_id' => $bookable->getKey(), |
|
63
|
|
|
'bookable_type' => $bookable->getMorphClass(), |
|
64
|
|
|
'customer_id' => $this->getKey(), |
|
|
|
|
|
|
65
|
|
|
'customer_type' => $this->getMorphClass(), |
|
|
|
|
|
|
66
|
|
|
'starts_at' => $starts, |
|
67
|
|
|
'ends_at' => $ends, |
|
68
|
|
|
'price' => $price, |
|
69
|
|
|
]); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
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
Idableprovides a methodequalsIdthat 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.