From a319f3a419790925bed539ba141038c72a83e70f Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Mon, 10 Jan 2022 20:29:04 +0100 Subject: Initial commit --- src/Model/Card.php | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Model/CardMeta.php | 57 +++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 src/Model/Card.php create mode 100644 src/Model/CardMeta.php (limited to 'src/Model') diff --git a/src/Model/Card.php b/src/Model/Card.php new file mode 100644 index 0000000..ab421ab --- /dev/null +++ b/src/Model/Card.php @@ -0,0 +1,87 @@ +meta = new ArrayCollection(); + } + + /** + * @param CardMeta $meta + */ + public function addMeta(CardMeta $meta) + { + $meta->card = $this; + $this->meta[] = $meta; + } + + /** + * @param string $key + * + * @return string + */ + public function getMeta(string $key): ?string + { + // if meta is already hydrated + if ($this->meta->isInitialized()) { + $meta = $this->meta->unwrap() + #->findFirst(fn (CardMeta $item) => $item->key === $key); + ->filter(fn ($item) => $item->key === $key)->first(); + + return $meta->value ?? null; + } + + // get directly from db otherwise + $result = DB::$entityManager + ->createQuery( + 'SELECT cm.value + FROM Elements\Model\CardMeta cm + WHERE cm.key = :key AND cm.card = :card' + ) + ->setParameter('key', $key) + ->setParameter('card', $this) + ->getOneOrNullResult(); + + return $result['value'] ?? null; + } +} + diff --git a/src/Model/CardMeta.php b/src/Model/CardMeta.php new file mode 100644 index 0000000..2ec0da9 --- /dev/null +++ b/src/Model/CardMeta.php @@ -0,0 +1,57 @@ +key = $key; + $this->value = $value; + } +} + -- cgit v1.2.3