Skip to content

Commit be60822

Browse files
authored
ADD allow any value to be saved against a mark (#47)
1 parent 4f37334 commit be60822

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,28 @@ Reaction::has($post, $user, 'heart'); // returns whether the user has reacted wi
245245
Reaction::count($post, 'person_raising_hand'); // returns the amount of 'person_raising_hand' reactions for the given post
246246
```
247247

248+
You can also use wildcards to allow any value for a specific mark.
249+
250+
Here's an example when working with reactions:
251+
252+
``` php
253+
'allowed_values' => [
254+
'reaction' => '*',
255+
],
256+
```
257+
258+
When set, you can use any value when giving a reaction:
259+
260+
``` php
261+
use App\Models\Post;
262+
use Maize\Markable\Models\Reaction;
263+
264+
$post = Post::firstOrFail();
265+
$user = auth()->user();
266+
267+
Reaction::add($post, $user, 'random_value'); // adds the 'random_value' reaction to the post for the given user
268+
```
269+
248270
### Retrieve the list of marks of an entity with eloquent
249271

250272
``` php

src/Mark.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class_basename(static::class)
2424
)->plural()->lower()->__toString();
2525
}
2626

27-
public static function allowedValues(): ?array
27+
public static function allowedValues(): array|string|null
2828
{
2929
$className = Str::lower(static::getMarkClassName());
3030

@@ -100,7 +100,13 @@ public static function toggle(Model $markable, Model $user, string $value = null
100100

101101
public static function hasAllowedValues(?string $value): bool
102102
{
103-
return in_array($value, static::allowedValues() ?? [null]);
103+
$allowedValues = static::allowedValues() ?? [null];
104+
105+
if ($allowedValues === '*') {
106+
return true;
107+
}
108+
109+
return in_array($value, $allowedValues);
104110
}
105111

106112
public function user(): BelongsTo

tests/ReactionTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ public function cannot_add_an_invalid_reaction_value_fails()
2929
Reaction::add($article, $user, 'not_valid_value');
3030
}
3131

32+
/** @test */
33+
public function can_add_any_value_with_wildcard()
34+
{
35+
config()->set('markable.allowed_values.reaction', '*');
36+
37+
$article = Article::factory()->create();
38+
$user = User::factory()->create();
39+
$table = (new Reaction)->getTable();
40+
41+
Reaction::add($article, $user, 'random_value');
42+
$this->assertDatabaseCount($table, 1);
43+
$this->assertDatabaseHas($table, [
44+
'user_id' => $user->getKey(),
45+
'markable_id' => $article->getKey(),
46+
'markable_type' => $article->getMorphClass(),
47+
'value' => 'random_value',
48+
]);
49+
}
50+
3251
/** @test */
3352
public function can_add_a_reaction()
3453
{

0 commit comments

Comments
 (0)