1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Bookings\Traits; |
6
|
|
|
|
7
|
|
|
use Rinvex\Bookings\Models\Rate; |
8
|
|
|
use Rinvex\Bookings\Models\Price; |
9
|
|
|
use Rinvex\Bookings\Models\Booking; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
12
|
|
|
|
13
|
|
|
trait Bookable |
14
|
|
|
{ |
15
|
|
|
use BookingScopes; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The resource may have many bookings. |
19
|
|
|
* |
20
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
21
|
|
|
*/ |
22
|
|
|
public function bookings(): MorphMany |
23
|
|
|
{ |
24
|
|
|
return $this->morphMany(config('rinvex.bookings.models.booking'), 'bookable'); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get bookings of the given user. |
29
|
|
|
* |
30
|
|
|
* @param \Illuminate\Database\Eloquent\Model $user |
31
|
|
|
* |
32
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
33
|
|
|
*/ |
34
|
|
|
public function bookingsOf(Model $user): MorphMany |
35
|
|
|
{ |
36
|
|
|
return $this->bookings()->where('user_type', $user->getMorphClass())->where('user_id', $user->getKey()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The resource may have many rates. |
41
|
|
|
* |
42
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
43
|
|
|
*/ |
44
|
|
|
public function rates(): MorphMany |
45
|
|
|
{ |
46
|
|
|
return $this->morphMany(config('rinvex.bookings.models.rate'), 'bookable'); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The resource may have many prices. |
51
|
|
|
* |
52
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
53
|
|
|
*/ |
54
|
|
|
public function prices(): MorphMany |
55
|
|
|
{ |
56
|
|
|
return $this->morphMany(config('rinvex.bookings.models.price'), 'bookable'); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Book the model for the given user at the given dates with the given price. |
61
|
|
|
* |
62
|
|
|
* @param \Illuminate\Database\Eloquent\Model $user |
63
|
|
|
* @param string $startsAt |
64
|
|
|
* @param string $endsAt |
65
|
|
|
* |
66
|
|
|
* @return \Rinvex\Bookings\Models\Booking |
67
|
|
|
*/ |
68
|
|
|
public function newBooking(Model $user, string $startsAt, string $endsAt): Booking |
69
|
|
|
{ |
70
|
|
|
return $this->bookings()->create([ |
71
|
|
|
'bookable_id' => static::getKey(), |
72
|
|
|
'bookable_type' => static::getMorphClass(), |
73
|
|
|
'user_id' => $user->getKey(), |
74
|
|
|
'user_type' => $user->getMorphClass(), |
75
|
|
|
'starts_at' => (new Carbon($startsAt))->toDateTimeString(), |
76
|
|
|
'ends_at' => (new Carbon($endsAt))->toDateTimeString(), |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create a new booking rate. |
82
|
|
|
* |
83
|
|
|
* @param float $percentage |
84
|
|
|
* @param string $operator |
85
|
|
|
* @param int $amount |
86
|
|
|
* |
87
|
|
|
* @return \Rinvex\Bookings\Models\Rate |
88
|
|
|
*/ |
89
|
|
|
public function newRate(float $percentage, string $operator, int $amount): Rate |
90
|
|
|
{ |
91
|
|
|
return $this->rates()->create([ |
92
|
|
|
'bookable_id' => static::getKey(), |
93
|
|
|
'bookable_type' => static::getMorphClass(), |
94
|
|
|
'percentage' => $percentage, |
95
|
|
|
'operator' => $operator, |
96
|
|
|
'amount' => $amount, |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Create a new booking price. |
102
|
|
|
* |
103
|
|
|
* @param string $weekday |
104
|
|
|
* @param string $startsAt |
105
|
|
|
* @param string $endsAt |
106
|
|
|
* @param float $percentage |
107
|
|
|
* |
108
|
|
|
* @return \Rinvex\Bookings\Models\Price |
109
|
|
|
*/ |
110
|
|
|
public function newPrice(string $weekday, string $startsAt, string $endsAt, float $percentage): Price |
111
|
|
|
{ |
112
|
|
|
return $this->prices()->create([ |
113
|
|
|
'bookable_id' => static::getKey(), |
114
|
|
|
'bookable_type' => static::getMorphClass(), |
115
|
|
|
'percentage' => $percentage, |
116
|
|
|
'weekday' => $weekday, |
117
|
|
|
'starts_at' => (new Carbon($startsAt))->toTimeString(), |
118
|
|
|
'ends_at' => (new Carbon($endsAt))->toTimeString(), |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.