24 lines
712 B
PHP
24 lines
712 B
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use Generator;
|
|
|
|
final class PublicSitemapXml
|
|
{
|
|
/** @param iterable<string> $locations
|
|
* @return Generator<int, string>
|
|
*/
|
|
public function chunks(iterable $locations, bool $index = false): Generator
|
|
{
|
|
$root = $index ? 'sitemapindex' : 'urlset';
|
|
$item = $index ? 'sitemap' : 'url';
|
|
yield '<?xml version="1.0" encoding="UTF-8"?>'."\n";
|
|
yield '<'.$root.' xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n";
|
|
foreach ($locations as $location) {
|
|
yield '<'.$item.'><loc>'.htmlspecialchars($location, ENT_XML1 | ENT_QUOTES, 'UTF-8').'</loc></'.$item.'>'."\n";
|
|
}
|
|
yield '</'.$root.'>';
|
|
}
|
|
}
|