summaryrefslogtreecommitdiff
path: root/src/Model/Card.php
blob: abaa06ec1a6850b98a8acf393ce57f31401f530a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?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\ManyToMany;
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;

  #[OneToMany(targetEntity: Artwork::class, mappedBy: 'card')]
  /**
   * @OneToMany(targetEntity="Artwork", mappedBy="card")
   */
  public Collection|ArrayCollection|PersistentCollection $artworks;

  #[OneToMany(targetEntity: VoteCard::class, mappedBy: 'card')]
  /**
   * @OneToMany(targetEntity="VoteCard", mappedBy="card")
   */
  public Collection|ArrayCollection|PersistentCollection $votes;

  #[ManyToMany(targetEntity: Category::class, inversedBy: 'cards')]
  /**
   * @ManyToMany(targetEntity="Category", inversedBy="cards", cascade={"persist"})
   */
  public Collection|ArrayCollection|PersistentCollection $categories;

  /**
   * Card constructor.
   */
  public function __construct()
  {
    $this->meta = new ArrayCollection();
    $this->artworks = new ArrayCollection();
    $this->votes = new ArrayCollection();
    $this->categories = new ArrayCollection();
  }

  /**
   * @param CardMeta $meta
   */
  public function addMeta(CardMeta $meta)
  {
    $meta->card = $this;
    $this->meta[] = $meta;
  }

  /**
   * @param Artwork $artwork
   */
  public function addArtwork(Artwork $artwork)
  {
    $artwork->card = $this;
    $this->artworks[] = $artwork;
  }

  /**
   * @param VoteCard $vote
   */
  public function addVote(VoteCard $vote)
  {
    $vote->card = $this;
    $this->votes[] = $vote;
  }

  /**
   * @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;
  }

  /**
   * @return int
   */
  public function getVotesTotal(): int
  {
    $result = DB::$entityManager
      ->createQuery(
	'SELECT sum(v.value) as total
	FROM Elements\Model\VoteCard v
	WHERE v.card = :card'
      )
      ->setParameter('card', $this)
      ->getOneOrNullResult();

      return $result['total'] ?? 0;
  }
}