diff --git a/lib/Parser/AbstractEntry.php b/lib/Parser/AbstractEntry.php new file mode 100644 index 0000000..b82b386 --- /dev/null +++ b/lib/Parser/AbstractEntry.php @@ -0,0 +1,36 @@ +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; + } +} \ No newline at end of file diff --git a/lib/Parser/AbstractFeed.php b/lib/Parser/AbstractFeed.php new file mode 100644 index 0000000..175474d --- /dev/null +++ b/lib/Parser/AbstractFeed.php @@ -0,0 +1,41 @@ +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; + } +} \ No newline at end of file diff --git a/lib/Parser/Feed.php b/lib/Parser/Feed.php index 0e2f80b..d2cbafb 100644 --- a/lib/Parser/Feed.php +++ b/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; diff --git a/lib/Parser/JSON/Construct.php b/lib/Parser/JSON/Construct.php index 0b86db1..fc442a6 100644 --- a/lib/Parser/JSON/Construct.php +++ b/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 diff --git a/lib/Parser/JSON/Entry.php b/lib/Parser/JSON/Entry.php index 57492c1..65c4585 100644 --- a/lib/Parser/JSON/Entry.php +++ b/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)) { diff --git a/lib/Parser/JSON/Feed.php b/lib/Parser/JSON/Feed.php index 2eae316..25a6f7a 100644 --- a/lib/Parser/JSON/Feed.php +++ b/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 diff --git a/lib/Parser/XML/Entry.php b/lib/Parser/XML/Entry.php index 83aaeb4..30cdc84 100644 --- a/lib/Parser/XML/Entry.php +++ b/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", diff --git a/lib/Parser/XML/Feed.php b/lib/Parser/XML/Feed.php index 16e4e84..be9cc9f 100644 --- a/lib/Parser/XML/Feed.php +++ b/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