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