Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

.DS_Store
composer.lock
.phpunit.result.cache
10 changes: 3 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: php
php:
- 5.4
- 5.5
- 5.6
- hhvm
- 7.3
- 7.4
- 8.0

before_script:
- composer self-update
Expand All @@ -12,7 +11,4 @@ before_script:
script: phpunit

matrix:
allow_failures:
- php: 5.6
- php: hhvm
fast_finish: true
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
"email": "dh@tackk.com"
}
],
"minimum-stability": "dev",
"require-dev": {
"phpunit/phpunit": "4.0.*"
"phpunit/phpunit": "^9.5"
},
"require": {
"php": ">=5.4",
"league/flysystem": "1.0.*"
"php": ">=7.3",
"league/flysystem": "^3.0",
"ext-dom": "*"
},
"autoload": {
"files": [
Expand Down
67 changes: 24 additions & 43 deletions src/AbstractSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,21 @@

namespace Tackk\Cartographer;

use DateTime;
use DateTimeZone;
use DOMDocument;
use DOMElement;
use InvalidArgumentException;
use RuntimeException;

class MaxUrlCountExceededException extends RuntimeException
{
}

use Tackk\Cartographer\Exception\MaxUrlCountExceededException;

abstract class AbstractSitemap
{
const MAX_URLS = 50000;
public const MAX_URLS = 50000;

/**
* Get the root node name for the sitemap (e.g. 'urlset').
* @return string
*/
abstract protected function getRootNodeName();
abstract protected function getRootNodeName(): string;

/**
* Get the node name for the sitemap (e.g. 'url').
* @return string
*/
abstract protected function getNodeName();
abstract protected function getNodeName(): string;

/**
* @var string
Expand All @@ -46,12 +34,12 @@ abstract protected function getNodeName();
protected $xmlNamespaceUri = 'http://www.sitemaps.org/schemas/sitemap/0.9';

/**
* @var DOMDocument
* @var \DOMDocument
*/
protected $document;

/**
* @var DOMElement
* @var \DOMElement
*/
protected $rootNode;

Expand All @@ -70,7 +58,7 @@ abstract protected function getNodeName();
*/
public function __construct()
{
$this->document = new DOMDocument($this->xmlVersion, $this->xmlEncoding);
$this->document = new \DOMDocument($this->xmlVersion, $this->xmlEncoding);
$this->rootNode = $this->document->createElementNS($this->xmlNamespaceUri, $this->getRootNodeName());

// Make the output Pretty
Expand All @@ -80,49 +68,45 @@ public function __construct()
/**
* Freeze the sitemap, and append the rootNode to the document.
*/
public function freeze()
public function freeze(): void
{
$this->document->appendChild($this->rootNode);
$this->isFrozen = true;
}

public function isFrozen()
public function isFrozen(): bool
{
return $this->isFrozen;
}

/**
* Gets the number of Urls in the sitemap.
* @return int
*/
public function getUrlCount()
public function getUrlCount(): int
{
return $this->urlCount;
}

/**
* Checks if the sitemap contains the maximum URL count.
* @return bool
*/
public function hasMaxUrlCount()
public function hasMaxUrlCount(): bool
{
return $this->urlCount === static::MAX_URLS;
}

/**
* Converts the Sitemap to an XML string.
* @return string
*/
public function toString()
public function toString(): string
{
return (string) $this;
}

/**
* Converts the Sitemap to an XML string.
* @return string
*/
public function __toString()
public function __toString(): string
{
if (!$this->isFrozen()) {
$this->freeze();
Expand All @@ -133,11 +117,10 @@ public function __toString()

/**
* Adds a URL to the document with the given array of elements.
* @param array $urlArray
* @return $this
*
* @throws MaxUrlCountExceededException
*/
protected function addUrlToDocument(array $urlArray)
protected function addUrlToDocument(array $urlArray): self
{
if ($this->hasMaxUrlCount()) {
throw new MaxUrlCountExceededException('Maximum number of URLs has been reached, cannot add more.');
Expand All @@ -149,7 +132,7 @@ protected function addUrlToDocument(array $urlArray)
if (is_null($value)) {
continue;
}
$node->appendChild(new DOMElement($key, $value));
$node->appendChild(new \DOMElement($key, $value));
}
$this->rootNode->appendChild($node);
$this->urlCount++;
Expand All @@ -160,9 +143,8 @@ protected function addUrlToDocument(array $urlArray)
/**
* Escapes a string so it can be inserted into the Sitemap
* @param string $string The string to escape.
* @return string
*/
protected function escapeString($string)
protected function escapeString(string $string): string
{
$from = ['&', '\'', '"', '>', '<'];
$to = ['&amp;', '&apos;', '&quot;', '&gt;', '&lt;'];
Expand All @@ -172,23 +154,22 @@ protected function escapeString($string)

/**
* Takes a date as a string (or int in the case of a unix timestamp).
* @param string $dateString
* @return string
* @throws InvalidArgumentException
*
* @throws \InvalidArgumentException
*/
protected function formatDate($dateString)
protected function formatDate(string $dateString): string
{
try {
// We have to handle timestamps a little differently
if (is_numeric($dateString) && (int) $dateString == $dateString) {
$date = DateTime::createFromFormat('U', (int) $dateString, new DateTimeZone('UTC'));
$date = \DateTime::createFromFormat('U', (int) $dateString, new \DateTimeZone('UTC'));
} else {
$date = new DateTime($dateString, new DateTimeZone('UTC'));
$date = new \DateTime($dateString, new \DateTimeZone('UTC'));
}

return $date->format(DateTime::W3C);
return $date->format(\DateTimeInterface::W3C);
} catch (\Exception $e) {
throw new InvalidArgumentException("Malformed last modified date: {$dateString}", 0, $e);
throw new \InvalidArgumentException("Malformed last modified date: {$dateString}", 0, $e);
}
}
}
14 changes: 7 additions & 7 deletions src/ChangeFrequency.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

class ChangeFrequency
{
const ALWAYS = 'always';
const HOURLY = 'hourly';
const DAILY = 'daily';
const WEEKLY = 'weekly';
const MONTHLY = 'monthly';
const YEARLY = 'yearly';
const NEVER = 'never';
public const ALWAYS = 'always';
public const HOURLY = 'hourly';
public const DAILY = 'daily';
public const WEEKLY = 'weekly';
public const MONTHLY = 'monthly';
public const YEARLY = 'yearly';
public const NEVER = 'never';
}
7 changes: 7 additions & 0 deletions src/Exception/MaxUrlCountExceededException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Tackk\Cartographer\Exception;

class MaxUrlCountExceededException extends \RuntimeException
{
}
11 changes: 3 additions & 8 deletions src/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,20 @@

class Sitemap extends AbstractSitemap
{
protected function getRootNodeName()
protected function getRootNodeName(): string
{
return 'urlset';
}

protected function getNodeName()
protected function getNodeName(): string
{
return 'url';
}

/**
* Adds the URL to the urlset.
* @param string $loc
* @param string|int $lastmod
* @param string $changefreq
* @param float $priority
* @return $this
*/
public function add($loc, $lastmod = null, $changefreq = null, $priority = null)
public function add(string $loc, string $lastmod = null, string $changefreq = null, float $priority = null): self
{
$loc = $this->escapeString($loc);
$lastmod = !is_null($lastmod) ? $this->formatDate($lastmod) : null;
Expand Down
Loading