WIP on document conversion
This commit is contained in:
53
src/lib/ItemAndFeed.php
Normal file
53
src/lib/ItemAndFeed.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace FeedReaderCentral;
|
||||
|
||||
use BitBadger\Documents\JsonMapper;
|
||||
use BitBadger\Documents\Mapper;
|
||||
use FeedReaderCentral\Domain\Feed;
|
||||
use FeedReaderCentral\Domain\Item;
|
||||
use FeedReaderCentral\Domain\Table;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user