Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
JsonFileUpload
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validateState
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 toUploadedFile
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 createSchema
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\Core\ValueObjects;
3
4use Apie\Core\Attributes\Optional;
5use Apie\Core\Attributes\SchemaMethod;
6use Apie\Core\FileStorage\StoredFile;
7use Apie\SchemaGenerator\Builders\ComponentsBuilder;
8use Apie\SchemaGenerator\Enums\SchemaUsages;
9
10#[SchemaMethod('createSchema')]
11final class JsonFileUpload implements CompositeWithOwnValidation
12{
13    use CompositeValueObject;
14
15    public function __construct(
16        private Filename $originalFilename,
17        #[Optional]
18        private BinaryStream $contents,
19        #[Optional]
20        private Base64Stream $base64,
21        #[Optional]
22        private ?StrictMimeType $mime = null
23    ) {
24    }
25
26    protected function validateState(): void
27    {
28        if (isset($this->contents) xor isset($this->base64)) {
29            return;
30        }
31        throw new \LogicException(
32            isset($this->contents)
33            ? 'You should only provide contents or base64'
34            : 'I need either a "contents" or a "base64" property'
35        );
36    }
37
38    /**
39     * @template T of StoredFile
40     * @param class-string<T> $className
41     * @return T
42     */
43    public function toUploadedFile(string $className = StoredFile::class): StoredFile
44    {
45        $contents = isset($this->contents) ? $this->contents->toNative() : $this->base64->decode()->toNative();
46        return $className::createFromString(
47            $contents,
48            isset($this->mime) ? $this->mime->toNative() : null,
49            $this->originalFilename->toNative(),
50        );
51    }
52
53    /**
54     * @return array<string, mixed>
55     */
56    public static function createSchema(SchemaUsages $schemaUsage, ComponentsBuilder $componentsBuilder): array
57    {
58        return [
59            'required' => [
60                'originalFilename',
61            ],
62            'oneOf' => [
63                [
64                    'required' => ['contents', 'originalFilename'],
65                ],
66                [
67                    'required' => ['base64', 'originalFilename'],
68                ],
69            ],
70            'type' => 'object',
71            'properties' => [
72                'contents' => $schemaUsage->toSchema($componentsBuilder, BinaryStream::class),
73                'originalFilename' => $schemaUsage->toSchema($componentsBuilder, Filename::class),
74                'base64' => $schemaUsage->toSchema($componentsBuilder, Base64Stream::class),
75                'mime' => $schemaUsage->toSchema($componentsBuilder, StrictMimeType::class, nullable: true),
76            ]
77        ];
78    }
79}