Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
9 / 10 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| SerializedPhpObject | |
90.00% |
9 / 10 |
|
75.00% |
3 / 4 |
5.03 | |
0.00% |
0 / 1 |
| parse | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| createFromPhpObject | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| validate | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
| toPhpObject | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Serializer\ValueObjects; |
| 3 | |
| 4 | use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface; |
| 5 | use Apie\Core\ValueObjects\IsStringValueObject; |
| 6 | use function Opis\Closure\init; |
| 7 | use function Opis\Closure\serialize; |
| 8 | use function Opis\Closure\set_security_provider; |
| 9 | use function Opis\Closure\unserialize; |
| 10 | |
| 11 | final class SerializedPhpObject implements StringValueObjectInterface |
| 12 | { |
| 13 | use IsStringValueObject; |
| 14 | |
| 15 | /** @var array<string, mixed> */ |
| 16 | private static array $alreadyParsed = []; |
| 17 | |
| 18 | private static function parse(string $input): void |
| 19 | { |
| 20 | init(null); |
| 21 | set_security_provider(null); |
| 22 | self::$alreadyParsed[$input] = unserialize($input); |
| 23 | } |
| 24 | |
| 25 | public static function createFromPhpObject(mixed $input): self |
| 26 | { |
| 27 | $inputString = serialize($input); |
| 28 | self::$alreadyParsed[$inputString] = $input; |
| 29 | return self::fromNative($inputString); |
| 30 | } |
| 31 | |
| 32 | public static function validate(string $input): void |
| 33 | { |
| 34 | if (!isset(self::$alreadyParsed[$input])) { |
| 35 | self::parse($input); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | public function toPhpObject(): mixed |
| 40 | { |
| 41 | self::parse($this->internal); |
| 42 | return self::$alreadyParsed[$this->internal]; |
| 43 | } |
| 44 | } |