Skip to content

Commit 2362958

Browse files
authored
Merge pull request #19 from maartenpaauw/feat/has-specifications-trait
Add has specifications trait
2 parents c00b425 + 8c6b959 commit 2362958

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to `laravel-specification-pattern` will be documented in thi
88

99
- Ability to make specification verbose using composite specification.
1010
- Exclusive or specification.
11+
- `HasSpecifications` trait.
1112

1213
## [v2.2.0] - 2024-03-13
1314

src/HasSpecifications.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Maartenpaauw\Specifications;
6+
7+
trait HasSpecifications
8+
{
9+
/**
10+
* @param Specification<self> $specification
11+
*/
12+
public function meets(Specification $specification): bool
13+
{
14+
return $specification->isSatisfiedBy($this);
15+
}
16+
}

tests/HasSpecificationsTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Maartenpaauw\Specifications\Tests;
6+
7+
use Workbench\App\Candidate;
8+
use Workbench\App\NegativeSpecification;
9+
use Workbench\App\PositiveSpecification;
10+
11+
/**
12+
* @internal
13+
*
14+
* @small
15+
*/
16+
final class HasSpecificationsTest extends TestCase
17+
{
18+
public function test_it_should_pass_the_candidate_to_the_specification(): void
19+
{
20+
// Arrange
21+
$candidate = new Candidate();
22+
23+
// Act + Assert
24+
$this->assertTrue($candidate->meets(new PositiveSpecification()));
25+
$this->assertFalse($candidate->meets(new NegativeSpecification()));
26+
}
27+
}

workbench/app/Candidate.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Workbench\App;
6+
7+
use Maartenpaauw\Specifications\HasSpecifications;
8+
9+
final class Candidate
10+
{
11+
use HasSpecifications;
12+
}

0 commit comments

Comments
 (0)