-
Notifications
You must be signed in to change notification settings - Fork 4
/
Decode.php
154 lines (131 loc) · 4.47 KB
/
Decode.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
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
145
146
147
148
149
150
151
152
153
154
<?php
namespace Camillebaronnet\ETL\Transformer;
use Camillebaronnet\ETL\Exception\BadInterface;
use Camillebaronnet\ETL\Exception\DecoderNotFound;
use Camillebaronnet\ETL\TransformInterface;
use Generator;
use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\YamlEncoder;
/**
* The Decoder transformer uses the Symfony's DecoderInterface classes to decode data.
*/
class Decode implements TransformInterface
{
/**
* Define which fields must be decoded.
* @var array
*/
public $decodeFields = ['body'];
/**
* Define which field contains the contentType information on the data set.
* @var string
*/
public $contentTypeField = 'contentType';
/**
* Force a DecoderInterface class instead of a smart decoding using the $contentTypeField.
* @see DecoderInterface
* @var
*/
public $forceDecoder;
/**
* This is the context raw passed to the Symfony's DecoderInterface implementation.
* @see DecoderInterface::decode()
* @var array
*/
public $context = [];
/**
* Content-Type decoders matches.
*/
protected static $TYPE_MIME_DECODERS = [
'application/json' => JsonEncoder::class,
'application/xml' => XmlEncoder::class,
'text/yaml' => YamlEncoder::class,
'text/x-yaml' => YamlEncoder::class,
'application/yaml' => YamlEncoder::class,
'text/vnd.yaml' => YamlEncoder::class,
'application/x-yaml' => YamlEncoder::class,
'text/csv' => CsvEncoder::class,
];
/**
* Content-Type extension decoders matches.
*/
protected static $EXTENSIONS_DECODERS = [
'json' => JsonEncoder::class,
'xml' => XmlEncoder::class,
'yaml' => YamlEncoder::class,
'yml' => YamlEncoder::class,
'csv' => CsvEncoder::class,
];
/**
* @throws BadInterface
*/
public static function setTypeMimeDecoder(string $typeMime, string $decoderClass): void
{
if (!new $decoderClass instanceof DecoderInterface) {
throw new BadInterface(sprintf('%s must be an instance of DecoderInterface.', $decoderClass));
}
static::$TYPE_MIME_DECODERS[$typeMime] = $decoderClass;
}
/**
* @throws BadInterface
*/
public static function setExtensionDecoder(string $extension, string $decoderClass): void
{
if (!new $decoderClass instanceof DecoderInterface) {
throw new BadInterface(sprintf('%s must be an instance of DecoderInterface.', $decoderClass));
}
static::$EXTENSIONS_DECODERS[$extension] = $decoderClass;
}
/**
* @return Generator
* @throws BadInterface
* @throws DecoderNotFound
*/
public function __invoke(iterable $line): iterable
{
foreach($this->decodeFields as $field){
$decoderClass = $this->forceDecoder ?? $this->findDecoder($line[$this->contentTypeField]);
$line[$field] = $this->decode($line[$field], $decoderClass);
foreach($line[$field] as $subsetItem){
yield $subsetItem;
}
}
}
/**
* @param mixed $data
* @throws BadInterface
* @throws DecoderNotFound
*/
public function decode($data, ?string $decoderClass): iterable
{
if (null === $decoderClass) {
throw new DecoderNotFound;
}
$decoder = new $decoderClass;
if (!$decoder instanceof DecoderInterface) {
throw new BadInterface(
sprintf('Bad interface. %s must be an instance of DecoderInterface.', $decoderClass)
);
}
$format = defined($decoderClass . '::FORMAT') ? $decoderClass::FORMAT : null;
return $decoder->decode($data, $format, $this->context);
}
/**
* @param $contentType
* @return null
*/
private function findDecoder($contentType)
{
preg_match('/^[^;]+/', $contentType, $matches);
$contentType = trim($matches[0] ?? null);
if (isset(static::$TYPE_MIME_DECODERS[$contentType])) {
return static::$TYPE_MIME_DECODERS[$contentType];
}
preg_match('/\+([^+]+)$/', $contentType, $matches);
$contentTypeExtension = $matches[1] ?? null;
return static::$EXTENSIONS_DECODERS[$contentTypeExtension] ?? null;
}
}