feed-reader-central/src/lib/ItemAndFeed.php
Daniel J. Summers 610ab67475 Move domain items up to lib
- WIP on converting more complex queries
2024-05-31 22:57:24 -04:00

52 lines
1.4 KiB
PHP

<?php
namespace FeedReaderCentral;
use BitBadger\Documents\JsonMapper;
use BitBadger\Documents\Mapper;
use FeedReaderCentral\Domain\Feed;
/**
* A combined item and feed (used for lists)
*/
class ItemAndFeed
{
/** @var Item The item to be manipulated */
public Item $item;
/** @var Feed The feed to which the item belongs */
public Feed $feed;
/**
* Create a mapper for this item
* @return Mapper<ItemAndFeed> A mapper to deserialize this from the query
*/
public static function mapper(): Mapper
{
return new class implements Mapper {
public function map(array $result): ItemAndFeed
{
$it = new ItemAndFeed();
$it->item = (new JsonMapper(Item::class, 'item_data'))->map($result);
$it->feed = (new JsonMapper(Feed::class, 'feed_data'))->map($result);
return $it;
}
};
}
/**
* Generate the `SELECT` and `FROM` clauses for the query to retrieve this item
*
* @return string The `SELECT` and `FROM` clauses to retrieve these items
*/
public static function selectFrom(): string
{
$item = Table::ITEM;
$feed = Table::FEED;
return <<<SQL
SELECT $item.data AS item_data, $feed.data AS feed_data
FROM $item INNER JOIN $feed ON $item.data->>'feed_id' = $feed.data->>'id'
SQL;
}
}