Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ArrayParser | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
13 | |
100.00% |
1 / 1 |
| serializeArray | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| parseVariable | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
10 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Utils\Parser\Php |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Utils\Parser\Php; |
| 16 | |
| 17 | use phpOMS\Contract\SerializableInterface; |
| 18 | |
| 19 | /** |
| 20 | * Array parser class. |
| 21 | * |
| 22 | * Parsing/serializing arrays to and from php file |
| 23 | * |
| 24 | * @package phpOMS\Utils\Parser\Php |
| 25 | * @license OMS License 2.0 |
| 26 | * @link https://jingga.app |
| 27 | * @since 1.0.0 |
| 28 | */ |
| 29 | class ArrayParser |
| 30 | { |
| 31 | /** |
| 32 | * Serializing array (recursively). |
| 33 | * |
| 34 | * @param array $arr Array to serialize |
| 35 | * @param int $depth Array depth |
| 36 | * |
| 37 | * @return string |
| 38 | * |
| 39 | * @since 1.0.0 |
| 40 | */ |
| 41 | public static function serializeArray(array $arr, int $depth = 1) : string |
| 42 | { |
| 43 | $stringify = '[' . "\n"; |
| 44 | |
| 45 | foreach ($arr as $key => $val) { |
| 46 | if (\is_string($key)) { |
| 47 | $key = '\'' . \str_replace('\'', '\\\'', $key) . '\''; |
| 48 | } |
| 49 | |
| 50 | $stringify .= \str_repeat(' ', $depth * 4) . $key . ' => ' . self::parseVariable($val, $depth + 1) . ',' . "\n"; |
| 51 | } |
| 52 | |
| 53 | return $stringify . \str_repeat(' ', ($depth - 1) * 4) . ']'; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Serialize value. |
| 58 | * |
| 59 | * @param mixed $value Value to serialzie |
| 60 | * @param int $depth Array depth |
| 61 | * |
| 62 | * @return string Returns the parsed value as string representation |
| 63 | * |
| 64 | * @throws \UnexpectedValueException Throws this exception if the value cannot be parsed (invalid data type) |
| 65 | * |
| 66 | * @since 1.0.0 |
| 67 | */ |
| 68 | public static function parseVariable(mixed $value, int $depth = 1) : string |
| 69 | { |
| 70 | if (\is_array($value)) { |
| 71 | return self::serializeArray($value, $depth); |
| 72 | } elseif (\is_string($value)) { |
| 73 | return '\'' . \str_replace('\'', '\\\'', $value) . '\''; |
| 74 | } elseif (\is_bool($value)) { |
| 75 | return $value ? 'true' : 'false'; |
| 76 | } elseif ($value === null) { |
| 77 | return 'null'; |
| 78 | } elseif (\is_float($value)) { |
| 79 | return \rtrim(\rtrim(\number_format($value, 5, '.', ''), '0'), '.'); |
| 80 | } elseif (\is_scalar($value)) { |
| 81 | return (string) $value; |
| 82 | } elseif ($value instanceof SerializableInterface) { |
| 83 | return self::parseVariable($value->serialize()); |
| 84 | } elseif ($value instanceof \JsonSerializable) { |
| 85 | return self::parseVariable($value->jsonSerialize()); |
| 86 | } else { |
| 87 | throw new \UnexpectedValueException(); |
| 88 | } |
| 89 | } |
| 90 | } |