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 ReactantContract; |
17
|
|
|
use Cog\Contracts\Love\Reactant\ReactionCounter\Models\ReactionCounter as ReactionCounterContract; |
18
|
|
|
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeContract; |
19
|
|
|
use Cog\Laravel\Love\Reactant\Models\Reactant; |
20
|
|
|
use Cog\Laravel\Love\ReactionType\Models\ReactionType; |
21
|
|
|
use Illuminate\Database\Eloquent\Model; |
22
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
23
|
|
|
|
24
|
|
|
final class ReactionCounter extends Model implements |
25
|
|
|
ReactionCounterContract |
26
|
|
|
{ |
27
|
|
|
protected $table = 'love_reactant_reaction_counters'; |
28
|
|
|
|
29
|
|
|
protected $fillable = [ |
30
|
|
|
'reaction_type_id', |
31
|
|
|
'count', |
32
|
|
|
'weight', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
protected $casts = [ |
36
|
|
|
'count' => 'integer', |
37
|
|
|
'weight' => 'integer', |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
public function reactant(): BelongsTo |
41
|
|
|
{ |
42
|
|
|
return $this->belongsTo(Reactant::class, 'reactant_id'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function reactionType(): BelongsTo |
46
|
|
|
{ |
47
|
|
|
return $this->belongsTo(ReactionType::class, 'reaction_type_id'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getReactant(): ReactantContract |
51
|
|
|
{ |
52
|
|
|
return $this->getAttribute('reactant'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getReactionType(): ReactionTypeContract |
56
|
|
|
{ |
57
|
|
|
return $this->getAttribute('reactionType'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function isReactionOfType( |
61
|
|
|
ReactionTypeContract $reactionType |
62
|
|
|
): bool { |
63
|
|
|
return $this->getReactionType()->isEqualTo($reactionType); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function isNotReactionOfType( |
67
|
|
|
ReactionTypeContract $reactionType |
68
|
|
|
): bool { |
69
|
|
|
return !$this->isReactionOfType($reactionType); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getCount(): int |
73
|
|
|
{ |
74
|
|
|
return $this->getAttributeValue('count') ?? 0; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getWeight(): int |
78
|
|
|
{ |
79
|
|
|
return $this->getAttributeValue('weight') ?? 0; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function incrementCount( |
83
|
|
|
int $amount |
84
|
|
|
): void { |
85
|
|
|
$this->increment('count', $amount); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function decrementCount( |
89
|
|
|
int $amount |
90
|
|
|
): void { |
91
|
|
|
$this->decrement('count', $amount); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function incrementWeight( |
95
|
|
|
int $amount |
96
|
|
|
): void { |
97
|
|
|
$this->increment('weight', $amount); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function decrementWeight( |
101
|
|
|
int $amount |
102
|
|
|
): void { |
103
|
|
|
$this->decrement('weight', $amount); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|