Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to `laravel-specification-pattern` will be documented in thi

- Ability to make specification verbose using composite specification.
- Exclusive or specification.
- `HasSpecifications` trait.

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

Expand Down
16 changes: 16 additions & 0 deletions src/HasSpecifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Maartenpaauw\Specifications;

trait HasSpecifications
{
/**
* @param Specification<self> $specification
*/
public function meets(Specification $specification): bool
{
return $specification->isSatisfiedBy($this);
}
}
27 changes: 27 additions & 0 deletions tests/HasSpecificationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Maartenpaauw\Specifications\Tests;

use Workbench\App\Candidate;
use Workbench\App\NegativeSpecification;
use Workbench\App\PositiveSpecification;

/**
* @internal
*
* @small
*/
final class HasSpecificationsTest extends TestCase
{
public function test_it_should_pass_the_candidate_to_the_specification(): void
{
// Arrange
$candidate = new Candidate();

// Act + Assert
$this->assertTrue($candidate->meets(new PositiveSpecification()));
$this->assertFalse($candidate->meets(new NegativeSpecification()));
}
}
12 changes: 12 additions & 0 deletions workbench/app/Candidate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Workbench\App;

use Maartenpaauw\Specifications\HasSpecifications;

final class Candidate
{
use HasSpecifications;
}