|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of ILIAS, a powerful learning management system |
| 5 | + * published by ILIAS open source e-Learning e.V. |
| 6 | + * |
| 7 | + * ILIAS is licensed with the GPL-3.0, |
| 8 | + * see https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | + * You should have received a copy of said license along with the |
| 10 | + * source code, too. |
| 11 | + * |
| 12 | + * If this is not the case or you just want to try ILIAS, you'll find |
| 13 | + * us at: |
| 14 | + * https://www.ilias.de |
| 15 | + * https://github.com/ILIAS-eLearning |
| 16 | + * |
| 17 | + *********************************************************************/ |
| 18 | + |
| 19 | +declare(strict_types=1); |
| 20 | + |
| 21 | +namespace ILIAS\Refinery\Decode\Transformation; |
| 22 | + |
| 23 | +use ILIAS\Refinery\DeriveApplyToFromTransform; |
| 24 | +use ILIAS\Refinery\DeriveInvokeFromTransform; |
| 25 | +use ILIAS\Refinery\Transformation; |
| 26 | +use InvalidArgumentException; |
| 27 | +use JsonException; |
| 28 | + |
| 29 | +/** |
| 30 | + * This class is a wrapper around `json_decode` which rejects undecodable input with an |
| 31 | + * `InvalidArgumentException` instead of returning `null`, which cannot be told apart from the |
| 32 | + * successfully decoded JSON literal `null`. |
| 33 | + * |
| 34 | + * JSON objects are decoded into associative arrays instead of `stdClass`, so that results can be |
| 35 | + * processed further with the `container`, `to` and `kindlyTo` groups and round-trip with the |
| 36 | + * `encode` group. |
| 37 | + * |
| 38 | + * Please see https://www.php.net/manual/en/function.json-decode.php for more information. |
| 39 | + */ |
| 40 | +final class Json implements Transformation |
| 41 | +{ |
| 42 | + use DeriveInvokeFromTransform; |
| 43 | + use DeriveApplyToFromTransform; |
| 44 | + |
| 45 | + /** |
| 46 | + * PHP does not expose the default of its JSON parser (PHP_JSON_PARSER_DEFAULT_DEPTH) to userland. |
| 47 | + */ |
| 48 | + public const int DEFAULT_MAX_DEPTH = 512; |
| 49 | + public const int MAX_DEPTH_LOWER_BOUND = 1; |
| 50 | + public const int MAX_DEPTH_UPPER_BOUND = 2147483647; |
| 51 | + |
| 52 | + public function __construct(private readonly int $max_depth = self::DEFAULT_MAX_DEPTH) |
| 53 | + { |
| 54 | + if ($max_depth < self::MAX_DEPTH_LOWER_BOUND || $max_depth > self::MAX_DEPTH_UPPER_BOUND) { |
| 55 | + throw new InvalidArgumentException( |
| 56 | + \sprintf( |
| 57 | + 'Maximum depth must be between %d and %d, got %d.', |
| 58 | + self::MAX_DEPTH_LOWER_BOUND, |
| 59 | + self::MAX_DEPTH_UPPER_BOUND, |
| 60 | + $max_depth |
| 61 | + ) |
| 62 | + ); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public function transform($from): mixed |
| 67 | + { |
| 68 | + if (!\is_string($from)) { |
| 69 | + throw new InvalidArgumentException( |
| 70 | + \sprintf( |
| 71 | + 'The value of type "%s" is not a string and cannot be decoded as JSON.', |
| 72 | + get_debug_type($from) |
| 73 | + ) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + return json_decode($from, true, $this->max_depth, JSON_THROW_ON_ERROR); |
| 79 | + } catch (JsonException $exception) { |
| 80 | + // The value itself is left out of the message, it may be large and carry sensitive data. |
| 81 | + throw new InvalidArgumentException( |
| 82 | + \sprintf('The value cannot be decoded as JSON: %s.', $exception->getMessage()) |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments