Skip to content

Commit

Permalink
Add: Repository Criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Dadyka committed Feb 8, 2023
1 parent a22f9eb commit 9d20743
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Mock Object
- Observer
- Proxy
- Repository Criteria
- Singleton
- State
- Strategy
Expand Down
108 changes: 108 additions & 0 deletions repositoryCriteria.php
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();

0 comments on commit 9d20743

Please sign in to comment.