Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.89% |
8 / 9 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
Base64Stream | |
88.89% |
8 / 9 |
|
80.00% |
4 / 5 |
5.03 | |
0.00% |
0 / 1 |
getRegularExpression | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
convert | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createRandom | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
decode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSchema | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | namespace Apie\Core\ValueObjects; |
3 | |
4 | use Apie\Core\Attributes\CmsSingleInput; |
5 | use Apie\Core\Attributes\Description; |
6 | use Apie\Core\Attributes\FakeMethod; |
7 | use Apie\Core\Attributes\SchemaMethod; |
8 | use Apie\Core\Dto\CmsInputOption; |
9 | use Apie\Core\Enums\FileStreamType; |
10 | use Apie\Core\RegexUtils; |
11 | use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface; |
12 | use Faker\Generator; |
13 | |
14 | #[SchemaMethod("getSchema")] |
15 | #[FakeMethod('createRandom')] |
16 | #[Description('A binary file stream, stored in base64 encoding')] |
17 | #[CmsSingleInput(['stream', 'file'], new CmsInputOption(streamType: FileStreamType::Base64String))] |
18 | final class Base64Stream implements HasRegexValueObjectInterface |
19 | { |
20 | use IsStringWithRegexValueObject; |
21 | |
22 | public static function getRegularExpression(): string |
23 | { |
24 | return '#^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$#'; |
25 | } |
26 | |
27 | protected function convert(string $input): string |
28 | { |
29 | return preg_replace('/\s/s', '', $input); |
30 | } |
31 | |
32 | public static function createRandom(Generator $generator): self |
33 | { |
34 | return new self(base64_encode($generator->text())); |
35 | } |
36 | |
37 | public function decode(): BinaryStream |
38 | { |
39 | return new BinaryStream(base64_decode($this->internal)); |
40 | } |
41 | |
42 | /** |
43 | * @return array<string, string> |
44 | */ |
45 | public static function getSchema(): array |
46 | { |
47 | return [ |
48 | 'type' => 'string', |
49 | 'format' => 'base64', |
50 | 'pattern' => RegexUtils::removeDelimiters(self::getRegularExpression()), |
51 | ]; |
52 | } |
53 | } |