|
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\TicketableBooking; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
|
10
|
|
|
|
|
11
|
|
|
trait Ticketable |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Register a saved model event with the dispatcher. |
|
15
|
|
|
* |
|
16
|
|
|
* @param \Closure|string $callback |
|
17
|
|
|
* |
|
18
|
|
|
* @return void |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract public static function saved($callback); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Register a deleted model event with the dispatcher. |
|
24
|
|
|
* |
|
25
|
|
|
* @param \Closure|string $callback |
|
26
|
|
|
* |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract public static function deleted($callback); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Define a polymorphic one-to-many relationship. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $related |
|
35
|
|
|
* @param string $name |
|
36
|
|
|
* @param string $type |
|
|
|
|
|
|
37
|
|
|
* @param string $id |
|
|
|
|
|
|
38
|
|
|
* @param string $localKey |
|
|
|
|
|
|
39
|
|
|
* |
|
40
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
41
|
|
|
*/ |
|
42
|
|
|
abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null); |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get the booking model name. |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
abstract public function getBookingModel(): string; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the ticket model name. |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
abstract public function getTicketModel(): string; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Boot the Ticketable trait for the model. |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function bootTicketable() |
|
64
|
|
|
{ |
|
65
|
|
|
static::deleted(function (self $model) { |
|
66
|
|
|
$model->bookings()->delete(); |
|
67
|
|
|
}); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Attach the given bookings to the model. |
|
72
|
|
|
* |
|
73
|
|
|
* @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids |
|
|
|
|
|
|
74
|
|
|
* @param mixed $bookings |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setBookingsAttribute($bookings): void |
|
79
|
|
|
{ |
|
80
|
|
|
static::saved(function (self $model) use ($bookings) { |
|
|
|
|
|
|
81
|
|
|
$this->bookings()->sync($bookings); |
|
82
|
|
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* The resource may have many tickets. |
|
87
|
|
|
* |
|
88
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
89
|
|
|
*/ |
|
90
|
|
|
public function tickets(): MorphMany |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->morphMany(static::getTicketModel(), 'ticketable'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* The resource may have many bookings. |
|
97
|
|
|
* |
|
98
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
99
|
|
|
*/ |
|
100
|
|
|
public function bookings(): MorphMany |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->morphMany(static::getBookingModel(), 'ticketable'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get bookings by the given customer. |
|
107
|
|
|
* |
|
108
|
|
|
* @param \Illuminate\Database\Eloquent\Model $customer |
|
109
|
|
|
* |
|
110
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
111
|
|
|
*/ |
|
112
|
|
|
public function bookingsBy(Model $customer): MorphMany |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->bookings()->where('customer_type', $customer->getMorphClass())->where('customer_id', $customer->getKey()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Book the model for the given customer at the given dates with the given price. |
|
119
|
|
|
* |
|
120
|
|
|
* @param \Illuminate\Database\Eloquent\Model $customer |
|
121
|
|
|
* @param float $paid |
|
122
|
|
|
* @param string $currency |
|
123
|
|
|
* |
|
124
|
|
|
* @return \Rinvex\Bookings\Models\TicketableBooking |
|
125
|
|
|
*/ |
|
126
|
|
|
public function newBooking(Model $customer, float $paid, string $currency): TicketableBooking |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->bookings()->create([ |
|
129
|
|
|
'ticketable_id' => static::getKey(), |
|
130
|
|
|
'ticketable_type' => static::getMorphClass(), |
|
131
|
|
|
'customer_id' => $customer->getKey(), |
|
132
|
|
|
'customer_type' => $customer->getMorphClass(), |
|
133
|
|
|
'paid' => $paid, |
|
134
|
|
|
'currency' => $currency, |
|
135
|
|
|
]); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
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.