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
|
<?php
// src/entities/CESU.php
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityManager;
#[ORM\Entity]
#[ORM\Table(name: 'cesu')]
class CESU
{
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue]
private int|null $id = null;
#[ORM\ManyToOne(targetEntity: Prestation::class, cascade: ['persist'])]
#[ORM\JoinColumn(name: 'id_presta', referencedColumnName: 'id')]
private Prestation|null $presta = null;
#[ORM\Column]
private string $taches;
#[ORM\Column]
private string $duree_travail;
#[ORM\Column]
private float $salaire;
public static EntityManager $entityManager;
public function __construct(Prestation $presta = null)
{
if($presta != null)
{
$this->setPresta($presta);
}
}
// getters
public function getPresta(): Prestation
{
return $this->presta;
}
public function getAllWithWindowFields(): array
{
return [
"Tâche effectuée:" => $this->taches,
"Durée du travail:" => $this->duree_travail,
"Salaire:" => $this->salaire];
}
// setters
public function set(string $entry, string $input)
{
$input = $this->cleanSpecialChars($input); // possibilité que $input devienne une chaîne vide
switch($entry)
{
case "Tâche effectuée:":
$this->setTaches($input);
break;
case "Durée du travail:":
$this->setDureeTravail($input);
break;
case "Salaire:":
$this->setSalaire($input);
break;
}
}
private function setPresta(Prestation $input) // private?
{
$this->presta = $input;
}
public function setTaches(string $value)
{
$this->taches = $value;
return($this);
}
public function setDureeTravail(string $value)
{
$this->duree_travail = $value;
return($this);
}
public function setSalaire($value)
{
$value = str_replace(',', '.', $value);
$this->salaire = (float) $value;
return($this);
}
private function setAll(array $input) // private?
{
$this->taches = $input[0];
$this->duree_travail = $input[1];
$this->salaire = (float)$input[2];
}
// à mettre plus tard dans une classe mère
protected function cleanSpecialChars(string $data): string
{
$search = ['"'];
return str_replace($search, '', $data);
}
// à mettre plus tard dans une classe mère
public function hydrate(string $answers)
{
$answers = $this->cleanSpecialChars($answers); // possibilité que $answers devienne une chaine vide
if($answers == '')
{
echo "erreur de CESU::hydrate(), la chaine \$answers est vide.\n";
return false;
}
$data_array = explode('|', $answers); // array
$check = false;
if(count($data_array) === 4) // facture normale
{
$this->getPresta()->setModePaiement($data_array[3]);
//array_pop($data_array); // supprime la dernière case
unset($data_array[3]);
$this->setAll($data_array);
}
else
{
echo "erreur de CESU::hydrate(), le tableau \$data_array n'a pas la taille attendue.\n";
return false;
}
//self::$entityManager->persist('Prestation');
self::$entityManager->persist($this); // $Presta avec en cascade!
self::$entityManager->flush();
}
}
|