|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Camillebaronnet\ETL\Extractor\Extension; |
| 4 | + |
| 5 | +use Camillebaronnet\ETL\Exception\BadInterface; |
| 6 | +use Camillebaronnet\ETL\Exception\DecoderNotFound; |
| 7 | +use Symfony\Component\Serializer\Encoder\CsvEncoder; |
| 8 | +use Symfony\Component\Serializer\Encoder\DecoderInterface; |
| 9 | +use Symfony\Component\Serializer\Encoder\JsonEncoder; |
| 10 | +use Symfony\Component\Serializer\Encoder\XmlEncoder; |
| 11 | +use Symfony\Component\Serializer\Encoder\YamlEncoder; |
| 12 | + |
| 13 | +trait SupportDecoders |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Content-Type decoders matches. |
| 17 | + */ |
| 18 | + protected static $TYPE_MIME_DECODERS = [ |
| 19 | + 'application/json' => JsonEncoder::class, |
| 20 | + 'application/xml' => XmlEncoder::class, |
| 21 | + 'text/yaml' => YamlEncoder::class, |
| 22 | + 'text/x-yaml' => YamlEncoder::class, |
| 23 | + 'application/yaml' => YamlEncoder::class, |
| 24 | + 'text/vnd.yaml' => YamlEncoder::class, |
| 25 | + 'application/x-yaml' => YamlEncoder::class, |
| 26 | + 'text/csv' => CsvEncoder::class, |
| 27 | + ]; |
| 28 | + |
| 29 | + /** |
| 30 | + * Content-Type extension decoders matches. |
| 31 | + */ |
| 32 | + protected static $EXTENSIONS_DECODERS = [ |
| 33 | + 'json' => JsonEncoder::class, |
| 34 | + 'xml' => XmlEncoder::class, |
| 35 | + 'yaml' => YamlEncoder::class, |
| 36 | + 'yml' => YamlEncoder::class, |
| 37 | + 'csv' => CsvEncoder::class, |
| 38 | + ]; |
| 39 | + |
| 40 | + /** |
| 41 | + * Extends the support of more type mime. |
| 42 | + * |
| 43 | + * @param $typeMime |
| 44 | + * @param $decoderClass |
| 45 | + * @throws BadInterface |
| 46 | + */ |
| 47 | + public static function setTypeMimeDecoder($typeMime, $decoderClass): void |
| 48 | + { |
| 49 | + if (!new $decoderClass instanceof DecoderInterface) { |
| 50 | + throw new BadInterface(sprintf('%s must be an instance of DecoderInterface.', $decoderClass)); |
| 51 | + } |
| 52 | + |
| 53 | + static::$TYPE_MIME_DECODERS[$typeMime] = $decoderClass; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Extends the support for more extensions. |
| 58 | + * |
| 59 | + * @param $extension |
| 60 | + * @param $decoderClass |
| 61 | + * @throws BadInterface |
| 62 | + */ |
| 63 | + public static function setExtensionDecoder($extension, $decoderClass): void |
| 64 | + { |
| 65 | + if (!new $decoderClass instanceof DecoderInterface) { |
| 66 | + throw new BadInterface(sprintf('%s must be an instance of DecoderInterface.', $decoderClass)); |
| 67 | + } |
| 68 | + |
| 69 | + static::$EXTENSIONS_DECODERS[$extension] = $decoderClass; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @param $contentType |
| 74 | + * @return null |
| 75 | + */ |
| 76 | + protected function findDecoder($contentType) |
| 77 | + { |
| 78 | + preg_match('/^[^;]+/', $contentType, $matches); |
| 79 | + $contentType = trim($matches[0] ?? null); |
| 80 | + |
| 81 | + if (isset(static::$TYPE_MIME_DECODERS[$contentType])) { |
| 82 | + return static::$TYPE_MIME_DECODERS[$contentType]; |
| 83 | + } |
| 84 | + |
| 85 | + preg_match('/\+([^+]+)$/', $contentType, $matches); |
| 86 | + $contentTypeExtension = $matches[1] ?? null; |
| 87 | + |
| 88 | + return static::$EXTENSIONS_DECODERS[$contentTypeExtension] ?? null; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param $body |
| 93 | + * @param $typeMime |
| 94 | + * @param array $context |
| 95 | + * @return mixed |
| 96 | + * @throws BadInterface |
| 97 | + * @throws DecoderNotFound |
| 98 | + */ |
| 99 | + protected function decode($body, $typeMime, $context = []) |
| 100 | + { |
| 101 | + $contextDecoder = $context['decoder'] ?? []; |
| 102 | + $decoderClass = $contextDecoder['class'] ?? $this->findDecoder($typeMime); |
| 103 | + |
| 104 | + if (null === $decoderClass) { |
| 105 | + throw new DecoderNotFound; |
| 106 | + } |
| 107 | + |
| 108 | + $decoder = new $decoderClass; |
| 109 | + |
| 110 | + if (!$decoder instanceof DecoderInterface) { |
| 111 | + throw new BadInterface(sprintf('Bad interface. %s must be an instance of DecoderInterface.', |
| 112 | + $decoderClass)); |
| 113 | + } |
| 114 | + |
| 115 | + $format = defined($decoderClass.'::FORMAT') ? $decoderClass::FORMAT : null; |
| 116 | + |
| 117 | + return $decoder->decode($body, $format, $context); |
| 118 | + } |
| 119 | +} |
0 commit comments