/
GoogleMultilangUrlDecorator.php
78 lines (67 loc) · 2.06 KB
/
GoogleMultilangUrlDecorator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* This file is part of the PrestaSitemapBundle package.
*
* (c) PrestaConcept <www.prestaconcept.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Presta\SitemapBundle\Sitemap\Url;
use Presta\SitemapBundle\Sitemap\Utils;
/**
* Decorate w/ google alternate language url guidelines
* @see http://support.google.com/webmasters/bin/answer.py?hl=en&answer=2620865
*
* @author David Epely <depely@prestaconcept.net>
*/
class GoogleMultilangUrlDecorator extends UrlDecorator
{
const REL_ALTERNATE = 'alternate';
/**
* @var array
*/
protected $customNamespaces = array('xhtml' => 'http://www.w3.org/1999/xhtml');
/**
* @var string
*/
protected $linkXml = '';
/**
* add an alternative language to the url
*
* @param string $href - valid url of the translated page
* @param string $hreflang - valid language code @see http://www.w3.org/TR/xhtml-modularization/abstraction.html#dt_LanguageCode
* @param string $rel (default is alternate) - valid link type @see http://www.w3.org/TR/xhtml-modularization/abstraction.html#dt_LinkTypes
*/
public function addLink($href, $hreflang, $rel = null)
{
$this->linkXml .= $this->generateLinkXml($href, $hreflang, $rel);
return $this;
}
/**
* @param string $href
* @param string $hreflang
* @param string $rel
* @return string
*/
protected function generateLinkXml($href, $hreflang, $rel = null)
{
if (null == $rel) {
$rel = self::REL_ALTERNATE;
}
$xml = '<xhtml:link rel="' . $rel
. '" hreflang="' . $hreflang
. '" href="' . Utils::encode($href) . '" />';
return $xml;
}
/**
* add link elements before the closing tag
*
* @return string
*/
public function toXml()
{
$baseXml = $this->urlDecorated->toXml();
return str_replace('</url>', $this->linkXml . '</url>', $baseXml);
}
}