Skip to content

Commit 814f1bf

Browse files
committed
Specification Pattern example
1 parent 7ab29cc commit 814f1bf

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Chain of Responsibility
1010
* Singleton
1111
* Factory
12+
* Specification
1213

1314
## Installation
1415
* Type `composer install` the first time to get dependencies

Diff for: index_specification.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
require_once 'src/bootstrap.php';
4+
5+
use GeoBas\Specification\{Customer, CustomerIsGold};
6+
7+
$specification = new CustomerIsGold;
8+
9+
$goldCustomer = new Customer('gold');
10+
$silverCustomer = new Customer('silver');
11+
12+
echo $specification->isSatisfiedBy($goldCustomer) ? 'true' : 'false';
13+
echo '<p>';
14+
echo $specification->isSatisfiedBy($silverCustomer) ? 'true' : 'false';

Diff for: src/Specification/Customer.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace GeoBas\Specification;
4+
5+
class Customer
6+
{
7+
private $type;
8+
9+
public function __construct($type)
10+
{
11+
$this->type = $type;
12+
}
13+
14+
public function getType()
15+
{
16+
return $this->type;
17+
}
18+
}

Diff for: src/Specification/CustomerIsGold.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace GeoBas\Specification;
4+
5+
class CustomerIsGold
6+
{
7+
public function isSatisfiedBy(Customer $customer)
8+
{
9+
return $customer->getType() == 'gold';
10+
}
11+
}

0 commit comments

Comments
 (0)