Skip to content

Instantly share code, notes, and snippets.

@WinterSilence
Last active January 31, 2020 17:18
Show Gist options
  • Save WinterSilence/7c5d025b14686cfadfad275cff1106f4 to your computer and use it in GitHub Desktop.
Save WinterSilence/7c5d025b14686cfadfad275cff1106f4 to your computer and use it in GitHub Desktop.
Version of Sitemap\Url
<?php
namespace Sitemap;
/**
* Basic sitemap URL class.
*/
class Url extends UrlAbstract
{
/**
* Valid values of "changefreq" element.
*/
const ALWAYS = 'always';
const HOURLY = 'hourly';
const DAILY = 'daily';
const WEEKLY = 'weekly';
const MONTHLY = 'monthly';
const YEARLY = 'yearly';
const NEVER = 'never';
/**
* @var string The date of last modification of the document.
*/
private $lastmod;
/**
* @var string How frequently the page is likely to change.
*/
private $changefreq;
/**
* @var string The priority of this URL relative to other URLs on site.
*/
private $priority;
/**
* Returns valid values of "changefreq" element.
* @return array
*/
public function getValidFrequencies()
{
return array(
self::ALWAYS,
self::HOURLY,
self::DAILY,
self::WEEKLY,
self::MONTHLY,
self::YEARLY,
self::NEVER
);
}
/**
* Sets "lastmod" element.
* @param string|int $date
* @return self
*/
private function setLastmod($date)
{
$this->lastmod = date('c', $date);
return $this;
}
/**
* Sets "changefreq" element.
* @param string $frequency
* @return self
* @throws \InvalidArgumentException
*/
private function setChangefreq($frequency)
{
if (!in_array($frequency, $this->getValidFrequencies(), true)) {
throw new \InvalidArgumentException(
'Invalid value (' . $frequency . ') of "changefreq" element.'
);
}
$this->changefreq = $frequency;
return $this;
}
/**
* Sets "priority" element.
* @param string|float $priority
* @return self
* @throws \InvalidArgumentException
*/
private function setPriority($priority)
{
if (!is_numeric($priority) || $priority < 0 || $priority > 1) {
throw new \InvalidArgumentException(
'Invalid value (' . $priority . ') of "priority" element.'
);
}
$this->priority = number_format($priority, 1, '.', '');
return $this;
}
}
<?php
namespace Sitemap;
/**
* Sitemap URL interface.
* @see http://sitemaps.org/protocol.html#xmlTagDefinitions
*/
interface UrlInterface
{
const MAX_URL_LENGTH = 2047;
/**
* Url constructor.
* @param string $location location item URL
* @param array $optional optional elements\tags
*/
public function __construct($location, array $optional = array());
/**
* Get values of sub elements\tags.
* @return array Pairs of tag => value.
*/
public function getElements();
}
<?php
namespace Sitemap;
/**
* Abstract sitemap URL.
*/
abstract class UrlAbstract implements UrlInterface
{
/**
* @var string
*/
private $urlString;
/**
* @var string URL of the page.
*/
private $loc;
/**
* Url constructor.
* @param string $location location item URL
* @param array $optional optional elements\tags
* @throws \InvalidArgumentException
*/
public function __construct($location, array $optional = array())
{
if (false === filter_var($location, FILTER_VALIDATE_URL)) {
throw new \InvalidArgumentException(
'The location URL "' . $location . '" is invalid.'
);
}
if (mb_strlen($location, 'UTF-8') > self::MAX_URL_LENGTH) {
throw new \InvalidArgumentException(
'The location must be less than ' . (self::MAX_URL_LENGTH + 1) . ' characters.'
);
}
$this->loc = $location;
foreach ($optional as $property => $value) {
if (!property_exists($this, $property)) {
throw new \InvalidArgumentException(
'Element "' . $property . '" not defined.'
);
}
$method = 'set' . ucfirst($property);
if (!method_exists($this, $method)) {
throw new \InvalidArgumentException(
'Element "' . $property . '" not writable.'
);
}
$this->{$method}($value);
}
}
/**
* Get values of elements\tags.
* @return array Pairs of tag => value.
*/
public function getElements()
{
return get_object_vars($this);
}
/**
*
* @return string
*/
public function __toSring()
{
if ($this->urlString !== null) {
$writer = new \XMLWriter;
$writer->openMemory();
$writer->startElement('url');
foreach ($this->getElements() as $name => $value) {
$writer->writeElement($name, $value);
}
$writer->endElement();
$this->urlString = (string)$writer->flush();
}
return $this->urlString;
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment