-
Notifications
You must be signed in to change notification settings - Fork 0
/
repositoryCriteria.php
108 lines (87 loc) · 2.13 KB
/
repositoryCriteria.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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();