|
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 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
|
|
|
* The user may have many bookings. |
|
30
|
|
|
* |
|
31
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
32
|
|
|
*/ |
|
33
|
|
|
public function bookings(): MorphMany |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->morphMany(config('rinvex.bookings.models.booking'), 'user'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get bookings of the given resource. |
|
40
|
|
|
* |
|
41
|
|
|
* @param \Illuminate\Database\Eloquent\Model $bookable |
|
42
|
|
|
* |
|
43
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
44
|
|
|
*/ |
|
45
|
|
|
public function bookingsOf(Model $bookable): MorphMany |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->bookings()->where('bookable_type', $bookable->getMorphClass())->where('bookable_id', $bookable->getKey()); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Check if the person booked the given model. |
|
52
|
|
|
* |
|
53
|
|
|
* @param \Illuminate\Database\Eloquent\Model $bookable |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
public function isBooked(Model $bookable): bool |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->bookings()->where('bookable_id', $bookable->getKey())->exists(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Book the given model at the given dates with the given price. |
|
64
|
|
|
* |
|
65
|
|
|
* @param \Illuminate\Database\Eloquent\Model $bookable |
|
66
|
|
|
* @param string $startsAt |
|
67
|
|
|
* @param string $endsAt |
|
68
|
|
|
* |
|
69
|
|
|
* @return \Rinvex\Bookings\Models\Booking |
|
70
|
|
|
*/ |
|
71
|
|
|
public function newBooking(Model $bookable, string $startsAt, string $endsAt): Booking |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->bookings()->create([ |
|
74
|
|
|
'bookable_id' => $bookable->getKey(), |
|
75
|
|
|
'bookable_type' => $bookable->getMorphClass(), |
|
76
|
|
|
'user_id' => $this->getKey(), |
|
|
|
|
|
|
77
|
|
|
'user_type' => $this->getMorphClass(), |
|
|
|
|
|
|
78
|
|
|
'starts_at' => $startsAt, |
|
79
|
|
|
'ends_at' => $endsAt, |
|
80
|
|
|
]); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
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.