|
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\ReactionType\Models; |
|
15
|
|
|
|
|
16
|
|
|
use Cog\Contracts\Love\ReactionType\Exceptions\ReactionTypeInvalid; |
|
17
|
|
|
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeInterface; |
|
18
|
|
|
use Cog\Laravel\Love\Reaction\Models\Reaction; |
|
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\HasMany; |
|
23
|
|
|
|
|
24
|
|
|
final class ReactionType extends Model implements |
|
25
|
|
|
ReactionTypeInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use HasFactory; |
|
28
|
|
|
|
|
29
|
|
|
public const MASS_DEFAULT = 0; |
|
30
|
|
|
|
|
31
|
|
|
protected $table = 'love_reaction_types'; |
|
32
|
|
|
|
|
33
|
|
|
protected static $unguarded = true; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array<string, int> |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $attributes = [ |
|
39
|
|
|
'mass' => self::MASS_DEFAULT, |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
public function id(): Attribute |
|
43
|
|
|
{ |
|
44
|
|
|
return new Attribute( |
|
45
|
|
|
get: fn (string | null $value) => $value, |
|
46
|
|
|
set: fn (string | null $value) => $value, |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function mass(): Attribute |
|
51
|
|
|
{ |
|
52
|
|
|
return new Attribute( |
|
53
|
|
|
get: fn (int | null $value) => $value ?? self::MASS_DEFAULT, |
|
54
|
|
|
set: fn (int | null $value) => $value ?? self::MASS_DEFAULT, |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var array<self> |
|
60
|
|
|
*/ |
|
61
|
|
|
private static array $nameCache = []; |
|
62
|
|
|
|
|
63
|
|
|
protected static function boot(): void |
|
64
|
|
|
{ |
|
65
|
|
|
parent::boot(); |
|
66
|
|
|
|
|
67
|
|
|
self::saved(function (self $reactionType) { |
|
68
|
|
|
self::$nameCache[$reactionType->getName()] = $reactionType; |
|
69
|
|
|
}); |
|
70
|
|
|
|
|
71
|
|
|
self::deleted(function (self $reactionType) { |
|
72
|
|
|
unset(self::$nameCache[$reactionType->getName()]); |
|
73
|
|
|
}); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function reactions(): HasMany |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->hasMany(Reaction::class, 'reaction_type_id'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public static function fromName( |
|
82
|
|
|
string $name, |
|
83
|
|
|
): ReactionTypeInterface { |
|
84
|
|
|
if (isset(self::$nameCache[$name])) { |
|
85
|
|
|
return self::$nameCache[$name]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** @var \Cog\Laravel\Love\ReactionType\Models\ReactionType|null $type */ |
|
89
|
|
|
$type = self::query()->where('name', $name)->first(); |
|
90
|
|
|
|
|
91
|
|
|
if ($type === null) { |
|
92
|
|
|
throw ReactionTypeInvalid::nameNotExists($name); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
self::$nameCache[$name] = $type; |
|
96
|
|
|
|
|
97
|
|
|
return $type; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function getId(): string |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->getAttributeValue('id'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getName(): string |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->getAttributeValue('name'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function getMass(): int |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->getAttributeValue('mass'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function isEqualTo( |
|
116
|
|
|
ReactionTypeInterface $that, |
|
117
|
|
|
): bool { |
|
118
|
|
|
return $this->getId() === $that->getId(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function isNotEqualTo( |
|
122
|
|
|
ReactionTypeInterface $that, |
|
123
|
|
|
): bool { |
|
124
|
|
|
return !$this->isEqualTo($that); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|