|
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 ReactionTypeContract; |
|
18
|
|
|
use Cog\Laravel\Love\Reaction\Models\Reaction; |
|
19
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
20
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
|
21
|
|
|
|
|
22
|
|
|
final class ReactionType extends Model implements |
|
23
|
|
|
ReactionTypeContract |
|
24
|
|
|
{ |
|
25
|
|
|
protected $table = 'love_reaction_types'; |
|
26
|
|
|
|
|
27
|
|
|
protected $fillable = [ |
|
28
|
|
|
'name', |
|
29
|
|
|
'weight', |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
protected $casts = [ |
|
33
|
|
|
'id' => 'string', |
|
34
|
|
|
'weight' => 'integer', |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
private static $nameCache = []; |
|
38
|
|
|
|
|
39
|
|
|
protected static function boot(): void |
|
40
|
|
|
{ |
|
41
|
|
|
parent::boot(); |
|
42
|
|
|
|
|
43
|
|
|
static::saved(function (ReactionTypeContract $reactionType) { |
|
44
|
|
|
static::$nameCache[$reactionType->getName()] = $reactionType; |
|
|
|
|
|
|
45
|
|
|
}); |
|
46
|
|
|
|
|
47
|
|
|
static::deleted(function (ReactionTypeContract $reactionType) { |
|
48
|
|
|
unset(static::$nameCache[$reactionType->getName()]); |
|
|
|
|
|
|
49
|
|
|
}); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function reactions(): HasMany |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->hasMany(Reaction::class, 'reaction_type_id'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public static function fromName( |
|
58
|
|
|
string $name |
|
59
|
|
|
): ReactionTypeContract { |
|
60
|
|
|
if (isset(static::$nameCache[$name])) { |
|
|
|
|
|
|
61
|
|
|
return static::$nameCache[$name]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @var \Cog\Laravel\Love\ReactionType\Models\ReactionType $type */ |
|
65
|
|
|
$type = static::query()->where('name', $name)->first(); |
|
66
|
|
|
|
|
67
|
|
|
if (is_null($type)) { |
|
68
|
|
|
throw ReactionTypeInvalid::nameNotExists($name); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
static::$nameCache[$name] = $type; |
|
72
|
|
|
|
|
73
|
|
|
return $type; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getId(): string |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->getAttributeValue('id'); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getName(): string |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->getAttributeValue('name'); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getWeight(): int |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->getAttributeValue('weight') ?? 0; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function isEqualTo( |
|
92
|
|
|
ReactionTypeContract $that |
|
93
|
|
|
): bool { |
|
94
|
|
|
return $this->getId() === $that->getId(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function isNotEqualTo( |
|
98
|
|
|
ReactionTypeContract $that |
|
99
|
|
|
): bool { |
|
100
|
|
|
return !$this->isEqualTo($that); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|