summaryrefslogtreecommitdiff
path: root/src/model/entities/Location.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/model/entities/Location.php')
-rw-r--r--src/model/entities/Location.php209
1 files changed, 209 insertions, 0 deletions
diff --git a/src/model/entities/Location.php b/src/model/entities/Location.php
new file mode 100644
index 0000000..7ce6f0d
--- /dev/null
+++ b/src/model/entities/Location.php
@@ -0,0 +1,209 @@
1<?php
2// src/entities/Location.php
3
4use Doctrine\ORM\Mapping as ORM;
5use Doctrine\ORM\EntityManager;
6
7#[ORM\Entity]
8#[ORM\Table(name: 'locations')]
9class Location
10{
11 #[ORM\Id]
12 #[ORM\Column(type: 'integer')]
13 #[ORM\GeneratedValue]
14 private int|null $id = null;
15
16 #[ORM\ManyToOne(targetEntity: Prestation::class, cascade: ['persist'])]
17 #[ORM\JoinColumn(name: 'id_presta', referencedColumnName: 'id')]
18 private Prestation|null $presta = null;
19
20 #[ORM\Column]
21 protected string $designation;
22 #[ORM\Column]
23 protected string $modele_description;
24 #[ORM\Column]
25 protected float $valeur;
26 #[ORM\Column]
27 protected string $etat_des_lieux_debut;
28 #[ORM\Column]
29 protected string $etat_des_lieux_fin = '';
30 #[ORM\Column]
31 protected string $duree_location;
32 #[ORM\Column]
33 protected float $loyer_hebdo;
34 #[ORM\Column]
35 protected int $loyers_payes;
36 #[ORM\Column]
37 protected int $caution;
38
39 public static EntityManager $entityManager;
40
41 public function __construct(Prestation $presta = null)
42 {
43 if($presta != null)
44 {
45 $this->setPresta($presta);
46 }
47 }
48
49 // getters
50 public function getPresta(): Prestation
51 {
52 return $this->presta;
53 }
54 public function getAll(): array
55 {
56 // n'utiliser get_object_vars() qu'avec une entité parce qu'on maîtrise le nombre de propriétés
57 return get_object_vars($this);
58 }
59
60 public function getAllWithWindowFields(): array
61 {
62 return [
63 "Désignation:" => $this->designation,
64 "Description du modèle:" => $this->modele_description,
65 "Valeur:" => $this->valeur,
66 "État des lieux de début:" => $this->etat_des_lieux_debut,
67 "État des lieux de fin:" => $this->etat_des_lieux_fin,
68 "Durée de la location:" => $this->duree_location,
69 "Loyer Hebdomadaire:" => $this->loyer_hebdo,
70 "Loyers Payés:" => $this->loyers_payes,
71 "Caution:" => $this->caution];
72 }
73
74 // setters
75 public function set(string $entry, string $input)
76 {
77 $input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaîne vide
78 switch($entry)
79 {
80 case "Désignation:":
81 $this->setDesignation($input);
82 break;
83 case "Description du modèle:":
84 $this->setModeleDescription($input);
85 break;
86 case "Valeur:":
87 $this->setValeur($input);
88 break;
89 case "État des lieux de début:":
90 $this->setEtatDesLieuxDebut($input);
91 break;
92 case "État des lieux de fin:":
93 $this->setEtatDesLieuxFin($input);
94 break;
95 case "Durée de la location:":
96 $this->setDureeLocation($input);
97 break;
98 case "Loyer Hebdomadaire:":
99 $this->setLoyerHebdo($input);
100 break;
101 case "Loyers Payés:":
102 $this->setLoyersPayes($input);
103 break;
104 case "Caution:":
105 $this->setCaution($input);
106 break;
107 }
108 }
109 private function setPresta(Prestation $input) // private?
110 {
111 $this->presta = $input;
112 }
113 public function setDesignation(string $value)
114 {
115 $this->designation = $value;
116 return($this);
117 }
118 public function setModeleDescription(string $value)
119 {
120 $this->modele_description = $value;
121 return($this);
122 }
123 public function setValeur($value)
124 {
125 $value = str_replace(',', '.', $value);
126 $this->valeur = (float) $value;
127 return($this);
128 }
129 public function setEtatDesLieuxDebut(string $value)
130 {
131 $this->etat_des_lieux_debut = $value;
132 return($this);
133 }
134 public function setEtatDesLieuxFin(string $value)
135 {
136 $this->etat_des_lieux_fin = $value;
137 return($this);
138 }
139 public function setDureeLocation(string $value)
140 {
141 $this->duree_location = $value;
142 return($this);
143 }
144 public function setLoyerMensuel($value)
145 {
146 $value = str_replace(',', '.', $value);
147 $this->loyer_mensuel = (float) $value;
148 return($this);
149 }
150 public function setLoyersPayes($value)
151 {
152 $value = str_replace(',', '.', $value);
153 $this->loyers_payes = (float) $value;
154 return($this);
155 }
156 public function setCaution($value)
157 {
158 $value = str_replace(',', '.', $value);
159 $this->caution = (float) $value;
160 return($this);
161 }
162
163 private function setAll(array $input) // private?
164 {
165 $this->designation = $input[0];
166 $this->modele_description = $input[1];
167 $this->valeur = (float)$input[2];
168 $this->etat_des_lieux_debut = $input[3];
169 $this->etat_des_lieux_fin = ''; // sécurité
170 $this->duree_location = $input[4];
171 $this->loyer_hebdo = (float)$input[5];
172 $this->loyers_payes = (int)$input[6];
173 $this->caution = (int)$input[7];
174 }
175
176 // à mettre plus tard dans une classe mère
177 protected function cleanSpecialChars(string $data): string
178 {
179 $search = ['"'];
180 return str_replace($search, '', $data);
181 }
182 // à mettre plus tard dans une classe mère
183 public function hydrate(string $answers)
184 {
185 $answers = $this->cleanSpecialChars($answers); // possibilité que $answers devienne une chaine vide
186 if($answers == '')
187 {
188 echo "erreur de Location::hydrate(), la chaine \$answers est vide.\n";
189 return false;
190 }
191 $data_array = explode('|', $answers); // array
192
193 $check = false;
194 if(count($data_array) === 8)
195 {
196 $this->getPresta()->setModePaiement(''); // sécurité, doctrine plante si "null"
197 $this->setAll($data_array);
198 }
199 else
200 {
201 echo "erreur de Location::hydrate(), le tableau \$data_array n'a pas la taille attendue.\n";
202 return false;
203 }
204
205 //self::$entityManager->persist('Prestation');
206 self::$entityManager->persist($this); // $Presta avec en cascade!
207 self::$entityManager->flush();
208 }
209}