-
Notifications
You must be signed in to change notification settings - Fork 4
Support for creating sitemaps #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
77d81d2
ac8f787
47dab57
7fe5786
ff578fa
93ba949
cfef249
4b0735d
87bcf9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Inteve\FeedGenerator\Feeds\Sitemap; | ||
|
||
use Inteve\FeedGenerator\Feed; | ||
use Inteve\FeedGenerator\InvalidItemException; | ||
use Inteve\FeedGenerator\IOutput; | ||
use Inteve\FeedGenerator\Utils\Helpers; | ||
|
||
|
||
class SitemapFeed extends Feed | ||
{ | ||
/** | ||
* @return string | ||
*/ | ||
public function getContentType() | ||
{ | ||
return 'text/xml'; | ||
} | ||
|
||
|
||
/** | ||
* @return void | ||
* @throws InvalidItemException | ||
*/ | ||
public function generate(IOutput $output) | ||
{ | ||
$output->open(); | ||
$output->output('<?xml version="1.0" encoding="utf-8"?>'); | ||
$output->output("\n"); | ||
$output->output('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'); | ||
$output->output("\n"); | ||
|
||
foreach ($this->items as $item) { | ||
if (!($item instanceof SitemapItem)) { | ||
throw new InvalidItemException('Feed item must be instance of SitemapItem.'); | ||
} | ||
|
||
$item->validate(); | ||
|
||
$output->output("<url>\n"); | ||
|
||
Helpers::writeXml($output, array( | ||
'loc' => $item->getLocation(), | ||
'lastmod' => $item->getLastModified(), | ||
'priority' => $item->getPriority() | ||
)); | ||
|
||
$output->output("</url>\n"); | ||
} | ||
|
||
$output->output('</urlset>'); | ||
$output->output("\n"); | ||
$output->close(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace Inteve\FeedGenerator\Feeds\Sitemap; | ||
|
||
use Inteve\FeedGenerator\AssertException; | ||
use Inteve\FeedGenerator\IFeedItem; | ||
use Inteve\FeedGenerator\Utils\Helpers; | ||
use Nette\Utils\Validators; | ||
|
||
|
||
class SitemapItem implements IFeedItem | ||
{ | ||
|
||
/** @var string|int|NULL */ | ||
private $location; | ||
|
||
/** @var string|NULL */ | ||
private $lastModified; | ||
|
||
/** @var string|NULL */ | ||
private $priority; | ||
|
||
|
||
/** | ||
* @return string|int|NULL | ||
*/ | ||
public function getLocation() | ||
{ | ||
return $this->location; | ||
} | ||
|
||
|
||
/** | ||
* @param string|int | ||
* @return self | ||
*/ | ||
public function setLocation($location) | ||
{ | ||
$this->location = $location; | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* @return string|NULL | ||
*/ | ||
public function getLastModified() | ||
{ | ||
return $this->lastModified; | ||
} | ||
|
||
|
||
/** | ||
* @param string | ||
* @return self | ||
*/ | ||
public function setLastModified($lastModified) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Co u |
||
{ | ||
$this->lastModified = $lastModified; | ||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* @return string|NULL | ||
*/ | ||
public function getPriority() | ||
{ | ||
return $this->priority; | ||
} | ||
|
||
|
||
/** | ||
* @param string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Priorita by IMHO mohla akceptovat kromě stringu i float (resp. |
||
* @return self | ||
*/ | ||
public function setPriority($prioritys) | ||
{ | ||
$this->priority = $prioritys; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return void | ||
* @throws AssertException | ||
*/ | ||
public function validate() | ||
{ | ||
Helpers::assert(isset($this->location), 'Missing item location, call $item->setLocation().'); | ||
Helpers::assert(isset($this->lastModified), 'Missing item last modified date, call $item->setLastModified().'); | ||
Helpers::assert(isset($this->priority), 'Missing item priority, call $item->setPriority().'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Asi bych nevyžadoval nastavení |
||
} | ||
|
||
|
||
/** | ||
* @return static | ||
*/ | ||
public static function create() | ||
{ | ||
return new static; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opravdu může být
location
typuint
?