Skip to content

Commit

Permalink
feat: make getEpisodes optionally forgiving
Browse files Browse the repository at this point in the history
  • Loading branch information
phanan committed May 30, 2024
1 parent 8c14629 commit 9c0e629
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions src/Poddle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
});
}
Expand Down

0 comments on commit 9c0e629

Please sign in to comment.