|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Helpers\MetaFormats; |
| 4 | + |
| 5 | +use App\Exceptions\InternalServerException; |
| 6 | + |
| 7 | +class FieldFormatDefinition |
| 8 | +{ |
| 9 | + public ?string $format; |
| 10 | + // A string name of the field type yielded by 'ReflectionProperty::getType()'. |
| 11 | + public ?string $type; |
| 12 | + |
| 13 | + ///TODO: double check this |
| 14 | + private static array $gettypeToReflectiveMap = [ |
| 15 | + "boolean" => "bool", |
| 16 | + "integer" => "int", |
| 17 | + "double" => "double", |
| 18 | + "string" => "string", |
| 19 | + "array" => "array", |
| 20 | + "object" => "object", |
| 21 | + "resource" => "resource", |
| 22 | + "NULL" => "null", |
| 23 | + ]; |
| 24 | + |
| 25 | + /** |
| 26 | + * Constructs a field format definition. |
| 27 | + * Either the @format or @type parameter need to have a non-null value (or both). |
| 28 | + * @param ?string $format The format of the field. |
| 29 | + * @param ?string $type The PHP type of the field yielded by a 'ReflectionProperty::getType()' call. |
| 30 | + * @throws \App\Exceptions\InternalServerException Thrown when both @format and @type were null. |
| 31 | + */ |
| 32 | + public function __construct(?string $format, ?string $type) |
| 33 | + { |
| 34 | + // if both are null, there is no way to validate an assigned value |
| 35 | + if ($format === null && $type === null) { |
| 36 | + throw new InternalServerException("Both the format and type of a field definition were undefined."); |
| 37 | + } |
| 38 | + |
| 39 | + $this->format = $format; |
| 40 | + $this->type = $type; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Checks whether a value meets this definition. |
| 45 | + * @param mixed $value The value to be checked. |
| 46 | + * @throws \App\Exceptions\InternalServerException Thrown when the format does not have a validator. |
| 47 | + * @return bool Returns whether the value passed the test. |
| 48 | + */ |
| 49 | + public function conformsToDefinition(mixed $value) |
| 50 | + { |
| 51 | + // use format validators if possible |
| 52 | + if ($this->format !== null) { |
| 53 | + // enables parsing more complicated formats (string[]?, string?[], string?[][]?, ...) |
| 54 | + $parsedFormat = new FormatParser($this->format); |
| 55 | + return self::recursiveFormatChecker($value, $parsedFormat); |
| 56 | + } |
| 57 | + |
| 58 | + // convert the gettype return value to the reflective return value |
| 59 | + $valueType = gettype($value); |
| 60 | + if (!array_key_exists($valueType, self::$gettypeToReflectiveMap)) { |
| 61 | + throw new InternalServerException("Unknown gettype value: $valueType"); |
| 62 | + } |
| 63 | + return $valueType === $this->type; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Checks whether the value fits a format recursively. |
| 68 | + * The format can contain array modifiers and thus all array elements need to be checked recursively. |
| 69 | + * @param mixed $value The value to be checked |
| 70 | + * @param \App\Helpers\MetaFormats\FormatParser $parsedFormat A parsed format used for recursive traversal. |
| 71 | + * @throws \App\Exceptions\InternalServerException Thrown when a format does not have a validator. |
| 72 | + * @return bool Returns whether the value conforms to the format. |
| 73 | + */ |
| 74 | + private static function recursiveFormatChecker(mixed $value, FormatParser $parsedFormat): bool |
| 75 | + { |
| 76 | + // check nullability |
| 77 | + if ($value === null) { |
| 78 | + return $parsedFormat->nullable; |
| 79 | + } |
| 80 | + |
| 81 | + // handle arrays |
| 82 | + if ($parsedFormat->isArray) { |
| 83 | + if (!is_array($value)) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + // if any element fails, the whole format fails |
| 88 | + foreach ($value as $element) { |
| 89 | + if (!self::recursiveFormatChecker($element, $parsedFormat->nested)) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + } |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + // check whether the validator exists |
| 97 | + $validators = FormatCache::getValidators(); |
| 98 | + if (!array_key_exists($parsedFormat->format, $validators)) { |
| 99 | + throw new InternalServerException("The format {$parsedFormat->format} does not have a validator."); |
| 100 | + } |
| 101 | + |
| 102 | + return $validators[$parsedFormat->format]($value); |
| 103 | + } |
| 104 | +} |
0 commit comments