1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace GeodisBundle\Domain\Base; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Author: Nils méchin <[email protected]> |
8
|
|
|
* Author: Maxime Lambot <[email protected]>. |
9
|
|
|
*/ |
10
|
|
|
abstract class Model |
11
|
|
|
{ |
12
|
|
|
public function toJson(): bool|string |
13
|
|
|
{ |
14
|
|
|
$json = array(); |
15
|
|
|
foreach ($this as $key => $value) { |
16
|
|
|
if ('url' === $key || 'primaryKey' === $key) { |
17
|
|
|
continue; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
if (null === $value) { |
21
|
|
|
continue; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
// If an array of entity is passed to the json, it squizzed it, |
25
|
|
|
// rebuild an proper array without being entity and pass it to the $json array |
26
|
|
|
if ('listEnvois' == $key) { |
27
|
|
|
$value = $this->encodeLines($value); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$json[$key] = $value; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return json_encode($json); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function encodeLines($value): array |
37
|
|
|
{ |
38
|
|
|
$salesOrderLine = []; |
39
|
|
|
|
40
|
|
|
foreach ($value as $entryKey => $entry) { |
41
|
|
|
if ('url' === $entryKey || 'primaryKey' === $entryKey) { |
42
|
|
|
continue; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (null === $entry) { |
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (in_array($entryKey, ['destinataire', 'expediteur'], true)) { |
50
|
|
|
$entry = $this->encodeSubLines($entry); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Liste d’objets (important pour Geodis !) |
54
|
|
|
if ($entryKey === 'listUmgs') { |
55
|
|
|
$entry = $this->encodeMultipleSubLines($entry); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$salesOrderLine[$entryKey] = $entry; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return [$salesOrderLine]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function encodeSubLines($entry): array |
65
|
|
|
{ |
66
|
|
|
$salesOrderLine = array(); |
67
|
|
|
foreach ($entry as $entryKey => $value) { |
68
|
|
|
if ('url' === $entryKey or 'primaryKey' === $entryKey) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
if (null === $value) { |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
$salesOrderLine[$entryKey] = $value; |
75
|
|
|
} |
76
|
|
|
return $salesOrderLine; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function encodeMultipleSubLines($entries): array |
80
|
|
|
{ |
81
|
|
|
$lines = []; |
82
|
|
|
|
83
|
|
|
// Si c’est un seul élément (non tableau), on le met dans un tableau |
84
|
|
|
if (!is_array($entries) || array_keys($entries) === range(0, count($entries) - 1)) { |
85
|
|
|
// c’est déjà un tableau d’objets, on ne change rien |
86
|
|
|
} else { |
87
|
|
|
// c’est un seul objet, on le met dans un tableau |
88
|
|
|
$entries = [$entries]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
foreach ($entries as $entry) { |
92
|
|
|
$cleaned = []; |
93
|
|
|
|
94
|
|
|
foreach ($entry as $key => $value) { |
95
|
|
|
if ($value === null || $key === 'url' || $key === 'primaryKey') { |
96
|
|
|
continue; |
97
|
|
|
} |
98
|
|
|
$cleaned[$key] = $value; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$lines[] = $cleaned; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $lines; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|