-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexander Dadyka
committed
Feb 8, 2023
1 parent
a22f9eb
commit 9d20743
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
- Mock Object | ||
- Observer | ||
- Proxy | ||
- Repository Criteria | ||
- Singleton | ||
- State | ||
- Strategy | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
class Builder | ||
{ | ||
private string $text = ''; | ||
|
||
public function buildStepA(string $param): void | ||
{ | ||
$this->setText(__METHOD__, $param); | ||
} | ||
|
||
public function buildStepB(string $param): void | ||
{ | ||
$this->setText(__METHOD__, $param); | ||
} | ||
|
||
public function getText(): string | ||
{ | ||
return $this->text; | ||
} | ||
|
||
private function setText(string $methodName, string $param): void | ||
{ | ||
$this->text .= $methodName . ' with ' . $param . '<br>'; | ||
} | ||
} | ||
|
||
interface CriterionInterface | ||
{ | ||
public function apply(Builder $builder): void; | ||
} | ||
|
||
class FirstCriterion implements CriterionInterface | ||
{ | ||
public function __construct( | ||
private string $param | ||
) { | ||
} | ||
|
||
public function apply(Builder $builder): void | ||
{ | ||
$builder->buildStepA($this->param); | ||
} | ||
} | ||
|
||
class SecondCriterion implements CriterionInterface | ||
{ | ||
public function __construct( | ||
private string $param | ||
) { | ||
} | ||
|
||
public function apply(Builder $builder): void | ||
{ | ||
$builder->buildStepB($this->param); | ||
} | ||
} | ||
|
||
class CriteriaApplier | ||
{ | ||
private array $criteria = []; | ||
|
||
public function __construct( | ||
private Builder $builder | ||
) { | ||
} | ||
|
||
public function addCriterion(CriterionInterface $criterion): void | ||
{ | ||
$this->criteria[] = $criterion; | ||
} | ||
|
||
public function applyCriteriaAndGetText(): string | ||
{ | ||
foreach ($this->criteria as $criterion) { | ||
$criterion->apply($this->builder); | ||
} | ||
|
||
return $this->builder->getText(); | ||
} | ||
} | ||
|
||
class Repository | ||
{ | ||
public function __construct( | ||
private CriteriaApplier $criteriaApplier | ||
) { | ||
} | ||
|
||
public function make(): string | ||
{ | ||
$this->criteriaApplier->addCriterion(new FirstCriterion('paramA')); | ||
$this->criteriaApplier->addCriterion(new SecondCriterion('paramB')); | ||
$this->criteriaApplier->addCriterion(new FirstCriterion('paramC')); | ||
|
||
return $this->criteriaApplier->applyCriteriaAndGetText(); | ||
} | ||
} | ||
|
||
/** | ||
* Client | ||
*/ | ||
|
||
$builder = new Builder(); | ||
$criteriaApplier = new CriteriaApplier($builder); | ||
$repository = new Repository($criteriaApplier); | ||
|
||
echo $repository->make(); |