Move domain items up to lib

- WIP on converting more complex queries
This commit is contained in:
2024-05-31 22:57:24 -04:00
parent f7f5dba795
commit 610ab67475
16 changed files with 189 additions and 167 deletions

110
src/lib/Item.php Normal file
View File

@@ -0,0 +1,110 @@
<?php
namespace FeedReaderCentral;
use BitBadger\Documents\DocumentException;
use BitBadger\Documents\Field;
use BitBadger\Documents\JsonMapper;
use BitBadger\Documents\Query;
use BitBadger\Documents\SQLite\Configuration;
use BitBadger\Documents\SQLite\Custom;
use BitBadger\Documents\SQLite\Parameters;
/**
* An item from a feed
*/
class Item
{
/** @var int The ID of this item in the Feed Reader Central database */
public int $id = 0;
/** @var int The ID of the feed to which this item belongs */
public int $feed_id = 0;
/** @var string The title of this item */
public string $title = '';
/** @var string The Globally Unique ID (GUID) for this item (an attribute in the feed XML) */
public string $item_guid = '';
/** @var string The link to the item on its original site */
public string $item_link = '';
/** @var string The date/time this item was published */
public string $published_on = '';
/** @var string|null The date/time this item was last updated */
public ?string $updated_on = null;
/** @var string The content for this item */
public string $content = '';
/** @var int 1 if the item has been read, 0 if not */
public int $is_read = 0;
/** @var int 1 if the item is bookmarked, 0 if not */
public int $is_bookmarked = 0;
/**
* Has the item been read?
*
* @return bool True if the item has been read, false if not
*/
public function isRead(): bool
{
return $this->is_read <> 0;
}
/**
* Is the item bookmarked?
*
* @return bool True if the item is bookmarked, false if not
*/
public function isBookmarked(): bool
{
return $this->is_bookmarked <> 0;
}
/**
* Create an item document from a parsed feed item
*
* @param int $feedId The ID of the feed to which this item belongs
* @param ParsedItem $item The parsed feed item
* @return static The item document
*/
public static function fromFeedItem(int $feedId, ParsedItem $item): static
{
$it = new static();
$it->feed_id = $feedId;
$it->item_guid = $item->guid;
$it->item_link = $item->link;
$it->title = $item->title;
$it->published_on = $item->publishedOn;
$it->updated_on = $item->updatedOn;
$it->content = $item->content;
return $it;
}
/**
* Retrieve an item by its ID, ensuring that its owner matches the current user
*
* @param int $id The ID of the item to retrieve
* @return Item|false The item if it exists and is owned by the current user, false if not
* @throws DocumentException If any is encountered
*/
public static function retrieveByIdForUser(int $id): Item|false
{
$idField = Field::EQ(Configuration::idField(), $id, '@id');
$idField->qualifier = Table::ITEM;
$userField = Field::EQ('user_id', $_SESSION[Key::USER_ID], '@user');
$userField->qualifier = Table::FEED;
$fields = [$idField, $userField];
$where = Query::whereByFields($fields);
$item = Table::ITEM;
$feed = Table::FEED;
return Custom::single(
"SELECT $item.data FROM $item INNER JOIN $feed ON $item.data->>'feed_id' = $feed.data->>'id' WHERE $where",
Parameters::addFields($fields, []), new JsonMapper(Item::class));
}
}