🎨 Item: store categories as json & add helper fns
+ added setter/getters that work with arrays to simplify use case Signed-off-by: Marco Nassabain <marco.nassabain@hotmail.com>
This commit is contained in:
parent
29b55ae030
commit
7e0aab358c
|
@ -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 [
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
]);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue