Browse Source

Deduplicate some parser code

master
J. King 4 years ago
parent
commit
f4f46c57a5
  1. 36
      lib/Parser/AbstractEntry.php
  2. 41
      lib/Parser/AbstractFeed.php
  3. 3
      lib/Parser/Feed.php
  4. 2
      lib/Parser/JSON/Construct.php
  5. 28
      lib/Parser/JSON/Entry.php
  6. 33
      lib/Parser/JSON/Feed.php
  7. 2
      lib/Parser/XML/Entry.php
  8. 31
      lib/Parser/XML/Feed.php

36
lib/Parser/AbstractEntry.php

@ -0,0 +1,36 @@
<?php
/** @license MIT
* Copyright 2018 J. King et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace MensBeam\Lax\Parser;
use MensBeam\Lax\Feed as FeedStruct;
use MensBeam\Lax\Entry as EntryStruct;
trait AbstractEntry {
public function __construct($data, FeedStruct $feed) {
$this->data = $data;
$this->feed = $feed;
$this->url = $feed->meta->url ? (string) $feed->meta->url : null;
}
public function parse(EntryStruct $entry = null): EntryStruct {
$entry = $this->init($entry ?? new EntryStruct);
$entry->lang = $this->getLang();
$entry->id = $this->getId();
$entry->link = $this->getLink();
$entry->relatedLink = $this->getRelatedLink();
$entry->title = $this->getTitle();
$entry->dateModified = $this->getDateModified();
$entry->dateCreated = $this->getDateCreated();
$entry->content = $this->getContent();
$entry->summary = $this->getSummary();
$entry->banner = $this->getBanner();
$entry->people = $this->getPeople();
$entry->categories = $this->getCategories();
$entry->enclosures = $this->getEnclosures();
return $entry;
}
}

41
lib/Parser/AbstractFeed.php

@ -0,0 +1,41 @@
<?php
/** @license MIT
* Copyright 2018 J. King et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace MensBeam\Lax\Parser;
use MensBeam\Lax\Url;
use MensBeam\Lax\Feed as FeedStruct;
trait AbstractFeed {
/** Constructs a feed parser */
public function __construct(string $data, string $contentType = null, string $url = null) {
$this->data = $data;
$this->contentType = $contentType;
if (strlen($url ?? "")) {
$this->url = $url;
}
}
/** Parses the feed to extract data */
public function parse(FeedStruct $feed = null): FeedStruct {
$feed = $this->init($feed ?? new FeedStruct);
$feed->meta->url = strlen($this->url ?? "") ? new Url($this->url) : null;
$feed->sched = $this->getSchedule();
$feed->id = $this->getId();
$feed->lang = $this->getLang();
$feed->url = $this->getUrl();
$feed->link = $this->getLink();
$feed->title = $this->getTitle();
$feed->summary = $this->getSummary();
$feed->dateModified = $this->getDateModified();
$feed->icon = $this->getIcon();
$feed->image = $this->getImage();
$feed->people = $this->getPeople();
$feed->categories = $this->getCategories();
$feed->entries = $this->getEntries($feed);
return $feed;
}
}

3
lib/Parser/Feed.php

