Browse Source

Make message parser not depend on feed class

master
J. King 4 years ago
parent
commit
d1e0cdfdc5
  1. 13
      lib/Feed.php
  2. 26
      lib/Parser/HTTP/Message.php
  3. 16
      lib/Parser/Parser.php
  4. 2
      tests/cases/HTTP/HTTPTest.php

13
lib/Feed.php

@ -84,10 +84,19 @@ class Feed {
* @param string|null $url The URL used to retrieve the newsfeed, if applicable
*/
public static function parse(string $data, ?string $contentType = null, ?string $url = null): self {
return Parser::parseIntoFeed(new self, $data, $contentType, $url);
return Parser::parseDataIntoFeed(new self, $data, $contentType, $url);
}
/** Parses a PSR-7 HTTP message to produce a Feed object
*
* Most users will probably rather want the Feed::fetch() method
*
* @param \Psr\Http\Message\MessageInterface $msg The message to parse
* @param string|null $url The URL used to retrieve the newsfeed, if applicable
*/
public static function parseMessage(\Psr\Http\Message\MessageInterface $msg, ?string $url): self {
return new self;
$feed = new self;
Parser::parseMessageIntoMeta($feed->meta, $msg, $url);
return Parser::parseDataIntoFeed($feed, $msg->getBody()->getContents());
}
}

26
lib/Parser/HTTP/Message.php

@ -9,7 +9,7 @@ namespace MensBeam\Lax\Parser\HTTP;
use MensBeam\Lax\Date;
use MensBeam\Lax\MimeType;
use MensBeam\Lax\Url;
use MensBeam\Lax\Feed as FeedStruct;
use MensBeam\Lax\Metadata as MetaStruct;
use MensBeam\Lax\Link\Link;
use MensBeam\Lax\Link\Collection as LinkCollection;
use Psr\Http\Message\MessageInterface;
@ -41,18 +41,18 @@ class Message {
}
}
public function parse(FeedStruct $feed = null): FeedStruct {
$feed = $feed ?? new FeedStruct;
$feed->meta->url = strlen($this->url ?? "") ? new Url($this->url) : null;
$feed->meta->type = $this->getContentType();
$feed->meta->date = $this->getDate();
$feed->meta->expires = $this->getExpires();
$feed->meta->lastModified = $this->getLastModified();
$feed->meta->etag = $this->getEtag();
$feed->meta->age = $this->getAge();
$feed->meta->maxAge = $this->getMaxAge();
$feed->meta->links = $this->getLinks();
return $feed;
public function parse(MetaStruct $meta = null): MetaStruct {
$meta = $meta ?? new MetaStruct;
$meta->url = strlen($this->url ?? "") ? new Url($this->url) : null;
$meta->type = $this->getContentType();
$meta->date = $this->getDate();
$meta->expires = $this->getExpires();
$meta->lastModified = $this->getLastModified();
$meta->etag = $this->getEtag();
$meta->age = $this->getAge();
$meta->maxAge = $this->getMaxAge();
$meta->links = $this->getLinks();
return $meta;
}
protected function parseHeader(string $name, string $pattern, bool $multi = false): ?array {

16
lib/Parser/Parser.php

@ -7,9 +7,11 @@ declare(strict_types=1);
namespace MensBeam\Lax\Parser;
use MensBeam\Lax\Feed;
use MensBeam\Lax\Metadata;
use MensBeam\Lax\MimeType;
use MensBeam\Lax\Parser\JSON\Feed as JSONParser;
use MensBeam\Lax\Parser\XML\Feed as XMLParser;
use Psr\Http\Message\MessageInterface;
abstract class Parser {
public static function findParserForType(string $type): ?string {
@ -61,11 +63,23 @@ abstract class Parser {
* @param string|null $contentType The HTTP Content-Type of the document; it is considered authoritative if supplied
* @param string|null $url The URL used to retrieve the newsfeed, if applicable
*/
public static function parseIntoFeed(Feed $feed, string $data, ?string $contentType = null, ?string $url = null): Feed {
public static function parseDataIntoFeed(Feed $feed, string $data, ?string $contentType = null, ?string $url = null): Feed {
$type = $contentType ?? $feed->meta->type ?? self::findTypeForContent($data);
$url = $url ?? $feed->meta->url;
$class = self::findParserForType($type);
$parser = new $class($data, (string) $contentType, (string) $url);
return $parser->parse($feed);
}
/** Parses an HTTP message's metadata into a metadata object
*
* @param \MensBeam\Lax\Feed $feed The newfeed object to populate
* @param string $data The HTTP message to parse
* @param string|null $url The URL used to retrieve the newsfeed, if applicable
*/
public static function parseMessageIntoMeta(Metadata $meta, MessageInterface $data, ?string $url = null): Metadata {
$parser = new HTTP\Message($data, $url);
$parser->parse($meta);
return $meta;
}
}

2
tests/cases/HTTP/HTTPTest.php

@ -18,7 +18,7 @@ class HTTPTest extends \MensBeam\Lax\TestCase\AbstractParserTestCase {
$p->parse();
} else {
$act = $p->parse();
$this->assertEquals($exp, $act);
$this->assertEquals($exp->meta, $act);
}
}

Loading…
Cancel
Save