1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Laravel Love. |
5
|
|
|
* |
6
|
|
|
* (c) Anton Komarev <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Cog\Laravel\Love\Reaction\Models; |
15
|
|
|
|
16
|
|
|
use Cog\Contracts\Love\Reactant\Models\Reactant as ReactantContract; |
17
|
|
|
use Cog\Contracts\Love\Reacter\Models\Reacter as ReacterContract; |
18
|
|
|
use Cog\Contracts\Love\Reaction\Exceptions\RateOutOfRange; |
19
|
|
|
use Cog\Contracts\Love\Reaction\Models\Reaction as ReactionContract; |
20
|
|
|
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeContract; |
21
|
|
|
use Cog\Laravel\Love\Reactant\Models\Reactant; |
22
|
|
|
use Cog\Laravel\Love\Reacter\Models\Reacter; |
23
|
|
|
use Cog\Laravel\Love\ReactionType\Models\ReactionType; |
24
|
|
|
use Cog\Laravel\Love\Support\Database\Eloquent\Model; |
25
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
26
|
|
|
|
27
|
|
|
final class Reaction extends Model implements |
28
|
|
|
ReactionContract |
29
|
|
|
{ |
30
|
|
|
public const RATE_DEFAULT = 1.0; |
31
|
|
|
|
32
|
|
|
public const RATE_MIN = 0.01; |
33
|
|
|
|
34
|
|
|
public const RATE_MAX = 99.99; |
35
|
|
|
|
36
|
|
|
protected $table = 'love_reactions'; |
37
|
|
|
|
38
|
|
|
protected $attributes = [ |
39
|
|
|
'rate' => self::RATE_DEFAULT, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
protected $fillable = [ |
43
|
|
|
'reactant_id', |
44
|
|
|
'reaction_type_id', |
45
|
|
|
'rate', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
protected $casts = [ |
49
|
|
|
'id' => 'string', |
50
|
|
|
'rate' => 'float', |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
public function reactant(): BelongsTo |
54
|
|
|
{ |
55
|
|
|
return $this->belongsTo(Reactant::class, 'reactant_id'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function reacter(): BelongsTo |
59
|
|
|
{ |
60
|
|
|
return $this->belongsTo(Reacter::class, 'reacter_id'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function type(): BelongsTo |
64
|
|
|
{ |
65
|
|
|
return $this->belongsTo(ReactionType::class, 'reaction_type_id'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getId(): string |
69
|
|
|
{ |
70
|
|
|
return $this->getAttributeValue('id'); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getReactant(): ReactantContract |
74
|
|
|
{ |
75
|
|
|
return $this->getAttribute('reactant'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getReacter(): ReacterContract |
79
|
|
|
{ |
80
|
|
|
return $this->getAttribute('reacter'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getType(): ReactionTypeContract |
84
|
|
|
{ |
85
|
|
|
return $this->getAttribute('type'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getRate(): float |
89
|
|
|
{ |
90
|
|
|
return $this->getAttributeValue('rate'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getWeight(): float |
94
|
|
|
{ |
95
|
|
|
return $this->getType()->getMass() * $this->getRate(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function setRateAttribute( |
99
|
|
|
?float $rate |
100
|
|
|
): void { |
101
|
|
|
if (!is_null($rate) && ($rate < self::RATE_MIN || $rate > self::RATE_MAX)) { |
102
|
|
|
throw RateOutOfRange::withValue($rate); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->attributes['rate'] = $rate; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function isOfType( |
109
|
|
|
ReactionTypeContract $reactionType |
110
|
|
|
): bool { |
111
|
|
|
return $this->getType()->isEqualTo($reactionType); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function isNotOfType( |
115
|
|
|
ReactionTypeContract $reactionType |
116
|
|
|
): bool { |
117
|
|
|
return $this->getType()->isNotEqualTo($reactionType); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function isToReactant( |
121
|
|
|
ReactantContract $reactant |
122
|
|
|
): bool { |
123
|
|
|
return $this->getReactant()->isEqualTo($reactant); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function isNotToReactant( |
127
|
|
|
ReactantContract $reactant |
128
|
|
|
): bool { |
129
|
|
|
return !$this->isToReactant($reactant); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function isByReacter( |
133
|
|
|
ReacterContract $reacter |
134
|
|
|
): bool { |
135
|
|
|
return $this->getReacter()->isEqualTo($reacter); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function isNotByReacter( |
139
|
|
|
ReacterContract $reacter |
140
|
|
|
): bool { |
141
|
|
|
return !$this->isByReacter($reacter); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|