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