Skip to content

Commit

Permalink
Merge pull request #112 from themrcatsu/main
Browse files Browse the repository at this point in the history
Add WhereIn attribute
  • Loading branch information
freekmurze authored Mar 9, 2023
2 parents b86b0e5 + 0f6dfca commit 6f19ad3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ For convenience, some commonly used regular expression patterns have helper attr
```php
#[WhereAlpha('alpha')]
#[WhereAlphaNumeric('alpha-numeric')]
#[WhereIn('in', ['value1', 'value2'])]
#[WhereNumber('number')]
#[WhereUlid('ulid')]
#[WhereUuid('uuid')]
Expand Down
15 changes: 15 additions & 0 deletions src/Attributes/WhereIn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\RouteAttributes\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class WhereIn extends Where
{
public function __construct(string $param, array $constraint)
{
$this->param = $param;
$this->constraint = implode('|', $constraint);
}
}
1 change: 1 addition & 0 deletions tests/AttributeTests/WhereAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function it_can_apply_shorthand_where()
'param' => '[0-9]+',
'alpha' => '[a-zA-Z]+',
'alpha-numeric' => '[a-zA-Z0-9]+',
'in' => 'value1|value2',
'number' => '[0-9]+',
'ulid' => '[0-7][0-9A-HJKMNP-TV-Z]{25}',
'uuid' => '[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}',
Expand Down
2 changes: 2 additions & 0 deletions tests/TestClasses/Controllers/WhereTestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Spatie\RouteAttributes\Attributes\Where;
use Spatie\RouteAttributes\Attributes\WhereAlpha;
use Spatie\RouteAttributes\Attributes\WhereAlphaNumeric;
use Spatie\RouteAttributes\Attributes\WhereIn;
use Spatie\RouteAttributes\Attributes\WhereNumber;
use Spatie\RouteAttributes\Attributes\WhereUlid;
use Spatie\RouteAttributes\Attributes\WhereUuid;
Expand Down Expand Up @@ -35,6 +36,7 @@ public function myWhereMethod()
#[Get('my-shorthands')]
#[WhereAlpha('alpha')]
#[WhereAlphaNumeric('alpha-numeric')]
#[WhereIn('in', ['value1', 'value2'])]
#[WhereNumber('number')]
#[WhereUlid('ulid')]
#[WhereUuid('uuid')]
Expand Down

0 comments on commit 6f19ad3

Please sign in to comment.