@ -15,6 +15,9 @@ use MensBeam\Lax\Feed as FeedStruct;
use MensBeam\Lax\Url;
interface Feed {
/** Maximally populates a Feed object */
public function parse(FeedStruct $feed = null): FeedStruct;
/** Returns the globally unique identifier of the newsfeed; this is usually a URI */
public function getId(): ?string;

2
lib/Parser/JSON/Construct.php

@ -13,7 +13,7 @@ use MensBeam\Lax\Person\Collection as PersonCollection;
use MensBeam\Lax\Person\Person;
use MensBeam\Lax\Url;
trait Construct {
abstract class Construct {
use \MensBeam\Lax\Parser\Construct;
/** Returns an object member if the member exists and is of the expected type

28
lib/Parser/JSON/Entry.php

@ -17,8 +17,8 @@ use MensBeam\Lax\Enclosure\Enclosure;
use MensBeam\Lax\Text;
use MensBeam\Lax\Url;
class Entry implements \MensBeam\Lax\Parser\Entry {
use Construct;
class Entry extends Construct implements \MensBeam\Lax\Parser\Entry {
use \MensBeam\Lax\Parser\AbstractEntry;
protected $url;
/** @var \MensBeam\Lax\Feed */
@ -26,34 +26,10 @@ class Entry implements \MensBeam\Lax\Parser\Entry {
/** @var \Mimey\MimeTypes */
protected $mime;
public function __construct(\stdClass $data, FeedStruct $feed) {
$this->data = $data;
$this->feed = $feed;
$this->url = $feed->meta->url ? (string) $feed->meta->url : null;
}
protected function init(EntryStruct $entry): EntryStruct {
return $entry;
}
public function parse(EntryStruct $entry = null): EntryStruct {
$entry = $this->init($entry ?? new EntryStruct);
$entry->lang = $this->getLang();
$entry->id = $this->getId();
$entry->link = $this->getLink();
$entry->relatedLink = $this->getRelatedLink();
$entry->title = $this->getTitle();
$entry->dateModified = $this->getDateModified();
$entry->dateCreated = $this->getDateCreated();
$entry->content = $this->getContent();
$entry->summary = $this->getSummary();
$entry->banner = $this->getBanner();
$entry->people = $this->getPeople();
$entry->categories = $this->getCategories();
$entry->enclosures = $this->getEnclosures();
return $entry;
}
public function getId(): ?string {
$id = $this->fetchMember("id", "str") ?? $this->fetchMember("id", "int") ?? $this->fetchMember("id", "float");
if (is_null($id)) {

33
lib/Parser/JSON/Feed.php

@ -17,8 +17,8 @@ use MensBeam\Lax\Category\Collection as CategoryCollection;
use MensBeam\Lax\Parser\Exception;
use MensBeam\Lax\Parser\JSON\Entry as EntryParser;
class Feed implements \MensBeam\Lax\Parser\Feed {
use Construct;
class Feed extends Construct implements \MensBeam\Lax\Parser\Feed {
use \MensBeam\Lax\Parser\AbstractFeed;
protected const VERSIONS = [
'https://jsonfeed.org/version/1' => "1",
@ -34,15 +34,6 @@ class Feed implements \MensBeam\Lax\Parser\Feed {
protected $contentType;
protected $url;
/** Constructs a feed parser without actually doing anything */
public function __construct(string $data, string $contentType = null, string $url = null) {
$this->data = $data;
$this->contentType = $contentType;
if (strlen($url ?? "")) {
$this->url = $url;
}
}
/** Performs format-specific preparation and validation */
protected function init(FeedStruct $feed): FeedStruct {
$type = MimeType::parse($this->contentType);
@ -62,26 +53,6 @@ class Feed implements \MensBeam\Lax\Parser\Feed {
return $feed;
}
/** Parses the feed to extract data */
public function parse(FeedStruct $feed = null): FeedStruct {
$feed = $this->init($feed ?? new FeedStruct);
$feed->meta->url = strlen($this->url ?? "") ? new Url($this->url) : null;
$feed->sched = $this->getSchedule();
$feed->id = $this->getId();
$feed->lang = $this->getLang();
$feed->url = $this->getUrl();
$feed->link = $this->getLink();
$feed->title = $this->getTitle();
$feed->summary = $this->getSummary();
$feed->dateModified = $this->getDateModified();
$feed->icon = $this->getIcon();
$feed->image = $this->getImage();
$feed->people = $this->getPeople();
$feed->categories = $this->getCategories();
$feed->entries = $this->getEntries($feed);
return $feed;
}
/** {@inheritdoc}
*
* For JSON feeds this is always the feed URL specified in the feed

2
lib/Parser/XML/Entry.php

@ -18,6 +18,8 @@ use MensBeam\Lax\Text;
use MensBeam\Lax\Url;
class Entry extends Construct implements \MensBeam\Lax\Parser\Entry {
use \MensBeam\Lax\Parser\AbstractEntry;
protected const ENCLOSURE_ATTR_INTEGERS = [
'size' => "@fileSize",
'width' => "@width",

31
lib/Parser/XML/Feed.php

@ -19,6 +19,8 @@ use MensBeam\Lax\Text;
use MensBeam\Lax\Url;
class Feed extends Construct implements \MensBeam\Lax\Parser\Feed {
use \MensBeam\Lax\Parser\AbstractFeed;
protected const LIBXML_OPTIONS = \LIBXML_BIGLINES | \LIBXML_COMPACT | \LIBXML_HTML_NODEFDTD | \LIBXML_NOCDATA | \LIBXML_NOENT | \LIBXML_NONET | \LIBXML_NOERROR | LIBXML_NOWARNING;
public const MIME_TYPES = [
"application/atom+xml", // Atom
@ -39,15 +41,6 @@ class Feed extends Construct implements \MensBeam\Lax\Parser\Feed {
/** @var \DOMXpath */
protected $xpath;
/** Constructs a parsed feed */
public function __construct(string $data, string $contentType = null, string $url = null) {
$this->data = $data;
$this->contentType = $contentType;
if (strlen($url ?? "")) {
$this->url = $url;
}
}
/** Performs initialization of the instance */
protected function init(FeedStruct $feed): FeedStruct {
$type = MimeType::parse($this->contentType) ?? "";
@ -92,26 +85,6 @@ class Feed extends Construct implements \MensBeam\Lax\Parser\Feed {
return $feed;
}
/** Parses the feed to extract data */
public function parse(FeedStruct $feed = null): FeedStruct {
$feed = $this->init($feed ?? new FeedStruct);
$feed->meta->url = strlen($this->url ?? "") ? new Url($this->url) : null;
$feed->sched = $this->getSchedule();
$feed->id = $this->getId();
$feed->lang = $this->getLang();
$feed->url = $this->getUrl();
$feed->link = $this->getLink();
$feed->title = $this->getTitle();
$feed->summary = $this->getSummary();
$feed->dateModified = $this->getDateModified();
$feed->icon = $this->getIcon();
$feed->image = $this->getImage();
$feed->people = $this->getPeople();
$feed->categories = $this->getCategories();
$feed->entries = $this->getEntries($feed);
return $feed;
}
public function getId(): ?string {
return $this->getIdAtom() // Atom ID
?? $this->getIdDC() // Dublin Core ID

Loading…
Cancel
Save