1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cecil\Generator; |
10
|
|
|
|
11
|
|
|
use Cecil\Collection\Page\Collection as PagesCollection; |
12
|
|
|
use Cecil\Collection\Page\Page; |
13
|
|
|
use Cecil\Collection\Page\Type; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Section. |
17
|
|
|
*/ |
18
|
|
|
class Section extends AbstractGenerator implements GeneratorInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function generate(): void |
24
|
|
|
{ |
25
|
|
|
$sections = []; |
26
|
|
|
|
27
|
|
|
// identify sections |
28
|
|
|
/* @var $page Page */ |
29
|
|
|
foreach ($this->pagesCollection as $page) { |
30
|
|
|
if ($page->getSection()) { |
31
|
|
|
$sections[$page->getSection()][] = $page; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// adds section to pages collection |
36
|
|
|
if (count($sections) > 0) { |
37
|
|
|
$menuWeight = 100; |
38
|
|
|
foreach ($sections as $section => $pagesAsArray) { |
39
|
|
|
$pageId = $path = Page::slugify($section); |
40
|
|
|
$page = (new Page($pageId))->setVariable('title', ucfirst($section)); |
41
|
|
|
if ($this->pagesCollection->has($pageId)) { |
42
|
|
|
$page = clone $this->pagesCollection->get($pageId); |
43
|
|
|
} |
44
|
|
|
$pages = new PagesCollection($section, $pagesAsArray); |
45
|
|
|
// sort |
46
|
|
|
$pages = $pages->sortByDate(); |
47
|
|
View Code Duplication |
if ($page->getVariable('sortby')) { |
|
|
|
|
48
|
|
|
$sortMethod = sprintf('sortBy%s', ucfirst($page->getVariable('sortby'))); |
49
|
|
|
if (method_exists($pages, $sortMethod)) { |
50
|
|
|
$pages = $pages->$sortMethod(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
// add navigation links |
54
|
|
|
$this->addNavigationLinks($pages); |
55
|
|
|
// create page for each section |
56
|
|
|
$page->setPath($path) |
57
|
|
|
->setType(Type::SECTION) |
58
|
|
|
->setVariable('pages', $pages) |
59
|
|
|
->setVariable('date', $pages->first()->getVariable('date')) |
60
|
|
|
->setVariable('menu', [ |
61
|
|
|
'main' => ['weight' => $menuWeight], |
62
|
|
|
]); |
63
|
|
|
$this->generatedPages->add($page); |
64
|
|
|
$menuWeight += 10; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Add pagination (next and prev) to section sub pages. |
71
|
|
|
* |
72
|
|
|
* @param PagesCollection $pages |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
protected function addNavigationLinks(PagesCollection $pages): void |
76
|
|
|
{ |
77
|
|
|
$pagesAsArray = $pages->toArray(); |
78
|
|
|
if (count($pagesAsArray) > 1) { |
79
|
|
|
foreach ($pagesAsArray as $position => $page) { |
80
|
|
|
switch ($position) { |
81
|
|
|
// first |
82
|
|
|
case 0: |
83
|
|
|
$page->setVariables([ |
84
|
|
|
'next' => [ |
85
|
|
|
'id' => $pagesAsArray[$position+1]->getId(), |
86
|
|
|
'path' => $pagesAsArray[$position+1]->getPath(), |
87
|
|
|
'title' => $pagesAsArray[$position+1]->getVariable('title'), |
88
|
|
|
], |
89
|
|
|
]); |
90
|
|
|
break; |
91
|
|
|
// last |
92
|
|
|
case (count($pagesAsArray)-1): |
93
|
|
|
$page->setVariables([ |
94
|
|
|
'prev' => [ |
95
|
|
|
'id' => $pagesAsArray[$position-1]->getId(), |
96
|
|
|
'path' => $pagesAsArray[$position-1]->getPath(), |
97
|
|
|
'title' => $pagesAsArray[$position-1]->getVariable('title'), |
98
|
|
|
], |
99
|
|
|
]); |
100
|
|
|
break; |
101
|
|
|
default: |
102
|
|
|
$page->setVariables([ |
103
|
|
|
'prev' => [ |
104
|
|
|
'id' => $pagesAsArray[$position-1]->getId(), |
105
|
|
|
'path' => $pagesAsArray[$position-1]->getPath(), |
106
|
|
|
'title' => $pagesAsArray[$position-1]->getVariable('title'), |
107
|
|
|
], |
108
|
|
|
'next' => [ |
109
|
|
|
'id' => $pagesAsArray[$position+1]->getId(), |
110
|
|
|
'path' => $pagesAsArray[$position+1]->getPath(), |
111
|
|
|
'title' => $pagesAsArray[$position+1]->getVariable('title'), |
112
|
|
|
], |
113
|
|
|
]); |
114
|
|
|
break; |
115
|
|
|
} |
116
|
|
|
$this->generatedPages->add($page); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.