From a1484dca298579fdeacec5c1daaffad7ab477eb6 Mon Sep 17 00:00:00 2001 From: "Abdulmhsen B. A. A" Date: Tue, 17 May 2022 21:22:10 +0300 Subject: [PATCH] Added new method to get database records using StateInterface parameters. --- src/Libs/Storage/PDO/PDOAdapter.php | 35 +++++++++++++++++++-------- src/Libs/Storage/StorageInterface.php | 9 +++++++ 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/Libs/Storage/PDO/PDOAdapter.php b/src/Libs/Storage/PDO/PDOAdapter.php index 3eab0b93..1e631c45 100644 --- a/src/Libs/Storage/PDO/PDOAdapter.php +++ b/src/Libs/Storage/PDO/PDOAdapter.php @@ -76,6 +76,16 @@ final class PDOAdapter implements StorageInterface public function get(StateInterface $entity): StateInterface|null { + if (null !== $entity->id) { + $stmt = $this->pdo->query("SELECT * FROM state WHERE id = " . (int)$entity->id); + + $item = $stmt->fetch(PDO::FETCH_ASSOC); + + if (true === is_array($item)) { + return $entity::fromArray($item); + } + } + if ($entity->isEpisode() && $entity->hasRelativeGuid() && null !== ($item = $this->findByRGuid($entity))) { return $item; } @@ -108,6 +118,21 @@ final class PDOAdapter implements StorageInterface return $arr; } + public function find(StateInterface ...$items): array + { + $list = []; + + foreach ($items as $item) { + if (null === ($entity = $this->get($item))) { + continue; + } + + $list[$entity->id] = $entity; + } + + return $list; + } + public function update(StateInterface $entity): StateInterface { try { @@ -410,16 +435,6 @@ final class PDOAdapter implements StorageInterface */ private function findByGuid(StateInterface $entity): StateInterface|null { - if (null !== $entity->id) { - $stmt = $this->pdo->query("SELECT * FROM state WHERE id = " . (int)$entity->id); - - if (false === ($row = $stmt->fetch(PDO::FETCH_ASSOC))) { - return null; - } - - return $entity::fromArray($row); - } - $guids = []; $cond = [ 'type' => $entity->type, diff --git a/src/Libs/Storage/StorageInterface.php b/src/Libs/Storage/StorageInterface.php index 7bbe2fed..7d57803a 100644 --- a/src/Libs/Storage/StorageInterface.php +++ b/src/Libs/Storage/StorageInterface.php @@ -46,6 +46,15 @@ interface StorageInterface */ public function getAll(DateTimeInterface|null $date = null, StateInterface|null $class = null): array; + /** + * Return database records for given items. + * + * @param array $items + * + * @return array + */ + public function find(StateInterface ...$items): array; + /** * Update Entity immediately. *