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