1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PODEntender\SitemapGenerator\Adapter\Jigsaw; |
4
|
|
|
|
5
|
|
|
use PODEntender\SitemapGenerator\Factory\SitemapXmlFactory; |
6
|
|
|
use PODEntender\SitemapGenerator\Factory\UrlSetFactory; |
7
|
|
|
|
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use TightenCo\Jigsaw\PageVariable; |
10
|
|
|
use DOMDocument; |
11
|
|
|
|
12
|
|
|
class JigsawAdapter |
13
|
|
|
{ |
14
|
|
|
private $sitemapXmlFactory; |
15
|
|
|
|
16
|
|
|
private $urlSetFactory; |
17
|
|
|
|
18
|
3 |
|
public function __construct(SitemapXmlFactory $sitemapXmlFactory, UrlSetFactory $urlSetFactory) |
19
|
|
|
{ |
20
|
3 |
|
$this->sitemapXmlFactory = $sitemapXmlFactory; |
21
|
3 |
|
$this->urlSetFactory = $urlSetFactory; |
22
|
3 |
|
} |
23
|
|
|
|
24
|
3 |
|
public function fromCollection(Collection $pages): DOMDocument |
25
|
|
|
{ |
26
|
|
|
$multiDimensionalArray = $pages |
27
|
|
|
->map(function (PageVariable $page) { |
28
|
3 |
|
$date = $this->fetchDataFromPage('lastModified', $page); |
29
|
|
|
|
30
|
3 |
|
if (is_string($date)) { |
31
|
|
|
$date = date_create_immutable(date('Y-m-d', $date)); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// Fallback to default post date metadata |
35
|
3 |
|
if ($date === null && $page->date) { |
36
|
2 |
|
$date = date_create_immutable(date('Y-m-d', $page->date)); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return [ |
40
|
3 |
|
'location' => $this->fetchDataFromPage('location', $page) ?? $page->getUrl(), |
41
|
3 |
|
'lastModified' => $date, |
42
|
3 |
|
'changeFrequency' => $this->fetchDataFromPage('changeFrequency', $page), |
43
|
3 |
|
'priority' => $this->fetchDataFromPage('priority', $page), |
44
|
|
|
]; |
45
|
3 |
|
}) |
46
|
3 |
|
->toArray(); |
47
|
|
|
|
48
|
3 |
|
return $this->sitemapXmlFactory->createFromUrlSet( |
49
|
3 |
|
$this->urlSetFactory->createFromMultiDimensionalArray($multiDimensionalArray) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
private function fetchDataFromPage(string $identifier, PageVariable $page) |
54
|
|
|
{ |
55
|
|
|
// Search for sitemap item in page property/config |
56
|
3 |
|
if ($page->sitemap === null || isset($page->sitemap[$identifier]) === false) { |
57
|
2 |
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
if (is_callable($page->sitemap[$identifier])) { |
61
|
1 |
|
return call_user_func($page->sitemap[$identifier], $page); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
return (string) $page->sitemap[$identifier]; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|