-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add XmlGenerator service and interface
Signed-off-by: Anton Fedurtsya <[email protected]>
- Loading branch information
Showing
5 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |