Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| StreamBucketNormalizer | |
100.00% |
16 / 16 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
| supportsNormalization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| normalize | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| supportsDenormalization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| denormalize | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | namespace Apie\Serializer\Normalizers; |
| 3 | |
| 4 | use Apie\Core\Lists\ItemHashmap; |
| 5 | use Apie\Core\Lists\ItemList; |
| 6 | use Apie\Core\ValueObjects\Utils; |
| 7 | use Apie\Serializer\Context\ApieSerializerContext; |
| 8 | use Apie\Serializer\Interfaces\DenormalizerInterface; |
| 9 | use Apie\Serializer\Interfaces\NormalizerInterface; |
| 10 | use Psr\Http\Message\UploadedFileInterface; |
| 11 | use StreamBucket; |
| 12 | |
| 13 | class StreamBucketNormalizer implements NormalizerInterface, DenormalizerInterface |
| 14 | { |
| 15 | public function supportsNormalization(mixed $object, ApieSerializerContext $apieSerializerContext): bool |
| 16 | { |
| 17 | return $object instanceof StreamBucket; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * @param StreamBucket $object |
| 22 | */ |
| 23 | public function normalize(mixed $object, ApieSerializerContext $apieSerializerContext): string|int|float|bool|null|ItemList|ItemHashmap |
| 24 | { |
| 25 | return new ItemHashmap([ |
| 26 | 'data' => $object->data, |
| 27 | 'dataLength' => $object->dataLength, |
| 28 | ]); |
| 29 | } |
| 30 | public function supportsDenormalization(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): bool |
| 31 | { |
| 32 | return $desiredType === StreamBucket::class; |
| 33 | } |
| 34 | public function denormalize(string|int|float|bool|null|ItemList|ItemHashmap|UploadedFileInterface $object, string $desiredType, ApieSerializerContext $apieSerializerContext): StreamBucket |
| 35 | { |
| 36 | if ($object instanceof ItemHashmap) { |
| 37 | $len = $object['dataLength'] ?? null; |
| 38 | $object = Utils::toString($object['data']); |
| 39 | if ($len !== null) { |
| 40 | $object = substr($object, 0, $len); |
| 41 | } |
| 42 | $stream = fopen('php://memory', 'r+'); |
| 43 | return stream_bucket_new($stream, $object); |
| 44 | } |
| 45 | $object = Utils::toString($object); |
| 46 | $stream = fopen('php://memory', 'r+'); |
| 47 | return stream_bucket_new($stream, $object); |
| 48 | } |
| 49 | |
| 50 | } |