Skip to content

Commit

Permalink
Add XmlGenerator service and interface
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Oct 14, 2023
1 parent d4edb37 commit 9e715ae
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions services.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
imports:
- { resource: src/Repository/services.yaml }
- { resource: src/Service/services.yaml }

services:
_defaults:
Expand Down
49 changes: 49 additions & 0 deletions src/Service/XmlGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace FreshAdvance\Sitemap\Service;

use FreshAdvance\Sitemap\DataStructure\UrlInterface;

class XmlGenerator implements XmlGeneratorInterface
{
public function generateUrlItem(UrlInterface $url): string
{
$attributes = [
$this->wrap($url->getLocation(), "loc"),
$this->wrap($url->getLastModified(), "lastmod"),
$this->wrap($url->getChangeFrequency(), "changefreq"),
$this->wrap((string)$url->getPriority(), "priority"),
];

return $this->wrap(implode("", $attributes), "url");
}

protected function wrap(string $data, string $tag, array $tagAttributes = []): string
{
$attributes = '';
foreach ($tagAttributes as $key => $attributeValue) {
$attributes .= " {$key}=\"{$attributeValue}\"";
}

return "<{$tag}{$attributes}>" . $data . "</{$tag}>";
}

public function generateSitemapDocument(iterable $items): string
{
$document = '<?xml version="1.0" encoding="UTF-8"?>';

$urlBlocks = [];
foreach ($items as $oneItem) {
$urlBlocks[] = $this->generateUrlItem($oneItem);
}

return $document
. $this->wrap(
implode("", $urlBlocks),
"urlset",
[
"xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9"
]
);
}
}
8 changes: 8 additions & 0 deletions src/Service/XmlGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace FreshAdvance\Sitemap\Service;

interface XmlGeneratorInterface
{
public function generateSitemapDocument(iterable $items): string;
}
8 changes: 8 additions & 0 deletions src/Service/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:

_defaults:
public: false
autowire: true

FreshAdvance\Sitemap\Service\XmlGeneratorInterface:
class: FreshAdvance\Sitemap\Service\XmlGenerator
54 changes: 54 additions & 0 deletions tests/Unit/Service/XmlGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace FreshAdvance\Sitemap\Tests\Unit\Service;

use FreshAdvance\Sitemap\DataStructure\UrlInterface;
use FreshAdvance\Sitemap\Service\XmlGenerator;

class XmlGeneratorTest extends \PHPUnit\Framework\TestCase
{
public function testGenerateUrlItem(): void
{
$urlStub = $this->createConfiguredMock(UrlInterface::class, [
'getLocation' => 'someLocation',
'getLastModified' => 'lastModifiedDate',
'getChangeFrequency' => 'someFrequency',
'getPriority' => 0.6,
]);

$expectation = implode("", [
"<url>",
"<loc>someLocation</loc>",
"<lastmod>lastModifiedDate</lastmod>",
"<changefreq>someFrequency</changefreq>",
"<priority>0.6</priority>",
"</url>",
]);

$sut = $this->createPartialMock(XmlGenerator::class, []);
$this->assertSame($expectation, $sut->generateUrlItem($urlStub));
}

public function testGenerateSitemapContent(): void
{
$sut = $this->createPartialMock(XmlGenerator::class, ['generateUrlItem']);
$sut->expects($this->exactly(3))->method('generateUrlItem')->willReturn("ItemContent");

$expectation = implode("", [
'<?xml version="1.0" encoding="UTF-8"?>',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
'ItemContent',
'ItemContent',
'ItemContent',
'</urlset>'
]);

$items = [
$this->createStub(UrlInterface::class),
$this->createStub(UrlInterface::class),
$this->createStub(UrlInterface::class),
];

$this->assertSame($expectation, $sut->generateSitemapDocument($items));
}
}

0 comments on commit 9e715ae

Please sign in to comment.