From 8c6b9591572977c73e0b1df4709b9f31834183cc Mon Sep 17 00:00:00 2001 From: Maarten Paauw Date: Sat, 6 Apr 2024 21:20:45 +0200 Subject: [PATCH] feat: add has specifications trait --- CHANGELOG.md | 1 + src/HasSpecifications.php | 16 ++++++++++++++++ tests/HasSpecificationsTest.php | 27 +++++++++++++++++++++++++++ workbench/app/Candidate.php | 12 ++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 src/HasSpecifications.php create mode 100644 tests/HasSpecificationsTest.php create mode 100644 workbench/app/Candidate.php diff --git a/CHANGELOG.md b/CHANGELOG.md index e55448f..6b7b81c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/HasSpecifications.php b/src/HasSpecifications.php new file mode 100644 index 0000000..82dfee3 --- /dev/null +++ b/src/HasSpecifications.php @@ -0,0 +1,16 @@ + $specification + */ + public function meets(Specification $specification): bool + { + return $specification->isSatisfiedBy($this); + } +} diff --git a/tests/HasSpecificationsTest.php b/tests/HasSpecificationsTest.php new file mode 100644 index 0000000..5b266fb --- /dev/null +++ b/tests/HasSpecificationsTest.php @@ -0,0 +1,27 @@ +assertTrue($candidate->meets(new PositiveSpecification())); + $this->assertFalse($candidate->meets(new NegativeSpecification())); + } +} diff --git a/workbench/app/Candidate.php b/workbench/app/Candidate.php new file mode 100644 index 0000000..2fb1209 --- /dev/null +++ b/workbench/app/Candidate.php @@ -0,0 +1,12 @@ +