-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriverJSON.php
37 lines (29 loc) · 1.12 KB
/
driverJSON.php
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
<?php
// Classe du nouveau driver pour la sauvegarde au format JSON
/**Créer un nouveau driver pour sauvegarder les données de l'objet PrixEssence dans un fichier
* au format JSON sans modifier la clase PrixEssenceRepository. Le chemin du fichier à écrire
* devra pouvoir être paramétrable.
* Tips : 1/ utiliser json_encode()
* 2/ utiliser file_put_contents()
*/
class JsonPrixEssenceSave {
private $filePath;
public function __construct(
$filePath
) {
$this->filePath = $filePath;
}
public function doSave(PrixEssence $prixEssence) {
$data = json_encode([
'nom' => $prixEssence->carburant_nom,
'prix' => $prixEssence->prix_nom
/*.......*/
]);
file_put_contents($this->filePath, $data);
}
}
$prixEssence = new PrixEssence('Essence 95', 1.50);
$filePath = '/file_essence.json';
$jsonDriver = new JsonPrixEssenceSave($filePath);
$jsonDriver->doSave($prixEssence);
?>