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