1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stitcher\Page; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Stitcher\Exception\InvalidConfiguration; |
7
|
|
|
use Stitcher\Page\Adapter\AdapterFactory; |
8
|
|
|
|
9
|
|
|
class PageParser |
10
|
|
|
{ |
11
|
|
|
/** @var PageFactory */ |
12
|
|
|
private $pageFactory; |
13
|
|
|
|
14
|
|
|
/** @var AdapterFactory */ |
15
|
|
|
private $adapterFactory; |
16
|
|
|
|
17
|
|
|
/** @var Page */ |
18
|
|
|
private $currentPage; |
19
|
|
|
|
20
|
|
|
public function __construct(PageFactory $pageFactory, AdapterFactory $adapterFactory) |
21
|
|
|
{ |
22
|
|
|
$this->pageFactory = $pageFactory; |
23
|
|
|
$this->adapterFactory = $adapterFactory; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static function make(PageFactory $factory, AdapterFactory $adapterFactory): PageParser |
27
|
|
|
{ |
28
|
|
|
return new self($factory, $adapterFactory); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function parse($pageConfiguration): PageCollection |
32
|
|
|
{ |
33
|
|
|
$result = new PageCollection(); |
34
|
|
|
|
35
|
|
|
$pageEntries = $this->parseAdapterConfiguration($pageConfiguration); |
36
|
|
|
|
37
|
|
|
foreach ($pageEntries as $pageEntry) { |
38
|
|
|
$page = $this->parsePage($pageEntry); |
39
|
|
|
|
40
|
|
|
$this->setCurrentPage($page); |
41
|
|
|
|
42
|
|
|
$result[$page->id()] = $page; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $result; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getCurrentPage(): ?Page |
49
|
|
|
{ |
50
|
|
|
return $this->currentPage; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function setCurrentPage(Page $page): PageParser |
54
|
|
|
{ |
55
|
|
|
$this->currentPage = $page; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function parseAdapterConfiguration(array $pageConfiguration): array |
61
|
|
|
{ |
62
|
|
|
$pageEntries = [$pageConfiguration]; |
63
|
|
|
|
64
|
|
|
$adapters = |
65
|
|
|
$pageConfiguration['config'] |
66
|
|
|
?? $pageConfiguration['adapters'] |
67
|
|
|
?? []; |
68
|
|
|
|
69
|
|
|
foreach ($adapters as $adapterType => $adapterConfiguration) { |
70
|
|
|
$adapter = $this->adapterFactory->create( |
71
|
|
|
$adapterType, |
72
|
|
|
$adapterConfiguration |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
if (! $adapter) { |
76
|
|
|
throw InvalidConfiguration::adapterNotFound($adapterType); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$pageEntries = $this->adaptPageEntries($pageEntries, $adapter); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $pageEntries; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function adaptPageEntries(array $pageEntries, Adapter $adapter): array |
86
|
|
|
{ |
87
|
|
|
$adaptedPageEntries = []; |
88
|
|
|
|
89
|
|
|
foreach ($pageEntries as $pageToTransform) { |
90
|
|
|
$adaptedPageEntries = array_merge( |
91
|
|
|
$adaptedPageEntries, |
92
|
|
|
$adapter->transform($pageToTransform) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$pageEntries = $adaptedPageEntries; |
97
|
|
|
|
98
|
|
|
return $pageEntries; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function parsePage($inputConfiguration): Page |
102
|
|
|
{ |
103
|
|
|
return $this->pageFactory->create($inputConfiguration); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|