diff --git a/README.md b/README.md index ff0a81b..718423d 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,8 @@ To access the podcast episodes, call `getEpisodes` on the `Poddle` object: $episodes = $poddle->getEpisodes(); ``` +By default, `getEpisodes` will throw an error if any of the episodes is malformed. If you want a more forgiving behavior, pass `true` into the call to silently ignore the invalid episodes. + This method returns a [lazy collection](https://laravel.com/docs/11.x/collections#lazy-collections) of `\PhanAn\Poddle\Values\Episode` objects. You can iterate over the collection to access each episode: ```php diff --git a/src/Poddle.php b/src/Poddle.php index 4d8242d..7ebd73d 100644 --- a/src/Poddle.php +++ b/src/Poddle.php @@ -59,11 +59,19 @@ public function getChannel(): Channel ); } - public function getEpisodes(): EpisodeCollection + public function getEpisodes(bool $ignoreInvalids = false): EpisodeCollection { - return new EpisodeCollection(function (): Generator { + return new EpisodeCollection(function () use ($ignoreInvalids): Generator { foreach ($this->xmlReader->element('rss.channel.item')->collectLazy() as $item) { - yield Episode::fromXmlElement($item); + try { + yield Episode::fromXmlElement($item); + } catch (Throwable $e) { + if ($ignoreInvalids) { + continue; + } + + throw $e; + } } }); }