Completed
Push — develop ( bdf589...e66eda )
by Abdelrahman
17:14 queued 10s
created

Value::attribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Attributes\Models;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Rinvex\Support\Traits\ValidatingTrait;
9
use Rinvex\Attributes\Support\ValueCollection;
10
use Illuminate\Database\Eloquent\Relations\MorphTo;
11
use Illuminate\Database\Eloquent\Relations\BelongsTo;
12
13
abstract class Value extends Model
14
{
15
    use ValidatingTrait;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected $fillable = [
21
        'content',
22
        'attribute_id',
23
        'entity_id',
24
        'entity_type',
25
    ];
26
27
    /**
28
     * Determine if value should push to relations when saving.
29
     *
30
     * @var bool
31
     */
32
    protected $shouldPush = false;
33
34
    /**
35
     * The default rules that the model will validate against.
36
     *
37
     * @var array
38
     */
39
    protected $rules = [];
40
41
    /**
42
     * Whether the model should throw a
43
     * ValidationException if it fails validation.
44
     *
45
     * @var bool
46
     */
47
    protected $throwValidationExceptions = true;
48
49
    /**
50
     * Relationship to the attribute entity.
51
     *
52
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
53
     */
54
    public function attribute(): BelongsTo
55
    {
56
        return $this->belongsTo(Attribute::class, 'attribute_id', 'id', 'attribute');
57
    }
58
59
    /**
60
     * Polymorphic relationship to the entity instance.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
63
     */
64
    public function entity(): MorphTo
65
    {
66
        return $this->morphTo('entity', 'entity_type', 'entity_id', 'id');
67
    }
68
69
    /**
70
     * Check if value should push to relations when saving.
71
     *
72
     * @return bool
73
     */
74
    public function shouldPush(): bool
75
    {
76
        return $this->shouldPush;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function newCollection(array $models = [])
83
    {
84
        return new ValueCollection($models);
85
    }
86
}
87