summaryrefslogtreecommitdiff
path: root/src/Model
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2022-01-10 20:29:04 +0100
committerDaniel Weipert <code@drogueronin.de>2022-01-10 20:29:04 +0100
commita319f3a419790925bed539ba141038c72a83e70f (patch)
treedfb24183cc693863df733e190dbbc6da93bb82e5 /src/Model
Initial commit
Diffstat (limited to 'src/Model')
-rw-r--r--src/Model/Card.php87
-rw-r--r--src/Model/CardMeta.php57
2 files changed, 144 insertions, 0 deletions
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 @@
+<?php
+
+namespace Elements\Model;
+
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\Common\Collections\Collection;
+use Doctrine\DBAL\Schema\Column;
+use Doctrine\DBAL\Schema\Table;
+use Doctrine\ORM\Mapping\Entity;
+use Doctrine\ORM\Mapping\GeneratedValue;
+use Doctrine\ORM\Mapping\Id;
+use Doctrine\ORM\Mapping\OneToMany;
+use Doctrine\ORM\PersistentCollection;
+use Elements\DB;
+
+#[Entity]
+#[Table(name: 'cards')]
+/**
+ * @Entity
+ * @Table(name="cards")
+ */
+class Card
+{
+ #[Id]
+ #[Column(type: 'integer')]
+ #[GeneratedValue]
+ /**
+ * @Id
+ * @Column(type="integer")
+ * @GeneratedValue
+ */
+ public int $id;
+
+ #[OneToMany(targetEntity: CardMeta::class, mappedBy: 'card')]
+ /**
+ * @OneToMany(targetEntity="CardMeta", mappedBy="card")
+ */
+ public Collection|ArrayCollection|PersistentCollection $meta;
+
+ /**
+ * Card constructor.
+ */
+ public function __construct()
+ {
+ $this->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 @@
+<?php
+
+namespace Elements\Model;
+
+use Doctrine\DBAL\Schema\Column;
+use Doctrine\DBAL\Schema\Table;
+use Doctrine\ORM\Mapping\Entity;
+use Doctrine\ORM\Mapping\GeneratedValue;
+use Doctrine\ORM\Mapping\Id;
+use Doctrine\ORM\Mapping\ManyToOne;
+
+#[Entity]
+#[Table(name: 'card_meta')]
+/**
+ * @Entity
+ * @Table(name="card_meta")
+ */
+class CardMeta
+{
+ #[Id]
+ #[Column(type: 'integer')]
+ #[GeneratedValue]
+ /**
+ * @Id
+ * @Column(type="integer")
+ * @GeneratedValue
+ */
+ public int $id;
+
+ #[Column(type: 'string')]
+ /**
+ * @Column(type="string")
+ */
+ public string $key;
+
+ #[Column(type: 'string')]
+ /**
+ * @Column(type="string")
+ */
+ public string $value;
+
+ #[ManyToOne(targetEntity: Card::class, inversedBy: 'meta')]
+ /**
+ * @ManyToOne(targetEntity="Card", inversedBy="meta", cascade={"persist"})
+ */
+ public Card $card;
+
+ /**
+ * CardMeta constructor.
+ */
+ public function __construct(string $key, string $value)
+ {
+ $this->key = $key;
+ $this->value = $value;
+ }
+}
+