Commit 3f3110f3 authored by David Maus's avatar David Maus
Browse files

Initial commit

parents
((nil . ((php-project-name . "HAB RFC4287"))))
# GNU Emacs
*~
\#*
ChangeLog
# Composer
composer.lock
/vendor/
This diff is collapsed.
.PHONY: test
test:
phpunit
HAB RFC4287 – Atom Syndication Format for PHP
=============================================
This source code is Copyright (c) 2017 by Herzog August Bibliothek Wolfenbüttel and released under the terms of the GNU
General Public License 3 or higher.
{
"name": "hab/rfc4287",
"autoload": {
"psr-0": {
"HAB": "src/"
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="vendor/autoload.php" strict="true">
<testsuite name="Unit tests">
<directory suffix="Test.php">tests/unit-tests</directory>
</testsuite>
</phpunit>
<?php
/**
* This file is part of HAB RFC4287.
*
* HAB RFC4287 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HAB RFC4287 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HAB RFC4287. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\RFC4287;
use DOMDocument;
use SimpleXMLElement;
use DateTimeInterface;
use DateTimeImmutable;
/**
* Abstract base class of FeedBuilder and EntryBuilder.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
abstract class Builder
{
/**
* Current container.
*
* @var Model\Container
*/
protected $container;
/**
* Constructor.
*
* @return void
*/
public function __construct ()
{
$this->init();
}
/**
* Initialize builder.
*
* @return void
*/
abstract public function init ();
/**
* Set container property.
*
* @param string $name
* @param Model\VisitableInterface $value
* @return static
*/
public function set ($name, Model\VisitableInterface $value)
{
$this->container[$name] = $value;
return $this;
}
/**
* Return container property.
*
* @param string $name
* @return Model\VisitableInterface|null
*/
public function get ($name)
{
if (isset($this->container[$name])) {
return $this->container[$name];
}
}
/**
* Add a link.
*
* @param string $target
* @param string $relation
* @param string $title
* @param string $type
* @param string $hreflang
* @param string $length
* @return static
*/
public function link ($target, $relation, $title = null, $type = null, $hreflang = null, $length = null)
{
if (!isset($this->container['link'])) {
$this->container['link'] = new Model\VisitableAggregate();
}
$this->container['link']->append(new Model\Link($target, $relation, $title, $type, $hreflang, $length));
return $this;
}
/**
* Add updated timestamp.
*
* @param string|DateTimeInterface $datetime
* @return static
*/
public function updated ($datetime)
{
if (!$datetime instanceof DateTimeInterface) {
$datetime = new DateTimeImmutable($datetime);
}
$this->container['updated'] = new Model\DateConstruct($datetime);
return $this;
}
/**
* Add title.
*
* @param mixed $content
* @param string $type
* @return static
*/
public function title ($content, $type = 'text')
{
$title = $this->makeTextConstruct($content, $type);
$this->container['title'] = $title;
return $this;
}
/**
* Add id.
*
* @param string $id
* @return static
*/
public function id ($id)
{
$this->container['id'] = new Model\Literal($id);
return $this;
}
/**
* Add category.
*
* @param string $term
* @param string $scheme
* @param string $label
* @return static
*/
public function category ($term, $scheme = null, $label = null)
{
if (!isset($this->container['category'])) {
$this->container['category'] = new Model\VisitableAggregate();
}
$this->container['category']->append(new Model\Category($term, $scheme, $label));
return $this;
}
/**
* Add author.
*
* @param string $name
* @param string $email
* @param string $url
* @return static
*/
public function author ($name, $email = null, $url = null)
{
$author = new Model\PersonConstruct($name, $email, $url);
if (!isset($this->container['author'])) {
$this->container['author'] = new Model\VisitableAggregate();
}
$this->container['author']->append($author);
return $this;
}
/**
* Add contributor.
*
* @param string $name
* @param string $email
* @param string $url
* @return static
*/
public function contributor ($name, $email = null, $url = null)
{
$contributor = new Model\PersonConstruct($name, $email, $url);
if (!isset($this->container['contributor'])) {
$this->container['contributor'] = new Model\VisitableAggregate();
}
$this->container['contributor']->append($contributor);
return $this;
}
/**
* Add rights.
*
* @param mixed $rights
* @param string $type
* @return static
*/
public function rights ($rights, $type = 'text')
{
$text = $this->makeTextConstruct($rights, $type);
$this->container['rights'] = $text;
return $this;
}
/**
* Return text construct.
*
* @param mixed $content
* @param string $type
* @param array $attributes
* @return Model\TextConstruct
*/
public function makeTextConstruct ($content, $type)
{
if ($type === 'xhtml' or preg_match('@^[^/]+/(xml|[^+]+\+xml)$@u', $type)) {
$text = new Model\XmlTextConstruct($content, $type);
} else {
$text = new Model\PlainTextConstruct($content, $type);
}
return $text;
}
}
\ No newline at end of file
<?php
/**
* This file is part of HAB RFC4287.
*
* HAB RFC4287 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HAB RFC4287 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HAB RFC4287. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\RFC4287;
/**
* Entry builder.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class EntryBuilder extends Builder
{
/**
* Feed builder.
*
* @var FeedBuilder
*/
private $feed;
/**
* Constructor.
*
* @param FeedBuilder $feed
* @return void
*/
public function __construct (FeedBuilder $feed)
{
parent::__construct();
$this->feed = $feed;
}
/**
* {@inheritDoc}
*/
public function init ()
{
$this->container = new Model\Entry();
}
/**
* Add content.
*
* @param mixed $content
* @param string $type
* @param string $source
* @return self
*/
public function content ($content, $type = 'text', $source = null)
{
if ($source) {
$text = new Model\OutOfLineContent($source, $type);
} else {
$text = $this->makeTextConstruct($content, $type);
}
$this->container['content'] = $text;
return $this;
}
/**
* Add source.
*
* @param Model\Feed $source
* @return static
*/
public function source (Model\Feed $source)
{
$this->container['source'] = $source;
return $this;
}
/**
* Add summary.
*
* @param mixed $summary
* @param string $type
* @return self
*/
public function summary ($summary, $type = 'text')
{
$text = $this->makeTextConstruct($summary, $type);
$this->container['summary'] = $text;
return $this;
}
/**
* Add current entry to feed.
*
* @return FeedBuilder
*/
public function add ()
{
$this->feed->entry($this->container);
$this->init();
return $this->feed;
}
}
\ No newline at end of file
<?php
/**
* This file is part of HAB RFC4287.
*
* HAB RFC4287 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HAB RFC4287 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HAB RFC4287. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\RFC4287;
/**
* Feed builder.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class FeedBuilder extends Builder
{
/**
* Entry builder instance.
*
* @var EntryBuilder
*/
private $entries;
/**
* {@inheritDoc}
*/
public function init ()
{
$this->container = new Model\Feed();
}
/**
* Add entry or return entry builder.
*
* @param Entry $entry
* @return static|EntryBuilder
*/
public function entry (Model\Entry $entry = null)
{
if ($entry) {
if (!isset($this->container['entry'])) {
$this->container['entry'] = new Model\VisitableAggregate();
}
$this->container['entry']->append($entry);
if ($entry['updated'] > $this->container['updated']) {
$this->container['updated'] = clone($entry['updated']);
}
return $this;
}
if (!$this->entries) {
$this->entries = new EntryBuilder($this);
}
return $this->entries;
}
/**
* Add generator.
*
* @param string $name
* @param string $version
* @param string $uri
* @return self
*/
public function generator ($name, $version = null, $uri = null)
{
$this->container['generator'] = new Model\Generator($name, $version, $uri);
return $this;
}
/**
* Add icon.
*
* @param string $uri
* @return static
*/
public function icon ($uri)
{
$this->container['icon'] = new Model\Literal($uri);
return $this;
}
/**
* Add logo.
*
* @param string $uri
* @return static
*/
public function logo ($uri)
{
$this->container['logo'] = new Model\Literal($uri);
return $this;
}
/**
* Add subtitle.
*
* @param mixed $subtitle
* @param string $type
* @return self
*/
public function subtitle ($subtitle, $type = 'text')
{
$text = $this->makeTextConstruct($subtitle, $type);
$this->container['subtitle'] = $text;
return $this;
}
/**
* Return current feed.
*
* @return Feed
*/
public function getFeed ()
{
if (!$this->container['updated']) {
$this->updated('now');
}
$this->container->sort();
return $this->container;
}
}
\ No newline at end of file
<?php
/**
* This file is part of HAB RFC4287.
*
* HAB RFC4287 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HAB RFC4287 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HAB RFC4287. If not, see <http://www.gnu.org/licenses/>.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
namespace HAB\RFC4287\Model;
/**
* Category.
*
* @author David Maus <maus@hab.de>
* @copyright (c) 2016 by Herzog August Bibliothek Wolfenbüttel
* @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3 or higher
*/
class Category implements VisitableInterface
{
/**
* Term.
*
* @var string
*/
private $term;
/**
* Scheme.
*
* @var string
*/
private $scheme;
/**
* Label.
*
* @var string
*/
private $label;
/**
* Constructor.
*
* @param string $term
* @param string $scheme
* @param string $label
* @return void
*/
public function __construct ($term, $scheme = null, $label = null)
{
$this->term = $term;
$this->scheme = $scheme;
$this->label = $label;
}
/**
* Return term.
*
* @return string
*/
public function getTerm ()
{
return $this->term;
}
/**
* Return scheme.
*
* @return string|null
*/
public function getScheme ()
{