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