diff --git a/classes/RssFetcher.php b/classes/RssFetcher.php index f62e804..5b39b7b 100644 --- a/classes/RssFetcher.php +++ b/classes/RssFetcher.php @@ -8,7 +8,9 @@ use Exception; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Support\Collection; +use Laminas\Feed\Reader\Entry\EntryInterface; use Laminas\Feed\Reader\Entry\Rss; +use Laminas\Feed\Reader\Feed\FeedInterface; use Laminas\Feed\Reader\Reader; use October\Rain\Support\Traits\Singleton; use Psr\Log\LoggerInterface; @@ -92,7 +94,8 @@ private function fetchSource(Source $source): void 'category' => implode(', ', $item->getCategories()->getValues()), 'comments' => $item->getCommentLink(), 'pub_date' => $dateCreated !== null ? $item->getDateCreated()->format('Y-m-d H:i:s') : null, - 'is_published' => (bool) $source->getAttribute('publish_new_items') + 'is_published' => (bool) $source->getAttribute('publish_new_items'), + 'author' => $this->getAuthor($channel, $item), ]; $enclosure = $item->getEnclosure(); @@ -103,10 +106,6 @@ private function fetchSource(Source $source): void $attributes['enclosure_type'] = $enclosure->type ?? null; } - if ($item->getAuthors() !== null && is_array($item->getAuthors())) { - $attributes['author'] = implode(', ', $item->getAuthors()); - } - try { Item::query()->updateOrCreate( [ @@ -128,6 +127,29 @@ private function fetchSource(Source $source): void $source->save(); } + /** + * @param FeedInterface $feed + * @param EntryInterface $entry + * @return string|null + */ + private function getAuthor(FeedInterface $feed, EntryInterface $entry): ?string + { + $result = null; + $author = $entry->getAuthor(); + + if ($author === null || empty($author)) { + $author = $feed->getAuthor(); + } + + if (is_array($author)) { + $result = $author['name'] ?? null; + } elseif (is_string($author)) { + $result = $author; + } + + return $result; + } + /** * @param int|null $sourceId * @return Collection diff --git a/updates/version.yaml b/updates/version.yaml index 5f7ea06..b824d3d 100644 --- a/updates/version.yaml +++ b/updates/version.yaml @@ -17,3 +17,4 @@ - 20200206_0006_remove_unique_constraint_from_items.php 2.0.0: - "Replace Zend Framework with Laminas and added events" +2.1.0: "Fix Author parsing for Atom/RSS feeds"