Completed
Push — develop ( 973520...768b39 )
by Abdelrahman
01:27
created

Addon::bookable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Bookings\Models;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Rinvex\Cacheable\CacheableEloquent;
9
use Rinvex\Support\Traits\ValidatingTrait;
10
use Illuminate\Database\Eloquent\Relations\MorphTo;
11
12
/**
13
 * Rinvex\Bookings\Models\Addon.
14
 *
15
 * @property int                                                $id
16
 * @property int                                                $bookable_id
17
 * @property string                                             $bookable_type
18
 * @property string                                             $name
19
 * @property string                                             $title
20
 * @property string                                             $description
21
 * @property float                                              $base_cost
22
 * @property string                                             $base_cost_modifier
23
 * @property float                                              $unit_cost
24
 * @property string                                             $unit_cost_modifier
25
 * @property int                                                $priority
26
 * @property \Carbon\Carbon|null                                $created_at
27
 * @property \Carbon\Carbon|null                                $updated_at
28
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $bookable
29
 *
30
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Addon whereCreatedAt($value)
31
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Addon whereId($value)
32
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Addon whereBaseCost($value)
33
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Addon whereBaseCostModifier($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Addon whereBookableId($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Addon whereBookableType($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Addon whereRange($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Addon whereRangeFrom($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Addon whereRangeTo($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Addon wherePriority($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Addon whereUnitCost($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Addon whereUnitCostModifier($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Bookings\Models\Addon whereUpdatedAt($value)
43
 * @mixin \Eloquent
44
 */
45
class Addon extends Model
46
{
47
    use ValidatingTrait;
48
    use CacheableEloquent;
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected $fillable = [
54
        'bookable_id',
55
        'bookable_type',
56
        'name',
57
        'title',
58
        'description',
59
        'base_cost',
60
        'base_cost_modifier',
61
        'unit_cost',
62
        'unit_cost_modifier',
63
    ];
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    protected $casts = [
69
        'bookable_id' => 'integer',
70
        'bookable_type' => 'string',
71
        'name' => 'string',
72
        'base_cost' => 'float',
73
        'base_cost_modifier' => 'string',
74
        'unit_cost' => 'float',
75
        'unit_cost_modifier' => 'string',
76
    ];
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    protected $observables = [
82
        'validating',
83
        'validated',
84
    ];
85
86
    /**
87
     * The default rules that the model will validate against.
88
     *
89
     * @var array
90
     */
91
    protected $rules = [];
92
93
    /**
94
     * Whether the model should throw a
95
     * ValidationException if it fails validation.
96
     *
97
     * @var bool
98
     */
99
    protected $throwValidationExceptions = true;
100
101
    /**
102
     * Create a new Eloquent model instance.
103
     *
104
     * @param array $attributes
105
     */
106
    public function __construct(array $attributes = [])
107
    {
108
        parent::__construct($attributes);
109
110
        $this->setTable(config('rinvex.bookings.tables.addons'));
111
        $this->setRules([
112
            'bookable_id' => 'required|integer',
113
            'bookable_type' => 'required|string',
114
            'name' => 'required|alpha_dash|max:150|unique:'.config('rinvex.bookings.tables.addons').',name',
115
            'title' => 'required|string|max:150',
116
            'description' => 'nullable|string|max:10000',
117
            'base_cost' => 'required|numeric',
118
            'base_cost_modifier' => 'required|string|in:+,-,×,÷',
119
            'unit_cost' => 'required|numeric',
120
            'unit_cost_modifier' => 'required|string|in:+,-,×,÷',
121
        ]);
122
    }
123
124
    /**
125
     * Get the owning resource model.
126
     *
127
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
128
     */
129
    public function bookable(): MorphTo
130
    {
131
        return $this->morphTo('bookable', 'bookable_type', 'bookable_id');
132
    }
133
}
134