Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DoNotChangeFileNormalizer
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 2
8.38
0.00% covered (danger)
0.00%
0 / 1
 supportsNormalization
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
7.05
 normalize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Apie\Serializer\Normalizers;
3
4use Apie\Core\Actions\ActionResponse;
5use Apie\Core\ContextConstants;
6use Apie\Core\Enums\DoNotChangeUploadedFile;
7use Apie\Serializer\Context\ApieSerializerContext;
8use Apie\Serializer\Interfaces\NormalizerInterface;
9use Psr\Http\Message\UploadedFileInterface;
10
11/**
12 * Special normalizer related to not changing files.
13 */
14class DoNotChangeFileNormalizer implements NormalizerInterface
15{
16    public function supportsNormalization(mixed $object, ApieSerializerContext $apieSerializerContext): bool
17    {
18        if ($apieSerializerContext->getContext()->hasContext(ActionResponse::class)) {
19            return false;
20        }
21        if (!$apieSerializerContext->getContext()->hasContext(ContextConstants::EDIT_OBJECT)
22            && !$apieSerializerContext->getContext()->hasContext(ContextConstants::REPLACE_OBJECT)) {
23            return false;
24        }
25        if (!$apieSerializerContext->getContext()->hasContext(ContextConstants::DISPLAY_FORM)) {
26            return false;
27        }
28        return is_resource($object)
29            || $object instanceof UploadedFileInterface
30            || get_debug_type($object) === 'resource (closed)';
31    }
32
33    /**
34     * @param resource|UploadedFileInterface $object
35     */
36    public function normalize(mixed $object, ApieSerializerContext $apieSerializerContext): string
37    {
38        return DoNotChangeUploadedFile::DoNotChange->value;
39    }
40}