1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AbterPhp\Website\Template\Builder\PageCategory; |
4
|
|
|
|
5
|
|
|
use AbterPhp\Framework\Constant\Html5; |
6
|
|
|
use AbterPhp\Framework\Html\Component; |
7
|
|
|
use AbterPhp\Framework\Template\Data; |
8
|
|
|
use AbterPhp\Framework\Template\IBuilder; |
9
|
|
|
use AbterPhp\Framework\Template\IData; |
10
|
|
|
use AbterPhp\Website\Constant\Event; |
11
|
|
|
use AbterPhp\Website\Constant\Routes; |
12
|
|
|
use AbterPhp\Website\Domain\Entities\Page as Entity; |
13
|
|
|
use Opulence\Events\Dispatchers\IEventDispatcher; |
14
|
|
|
use Opulence\Routing\Urls\UrlGenerator; |
15
|
|
|
|
16
|
|
|
class Simple implements IBuilder |
17
|
|
|
{ |
18
|
|
|
const IDENTIFIER = 'simple'; |
19
|
|
|
|
20
|
|
|
const LIST_ITEM_TEMPLATE = '<li><a href="%s">%s</a></li>'; |
21
|
|
|
const LIST_TEMPLATE = '<ul>%s</ul>'; |
22
|
|
|
const LIST_CONTAINER_TEMPLATE = '<div class="page-categories"><h2>%s</h2>%s</div>'; |
23
|
|
|
|
24
|
|
|
/** @var IEventDispatcher */ |
25
|
|
|
protected $dispatcher; |
26
|
|
|
|
27
|
|
|
/** @var UrlGenerator */ |
28
|
|
|
protected $urlGenerator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* PageCategory constructor. |
32
|
|
|
* |
33
|
|
|
* @param IEventDispatcher $dispatcher |
34
|
|
|
* @param UrlGenerator $urlGenerator |
35
|
|
|
*/ |
36
|
|
|
public function __construct(IEventDispatcher $dispatcher, UrlGenerator $urlGenerator) |
37
|
|
|
{ |
38
|
|
|
$this->dispatcher = $dispatcher; |
39
|
|
|
$this->urlGenerator = $urlGenerator; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function getIdentifier(): string |
46
|
|
|
{ |
47
|
|
|
return static::IDENTIFIER; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Entity[] $pages |
52
|
|
|
* |
53
|
|
|
* @return Data |
54
|
|
|
*/ |
55
|
|
|
public function build(array $pages): IData |
56
|
|
|
{ |
57
|
|
|
$category = $pages[0]->getCategory(); |
58
|
|
|
|
59
|
|
|
$body = $this->getCategoryHtml($pages, $category->getName(), $category->getIdentifier()); |
60
|
|
|
|
61
|
|
|
$this->dispatcher->dispatch(Event::PAGE_CATEGORY_READY, $body); |
62
|
|
|
|
63
|
|
|
return new Data( |
64
|
|
|
$category->getIdentifier(), |
65
|
|
|
[], |
66
|
|
|
['body' => (string)$body] |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Entity[] $pages |
72
|
|
|
* @param string $categoryName |
73
|
|
|
* @param string $categoryIdentifier |
74
|
|
|
* |
75
|
|
|
* @return Component |
76
|
|
|
*/ |
77
|
|
|
protected function getCategoryHtml(array $pages, string $categoryName, string $categoryIdentifier): Component |
78
|
|
|
{ |
79
|
|
|
$container = new Component(null, [], [Html5::ATTR_CLASS => 'page-category'], Html5::TAG_DIV); |
80
|
|
|
|
81
|
|
|
if (count($pages) === 0) { |
82
|
|
|
return $container; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$list = new Component(null, [], [], Html5::TAG_UL); |
86
|
|
|
foreach ($pages as $page) { |
87
|
|
|
$url = $this->urlGenerator->createFromName(Routes::ROUTE_FALLBACK, $page->getIdentifier()); |
88
|
|
|
$a = new Component($page->getTitle(), [], [Html5::ATTR_HREF => $url], Html5::TAG_A); |
89
|
|
|
|
90
|
|
|
$list[] = new Component($a, [], [], Html5::TAG_LI); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$url = $this->urlGenerator->createFromName(Routes::ROUTE_FALLBACK, $categoryIdentifier); |
94
|
|
|
$a = new Component($categoryName, [], [Html5::ATTR_HREF => $url], Html5::TAG_A); |
95
|
|
|
|
96
|
|
|
$container[] = new Component($a, [], [], Html5::TAG_H2); |
97
|
|
|
$container[] = $list; |
98
|
|
|
|
99
|
|
|
return $container; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|