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\Reactant\ReactionTotal\Models; |
15
|
|
|
|
16
|
|
|
use Cog\Contracts\Love\Reactant\Models\Reactant as ReactantInterface; |
17
|
|
|
use Cog\Contracts\Love\Reactant\ReactionTotal\Models\ReactionTotal as ReactionTotalInterface; |
18
|
|
|
use Cog\Laravel\Love\Reactant\Models\Reactant; |
19
|
|
|
use Cog\Laravel\Love\Support\Database\Eloquent\Model; |
20
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
21
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
22
|
|
|
|
23
|
|
|
final class ReactionTotal extends Model implements |
24
|
|
|
ReactionTotalInterface |
25
|
|
|
{ |
26
|
|
|
use HasFactory; |
27
|
|
|
|
28
|
|
|
public const COUNT_DEFAULT = 0; |
29
|
|
|
|
30
|
|
|
public const WEIGHT_DEFAULT = 0.0; |
31
|
|
|
|
32
|
|
|
protected $table = 'love_reactant_reaction_totals'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array<int|float> |
36
|
|
|
*/ |
37
|
|
|
protected $attributes = [ |
38
|
|
|
'count' => self::COUNT_DEFAULT, |
39
|
|
|
'weight' => self::WEIGHT_DEFAULT, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string[] |
44
|
|
|
*/ |
45
|
|
|
protected $fillable = [ |
46
|
|
|
'count', |
47
|
|
|
'weight', |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string[] |
52
|
|
|
*/ |
53
|
|
|
protected $casts = [ |
54
|
|
|
'count' => 'integer', |
55
|
|
|
'weight' => 'float', |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
public function reactant(): BelongsTo |
59
|
|
|
{ |
60
|
|
|
return $this->belongsTo(Reactant::class, 'reactant_id'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getReactant(): ReactantInterface |
64
|
|
|
{ |
65
|
|
|
return $this->getAttribute('reactant'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getCount(): int |
69
|
|
|
{ |
70
|
|
|
return $this->getAttributeValue('count'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getWeight(): float |
74
|
|
|
{ |
75
|
|
|
return $this->getAttributeValue('weight'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function incrementCount( |
79
|
|
|
int $amount, |
80
|
|
|
): void { |
81
|
|
|
$this->increment('count', $amount); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function decrementCount( |
85
|
|
|
int $amount, |
86
|
|
|
): void { |
87
|
|
|
$this->decrement('count', $amount); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function incrementWeight( |
91
|
|
|
float $amount, |
92
|
|
|
): void { |
93
|
|
|
$this->increment('weight', $amount); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function decrementWeight( |
97
|
|
|
float $amount, |
98
|
|
|
): void { |
99
|
|
|
$this->decrement('weight', $amount); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function setCountAttribute( |
103
|
|
|
int | null $count, |
104
|
|
|
): void { |
105
|
|
|
$this->attributes['count'] = $count ?? self::COUNT_DEFAULT; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function setWeightAttribute( |
109
|
|
|
float | null $weight, |
110
|
|
|
): void { |
111
|
|
|
$this->attributes['weight'] = $weight ?? self::WEIGHT_DEFAULT; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|