1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stitcher\Page\Adapter; |
4
|
|
|
|
5
|
|
|
use Stitcher\Adapter; |
6
|
|
|
use Stitcher\DynamicFactory; |
7
|
|
|
use Stitcher\Variable\VariableParser; |
8
|
|
|
|
9
|
|
|
class AdapterFactory extends DynamicFactory |
10
|
|
|
{ |
11
|
|
|
private $variableParser; |
12
|
|
|
|
13
|
19 |
|
public function __construct(VariableParser $variableParser) |
14
|
|
|
{ |
15
|
19 |
|
$this->setCollectionRule(); |
16
|
19 |
|
$this->setFilterRule(); |
17
|
19 |
|
$this->setPaginationRule(); |
18
|
|
|
|
19
|
19 |
|
$this->variableParser = $variableParser; |
20
|
19 |
|
} |
21
|
|
|
|
22
|
18 |
|
public static function make(VariableParser $variableParser): AdapterFactory |
23
|
|
|
{ |
24
|
18 |
|
return new self($variableParser); |
25
|
|
|
} |
26
|
|
|
|
27
|
10 |
|
public function create($adapterType, $adapterConfiguration): ?Adapter |
28
|
|
|
{ |
29
|
10 |
|
foreach ($this->getRules() as $rule) { |
30
|
10 |
|
$adapter = $rule($adapterType, $adapterConfiguration); |
31
|
|
|
|
32
|
10 |
|
if ($adapter) { |
33
|
10 |
|
return $adapter; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function setCollectionRule(): void |
41
|
|
|
{ |
42
|
19 |
|
$this->setRule(CollectionAdapter::class, function (string $adapterType, array $adapterConfiguration) { |
43
|
10 |
|
if ($adapterType === 'collection') { |
44
|
8 |
|
return CollectionAdapter::make($adapterConfiguration, $this->variableParser); |
45
|
|
|
} |
46
|
|
|
|
47
|
7 |
|
return null; |
48
|
19 |
|
}); |
49
|
19 |
|
} |
50
|
|
|
|
51
|
|
|
private function setFilterRule(): void |
52
|
|
|
{ |
53
|
19 |
|
$this->setRule(FilterAdapter::class, function (string $adapterType, array $adapterConfiguration) { |
54
|
7 |
|
if ($adapterType === 'filter') { |
55
|
1 |
|
return FilterAdapter::make($adapterConfiguration, $this->variableParser); |
56
|
|
|
} |
57
|
|
|
|
58
|
7 |
|
return null; |
59
|
19 |
|
}); |
60
|
19 |
|
} |
61
|
|
|
|
62
|
|
|
private function setPaginationRule(): void |
63
|
|
|
{ |
64
|
19 |
|
$this->setRule(PaginationAdapter::class, function (string $adapterType, array $adapterConfiguration) { |
65
|
7 |
|
if ($adapterType === 'pagination') { |
66
|
7 |
|
return PaginationAdapter::make($adapterConfiguration, $this->variableParser); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return null; |
70
|
19 |
|
}); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|