Browse Source

Initial work on XML entries

master
J. King 6 years ago
parent
commit
ed098c457e
  1. 2
      lib/Collection.php
  2. 3
      lib/Entry.php
  3. 1
      lib/JSON/Feed.php
  4. 2
      lib/XML/Construct.php
  5. 75
      lib/XML/Entry.php
  6. 11
      lib/XML/Feed.php
  7. 32
      lib/XML/Primitives/Construct.php

2
lib/Collection.php

@ -73,7 +73,7 @@ abstract class Collection implements \IteratorAggregate, \ArrayAccess, \Countabl
protected function filter(array $terms, string $axis, bool $inclusive): self {
$out = new static;
foreach ($this as $item) {
if (in_array($p->$axis, $terms)==$inclusive) {
if (in_array($item->$axis, $terms)==$inclusive) {
$out[] = $item;
}
}

3
lib/Entry.php

@ -21,9 +21,6 @@ abstract class Entry {
public $author;
public $dateModified;
/** Constructs a parsed feed */
abstract public function __construct($data, Feed $feed);
/** Parses the feed to extract sundry metadata */
protected function parse() {
$this->id = $this->getId();

1
lib/JSON/Feed.php

@ -91,7 +91,6 @@ class Feed extends \JKingWeb\Lax\Feed {
continue;
} else {
$out[] = $entry;
break;
}
}
return $out;

2
lib/XML/Construct.php

@ -45,7 +45,7 @@ trait Construct {
$out = [];
$nodes = $this->xpath->query($query, $context ?? $this->subject);
foreach ($nodes as $node) {
$out[] = $this->trimText($node->item(0)->textContent);
$out[] = $this->trimText($node->textContent);
}
return ($out) ? $out : null;
}

75
lib/XML/Entry.php

@ -0,0 +1,75 @@
<?php
/** @license MIT
* Copyright 2018 J. King et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Lax\XML;
use JKingWeb\Lax\Person\Collection as PersonCollection;
use JKingWeb\Lax\Category\Collection as CategoryCollection;
class Entry extends \JKingWeb\Lax\Entry {
use Construct;
use Primitives\Construct;
use Primitives\Entry;
/** Constructs a parsed feed */
public function __construct(\DOMElement $data, Feed $feed, XPath $xpath = null) {
$this->init($data, $feed, $xpath);
$this->parse();
}
/** Performs initialization of the instance */
protected function init(\DOMElement $node, Feed $feed, XPath $xpath = null) {
$this->xpath = $xpath ?? new XPath($node->ownerDocument);
$this->subject = $node;
$this->feed = $feed;
}
/** General function to fetch the canonical entry URL
*
* If the entry does not include a canonical URL, the request URL is returned instead
*/
public function getUrl(): string {
return $this->getUrlAtom() ?? $this->getUrlRss1() ?? $this->getUrlPod() ?? $this->reqUrl;
}
/** General function to fetch the entry title */
public function getTitle(): string {
return $this->getTitleAtom() ?? $this->getTitleRss1() ?? $this->getTitleRss2() ?? $this->getTitleDC() ?? $this->getTitlePod() ?? "";
}
/** General function to fetch the entry's Web-representation URL */
public function getLink(): string {
return $this->getLinkAtom() ?? $this->getLinkRss1() ?? $this->getLinkRss2() ?? "";
}
/** General function to fetch the description of a entry */
public function getSummary(): string {
// unlike most other data, Atom is not preferred, because Atom doesn't really have entry summaries
return $this->getSummaryDC() ?? $this->getSummaryRss1() ?? $this->getSummaryRss2() ?? $this->getSummaryPod() ?? $this->getSummaryAtom() ?? "";
}
/** General function to fetch the categories of a entry */
public function getCategories(): CategoryCollection {
return $this->getCategoriesAtom() ?? $this->getCategoriesRss2() ?? $this->getCategoriesDC() ?? $this->getCategoriesPod() ?? new CategoryCollection;
}
/** General function to fetch the entry identifier */
public function getId(): string {
return $this->getIdAtom() ?? $this->getIdDC() ?? $this->getIdRss2() ?? "";
}
/** General function to fetch a collection of all people associated with a entry */
public function getPeople(): PersonCollection {
$authors = $this->getAuthorsAtom() ?? $this->getAuthorsDC() ?? $this->getAuthorsPod() ?? $this->getAuthorsRss2() ?? $this->feed->people->filterForRole("author");
$contributors = $this->getContributorsAtom() ?? $this->getContributorsDC() ?? new PersonCollection;
return $authors->merge($contributors);
}
/** General function to fetch the modification date of a entry */
public function getDateModified() {
return $this->getDateModifiedAtom() ?? $this->getDateModifiedDC() ?? $this->getDateModifiedRss2();
}
}

11
lib/XML/Feed.php

@ -79,12 +79,7 @@ class Feed extends \JKingWeb\Lax\Feed {
return $this->getSummaryDC() ?? $this->getSummaryRss1() ?? $this->getSummaryRss2() ?? $this->getSummaryPod() ?? $this->getSummaryAtom() ?? "";
}
/** General function to fetch the categories of a feed
*
* If the $grouped parameter is true, and array of arrays will be returned, keyed by taxonomy/scheme
*
* The $humanFriendly parameter only affects Atom categories
*/
/** General function to fetch the categories of a feed */
public function getCategories(): CategoryCollection {
return $this->getCategoriesAtom() ?? $this->getCategoriesRss2() ?? $this->getCategoriesDC() ?? $this->getCategoriesPod() ?? new CategoryCollection;
}
@ -110,8 +105,6 @@ class Feed extends \JKingWeb\Lax\Feed {
/** General function to fetch the entries of a feed */
public function getEntries(): array {
$out = [];
// do stuff
return $out;
return $this->getEntriesAtom() ?? $this->getEntriesRss1() ?? $this->getEntriesRss2() ?? [];
}
}

32
lib/XML/Primitives/Construct.php

@ -10,6 +10,7 @@ use JKingWeb\Lax\Person\Person;
use JKingWeb\Lax\Person\Collection as PersonCollection;
use JKingWeb\Lax\Category\Category;
use JKingWeb\Lax\Category\Collection as CategoryCollection;
use JKingWeb\Lax\XML\Entry as FeedEntry;
trait Construct {
@ -161,9 +162,9 @@ trait Construct {
return $this->fetchPeople("webMaster", "webMaster");
}
/** Primitive to fetch a collection of authors associated with an Atom feed */
/** Primitive to fetch a collection of contributors associated with an Atom feed */
protected function getContributorsAtom() {
return $this->fetchPeopleAtom("atom:author", "author");
return $this->fetchPeopleAtom("atom:contributor", "contributor");
}
/** Primitive to fetch a collection of authors associated with a podcast/episode
@ -221,4 +222,31 @@ trait Construct {
protected function getDateCreatedAtom() {
return $this->fetchDate("atom:published");
}
/** Primitive to fetch the list of entries in an Atom feed */
protected function getEntriesAtom() {
$out = [];
foreach ($this->fetchElements("atom:entry") ?? [] as $node) {
$out[] = new FeedEntry($node, $this, $this->xpath);
}
return count($out) ? $out : null;
}
/** Primitive to fetch the list of entries in an RDF feed */
protected function getEntriesRss1() {
$out = [];
foreach ($this->fetchElements("rss1:item|rss0:item", $this->subject->ownerDocument->documentElement) ?? $this->fetchElements("rss1:item|rss0:item") ?? [] as $node) {
$out[] = new FeedEntry($node, $this, $this->xpath);
}
return count($out) ? $out : null;
}
/** Primitive to fetch the list of entries in an RSS feed */
protected function getEntriesRss2() {
$out = [];
foreach ($this->fetchElements("item") ?? [] as $node) {
$out[] = new FeedEntry($node, $this, $this->xpath);
}
return count($out) ? $out : null;
}
}

Loading…
Cancel
Save