Skip to content

Commit c5d63c0

Browse files
committed
docs: add basic documentation
1 parent cc24395 commit c5d63c0

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

README.md

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,76 @@ return [
3131

3232
## Usage
3333

34+
Here's how you can create a specification:
35+
36+
```shell
37+
php artisan make:specification AdultSpecification
38+
```
39+
40+
This will generate a specification class within the `App\Specifications` namespace.
41+
42+
```php
43+
<?php
44+
45+
namespace App\Specifications;
46+
47+
use Maartenpaauw\Specifications\Specification;
48+
49+
/**
50+
* @implements Specification<mixed>
51+
*/
52+
class AdultSpecification implements Specification
53+
{
54+
public function isSatisfiedBy(mixed $candidate): bool
55+
{
56+
return $candidate->age >= 18;
57+
}
58+
}
59+
```
60+
61+
After implementing the business logic, you can use it in various ways.
62+
63+
imagine we have the following class which represents a person with a given age.
64+
65+
```php
66+
class Person {
67+
public function __construct(public int $age)
68+
{}
69+
}
70+
```
71+
72+
Now we can use the specification by **directly** calling the `isSatisfiedBy` method
73+
or **indirectly** be filtering an eloquent collection by calling the `matching` method.
74+
75+
### Direct
76+
77+
```php
78+
$specification = new AdultSpecification();
79+
80+
// ...
81+
82+
$specification->isSatisfiedBy(new Person(16)); // false
83+
$specification->isSatisfiedBy(new Person(32)); // true
84+
```
85+
86+
### Indirect
87+
3488
```php
35-
$laravel-specification-pattern = new Maartenpaauw\Specifications();
36-
echo $laravel-specification-pattern->echoPhrase('Hello, Maartenpaauw!');
89+
$persons = collect([
90+
new Person(10),
91+
new Person(17),
92+
new Person(18),
93+
new Person(32),
94+
]);
95+
96+
// ...
97+
98+
// Returns a collection with persons matching the specification
99+
$persons = $persons->matching(new AdultSpecification());
100+
101+
// ...
102+
103+
$persons->count(); // 2
37104
```
38105

39106
## Testing

0 commit comments

Comments
 (0)