Skip to content

Commit ed47b07

Browse files
committed
feat: add laravel access response transformer
1 parent e6af9ca commit ed47b07

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to `laravel-specification-pattern` will be documented in this file.
44

5+
## [v2.8.0] - 2024-11-21
6+
7+
### Added
8+
9+
- Transformer to transform specification into Laravel authorization access responses.
10+
511
## [v2.7.0] - 2024-11-20
612

713
### Added
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Maartenpaauw\Specifications\Laravel;
6+
7+
use Illuminate\Auth\Access\Response;
8+
use Maartenpaauw\Specifications\DissatisfiedSpecification;
9+
use Maartenpaauw\Specifications\Specification;
10+
11+
/**
12+
* @template TCandidate
13+
*/
14+
final class AccessResponseTransformer
15+
{
16+
/**
17+
* @param Specification<TCandidate> $specification
18+
* @param TCandidate $candidate
19+
*/
20+
public function transform(Specification $specification, mixed $candidate): Response
21+
{
22+
try {
23+
$satisfied = $specification->isSatisfiedBy($candidate);
24+
} catch (DissatisfiedSpecification $e) {
25+
return Response::deny($e->getMessage(), $e->getCode());
26+
}
27+
28+
return $satisfied ? Response::allow() : Response::deny();
29+
}
30+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Maartenpaauw\Specifications\Tests\Laravel;
6+
7+
use Maartenpaauw\Specifications\Laravel\AccessResponseTransformer;
8+
use Maartenpaauw\Specifications\Tests\TestCase;
9+
use Maartenpaauw\Specifications\VerboseSpecification;
10+
use Workbench\App\NegativeSpecification;
11+
use Workbench\App\PositiveSpecification;
12+
13+
/**
14+
* @internal
15+
*
16+
* @small
17+
*/
18+
final class AccessResponseTransformerTest extends TestCase
19+
{
20+
public function test_transform(): void
21+
{
22+
// Arrange
23+
$accessResponseTransformer = new AccessResponseTransformer();
24+
25+
// Act
26+
$allowed = $accessResponseTransformer->transform(new PositiveSpecification(), null);
27+
$denied = $accessResponseTransformer->transform(new NegativeSpecification(), null);
28+
$verbose = $accessResponseTransformer->transform(
29+
new VerboseSpecification(new NegativeSpecification(), 'This is a message.', 10),
30+
null,
31+
);
32+
33+
// Assert
34+
$this->assertTrue($allowed->allowed());
35+
$this->assertNull($allowed->message());
36+
$this->assertNull($allowed->code());
37+
38+
$this->assertTrue($denied->denied());
39+
$this->assertNull($denied->message());
40+
$this->assertNull($denied->code());
41+
42+
$this->assertTrue($verbose->denied());
43+
$this->assertIsString($verbose->message());
44+
$this->assertNotEmpty($verbose->message());
45+
$this->assertSame('This is a message.', $verbose->message());
46+
$this->assertIsInt($verbose->code());
47+
$this->assertSame(10, $verbose->code());
48+
}
49+
}

0 commit comments

Comments
 (0)