|
@ -7,9 +7,11 @@ declare(strict_types=1); |
|
|
namespace MensBeam\Lax\Parser; |
|
|
namespace MensBeam\Lax\Parser; |
|
|
|
|
|
|
|
|
use MensBeam\Lax\Feed; |
|
|
use MensBeam\Lax\Feed; |
|
|
|
|
|
use MensBeam\Lax\Metadata; |
|
|
use MensBeam\Lax\MimeType; |
|
|
use MensBeam\Lax\MimeType; |
|
|
use MensBeam\Lax\Parser\JSON\Feed as JSONParser; |
|
|
use MensBeam\Lax\Parser\JSON\Feed as JSONParser; |
|
|
use MensBeam\Lax\Parser\XML\Feed as XMLParser; |
|
|
use MensBeam\Lax\Parser\XML\Feed as XMLParser; |
|
|
|
|
|
use Psr\Http\Message\MessageInterface; |
|
|
|
|
|
|
|
|
abstract class Parser { |
|
|
abstract class Parser { |
|
|
public static function findParserForType(string $type): ?string { |
|
|
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 $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 |
|
|
* @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); |
|
|
$type = $contentType ?? $feed->meta->type ?? self::findTypeForContent($data); |
|
|
$url = $url ?? $feed->meta->url; |
|
|
$url = $url ?? $feed->meta->url; |
|
|
$class = self::findParserForType($type); |
|
|
$class = self::findParserForType($type); |
|
|
$parser = new $class($data, (string) $contentType, (string) $url); |
|
|
$parser = new $class($data, (string) $contentType, (string) $url); |
|
|
return $parser->parse($feed); |
|
|
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; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|