Skip to content

Commit

Permalink
Merge pull request #14 from melbahja/feat/localized
Browse files Browse the repository at this point in the history
add support for localized page variations.
  • Loading branch information
melbahja authored May 31, 2022
2 parents 0c7e828 + 1fa7f91 commit 0ef0a7c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Interfaces/SitemapBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ interface SitemapBuilderInterface extends SeoInterface
*/
public const VIDEO_NS = 'http://www.google.com/schemas/sitemap-video/1.1';


/**
* XHTML links namespace
*/
public const XHTML_NS = 'http://www.w3.org/1999/xhtml';

/**
* News namespace
* @var string
Expand Down
35 changes: 35 additions & 0 deletions src/Sitemap/SitemapBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class SitemapBuilder implements SitemapBuilderInterface
[
'images' => false,
'videos' => false,
'localized' => false,
];


Expand All @@ -89,6 +90,10 @@ public function __construct(string $domain, ?array $options = null, string $ns =
$urlset .= ' xmlns:video="'. static::VIDEO_NS .'"';
}

if ($this->options['localized']) {
$urlset .= 'xmlns:xhtml="'. static::XHTML_NS .'"';
}

$this->doc = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?>' . $urlset . "{$ns}/>");
}

Expand Down Expand Up @@ -123,6 +128,23 @@ public function url(string $url): SitemapBuilderInterface
return $this;
}

/**
* Set alternative language url for multi lang support.
*
* @param string $url
* @param string $lang ISO 639-1 or ISO 3166-1 alpha-2
* @return SitemapBuilderInterface
*/
public function alternate(string $path, string $lang)
{
if ($path[0] !== '/') {
$path = "/{$path}";
}

$this->url['alternate'][] = [Helper::escapeUrl($this->domain . $path), $lang];
return $this;
}

/**
* Append url
*
Expand Down Expand Up @@ -166,6 +188,19 @@ public function append(): SitemapBuilderInterface
$child->addChild("{$n}:{$k}", $p);
}

continue;

} elseif ($n === 'alternate') {


foreach ($v as $k => $alt)
{
$child = $url->addChild('xhtml:link', null, static::XHTML_NS);
$child->addAttribute('rel', 'alternate');
$child->addAttribute('href', $alt[0]);
$child->addAttribute('hreflang', $alt[1]);
}

continue;
}

Expand Down
25 changes: 25 additions & 0 deletions tests/SitemapsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,31 @@ public function testBuildedSitemapWithVideos()
}


public function testBuildedSitemapWithLocalizedUrls()
{
$sitemap = $this->sitemapProvider();
$name = 'm.xml';

$sitemap->links(['name' => $name, 'localized' => true], function($map)
{
$map->loc('/blog/12')->freq('weekly')->priority('0.7');

$map->loc('/blog/13')->freq('monthly')->priority('0.8')->alternate("/ar/blog/13", "ar")->alternate("/de/blog/13", "de");

$map->loc('/blog/14')->alternate("/ar/blog/14", "ar")->alternate("/de/blog/14", "de");
});

$this->assertTrue($sitemap->save());

$this->assertEquals('<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated by https://git.io/phpseo -->
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"><url><loc>https://example.com/blog/12</loc><changefreq>weekly</changefreq><priority>0.7</priority></url><url><loc>https://example.com/blog/13</loc><changefreq>monthly</changefreq><priority>0.8</priority><xhtml:link rel="alternate" href="https://example.com/ar/blog/13" hreflang="ar"/><xhtml:link rel="alternate" href="https://example.com/de/blog/13" hreflang="de"/></url><url><loc>https://example.com/blog/14</loc><xhtml:link rel="alternate" href="https://example.com/ar/blog/14" hreflang="ar"/><xhtml:link rel="alternate" href="https://example.com/de/blog/14" hreflang="de"/></url></urlset>',
trim(file_get_contents($sitemap->getSavePath() . "/{$name}"))
);

}


public function testSitemapExistent()
{
$sitemap = $this->sitemapProvider();
Expand Down

0 comments on commit 0ef0a7c

Please sign in to comment.