From 7e0aab358c07a9999b511e0e91e6501dc6b04461 Mon Sep 17 00:00:00 2001 From: Marco Nassabain Date: Tue, 16 Mar 2021 22:03:50 +0100 Subject: =?UTF-8?q?=F0=9F=8E=A8=20Item:=20store=20categories=20as=20json?= =?UTF-8?q?=20&=20add=20helper=20fns=20+=20added=20setter/getters=20that?= =?UTF-8?q?=20work=20with=20arrays=20to=20simplify=20use=20case?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marco Nassabain --- lib/Db/Item.php | 34 +++++++++++++++++------ lib/Fetcher/FeedFetcher.php | 4 +-- lib/Migration/Version150302Date20210312231251.php | 2 +- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/Db/Item.php b/lib/Db/Item.php index 86a484315..fe2cd6ac5 100644 --- a/lib/Db/Item.php +++ b/lib/Db/Item.php @@ -64,7 +64,7 @@ class Item extends Entity implements IAPI, \JsonSerializable /** @var bool */ protected $starred = false; /** @var string|null */ - protected $categories; + protected $categoriesJson; public function __construct() { @@ -87,7 +87,7 @@ class Item extends Entity implements IAPI, \JsonSerializable $this->addType('fingerprint', 'string'); $this->addType('unread', 'boolean'); $this->addType('starred', 'boolean'); - $this->addType('categories', 'string'); + $this->addType('categoriesJson', 'string'); } /** @@ -131,7 +131,7 @@ class Item extends Entity implements IAPI, \JsonSerializable html_entity_decode(strip_tags($this->getBody())) . html_entity_decode($this->getAuthor()) . html_entity_decode($this->getTitle()) . - html_entity_decode($this->getCategories()) . + html_entity_decode($this->getCategoriesJson()) . $this->getUrl(), 'UTF-8' ) @@ -282,9 +282,17 @@ class Item extends Entity implements IAPI, \JsonSerializable /** * @return null|string */ - public function getCategories(): ?string + public function getCategoriesJson(): ?string { - return $this->categories; + return $this->categoriesJson; + } + + /** + * @return null|array + */ + public function getCategories(): ?array + { + return json_decode($this->getCategoriesJson()); } /** @@ -518,16 +526,24 @@ class Item extends Entity implements IAPI, \JsonSerializable return $this; } - public function setCategories(string $categories = null): self + public function setCategoriesJson(string $categoriesJson = null): self { - if ($this->categories !== $categories) { - $this->categories = $categories; - $this->markFieldUpdated('categories'); + if ($this->categoriesJson !== $categoriesJson) { + $this->categoriesJson = $categoriesJson; + $this->markFieldUpdated('categoriesJson'); } return $this; } + public function setCategories(array $categories = null): self + { + $categoriesJson = !empty($categories) ? json_encode($categories) : null; + $this->setCategoriesJson($categoriesJson); + + return $this; + } + public function toAPI(): array { return [ diff --git a/lib/Fetcher/FeedFetcher.php b/lib/Fetcher/FeedFetcher.php index 7840c8cfd..01cb20853 100755 --- a/lib/Fetcher/FeedFetcher.php +++ b/lib/Fetcher/FeedFetcher.php @@ -260,9 +260,7 @@ class FeedFetcher implements IFeedFetcher foreach ($parsedItem->getCategories() as $category) { $categories[] = $this->decodeTwice($category->getLabel()); } - if (count($categories) > 0) { - $item->setCategories(implode(',', $categories)); - } + $item->setCategories($categories); // Use description from feed if body is not provided (by a scraper) if ($body === null) { diff --git a/lib/Migration/Version150302Date20210312231251.php b/lib/Migration/Version150302Date20210312231251.php index 70595239e..80f058ba2 100644 --- a/lib/Migration/Version150302Date20210312231251.php +++ b/lib/Migration/Version150302Date20210312231251.php @@ -34,7 +34,7 @@ class Version150302Date20210312231251 extends SimpleMigrationStep { if ($schema->hasTable('news_items')) { $table = $schema->getTable('news_items'); - $table->addColumn('categories', 'text', [ + $table->addColumn('categories_json', 'json', [ 'notnull' => false ]); } -- cgit v1.2.3