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
|
1 |
|
public function __construct(SitemapXmlFactory $sitemapXmlFactory, UrlSetFactory $urlSetFactory) |
19
|
|
|
{ |
20
|
1 |
|
$this->sitemapXmlFactory = $sitemapXmlFactory; |
21
|
1 |
|
$this->urlSetFactory = $urlSetFactory; |
22
|
1 |
|
} |
23
|
|
|
|
24
|
1 |
|
public function fromCollection(Collection $pages): DOMDocument |
25
|
|
|
{ |
26
|
|
|
$multiDimensionalArray = $pages |
27
|
|
|
->map(function (PageVariable $page) { |
28
|
1 |
|
$sitemapUrl = $page->getUrl(); |
29
|
|
|
|
30
|
1 |
|
$parsedUrl = parse_url($sitemapUrl); |
31
|
|
|
|
32
|
|
|
// If index page, don't append trailing forward slash |
33
|
1 |
|
if (false === isset($parsedUrl['path'])) { |
34
|
|
|
$sitemapUrl = rtrim($sitemapUrl, '/'); |
35
|
|
|
} else { |
36
|
1 |
|
$pathInfo = pathinfo($parsedUrl['path']); |
37
|
|
|
|
38
|
|
|
// If .html, send it right away. If path-like, make sure last character is a forward slash |
39
|
1 |
|
$sitemapUrl = isset($pathInfo['extension']) ? $sitemapUrl : rtrim($sitemapUrl, '/') . '/'; |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
$output = ['location' => $sitemapUrl]; |
43
|
|
|
|
44
|
1 |
|
if ($page->date) { |
45
|
1 |
|
$output['lastModified'] = date_create_immutable(date('Y-m-d', $page->date)); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
if ($page->get('sitemap') && isset($page->sitemap['changeFrequency'])) { |
49
|
1 |
|
$output['changeFrequency'] = $page->sitemap['changeFrequency']; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
if ($page->get('sitemap') && isset($page->sitemap['priority'])) { |
53
|
1 |
|
$output['priority'] = $page->sitemap['priority']; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return $output; |
57
|
1 |
|
}) |
58
|
1 |
|
->toArray(); |
59
|
|
|
|
60
|
1 |
|
return $this->sitemapXmlFactory->createFromUrlSet( |
61
|
1 |
|
$this->urlSetFactory->createFromMultiDimensionalArray($multiDimensionalArray) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|