XmlTemplate::replace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
/**
3
 * This file is part of sitemap-common.
4
 *
5
 * (c) 2016 Daniele Moraschi
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace SiteMap\Template;
12
13
14
use SiteMap\Schema\SiteMapUrl;
15
use SiteMap\Schema\SiteMapUrlCollection;
16
17
class XmlTemplate implements Template
18
{
19
20
    const TEMPLATE = <<<HTML
21
<?xml version="1.0" encoding="UTF-8"?>
22
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
23
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
24
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
25
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">\n{{entry}}</urlset>
26
HTML;
27
28
    const ENTRY = <<<HTML
29
    <url>
30
        <loc>{{link}}</loc>
31
        <changefreq>{{frequency}}</changefreq>
32
        <priority>{{priority}}</priority>
33
    </url>\n
34
HTML;
35
36
    /**
37
     * @param SiteMapUrlCollection $collection
38
     * @return mixed|string
39
     */
40
    public function map(SiteMapUrlCollection $collection) {
41
        $output = '';
42
        /** @var SiteMapUrl $item */
43
        foreach ($collection as $item) {
44
            $output .= $this->replace(
45
                array('{{link}}', '{{frequency}}', '{{priority}}'),
46
                array($item->getUrl()->getWebUrl(), $item->getFrequency(), $item->getPriority()),
47
                self::ENTRY
48
            );
49
        }
50
51
        return $this->replace(array('{{entry}}'), array($output), self::TEMPLATE);
52
    }
53
54
    /**
55
     * @param $key
56
     * @param $value
57
     * @param $template
58
     * @return mixed
59
     */
60
    private function replace($key, $value, $template) {
61
        return str_replace($key, $value, $template);
62
    }
63
}