|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @see https://github.com/open-code-modeling/json-schema-to-php for the canonical source repository |
| 5 | + * @copyright https://github.com/open-code-modeling/json-schema-to-php/blob/master/COPYRIGHT.md |
| 6 | + * @license https://github.com/open-code-modeling/json-schema-to-php/blob/master/LICENSE.md MIT License |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace OpenCodeModeling\JsonSchemaToPhp\Shorthand; |
| 12 | + |
| 13 | +use OpenCodeModeling\JsonSchemaToPhp\Exception\InvalidShorthand; |
| 14 | +use OpenCodeModeling\JsonSchemaToPhp\Exception\LogicException; |
| 15 | + |
| 16 | +final class Shorthand |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @param array<string, mixed> $shorthand |
| 20 | + * @return array<string, mixed> |
| 21 | + */ |
| 22 | + public static function convertToJsonSchema(array $shorthand): array |
| 23 | + { |
| 24 | + $schema = [ |
| 25 | + 'type' => 'object', |
| 26 | + 'properties' => [ |
| 27 | + |
| 28 | + ], |
| 29 | + 'required' => [], |
| 30 | + 'additionalProperties' => false, |
| 31 | + ]; |
| 32 | + |
| 33 | + foreach ($shorthand as $property => $shorthandDefinition) { |
| 34 | + if (! \is_string($property) || empty($property)) { |
| 35 | + throw InvalidShorthand::emptyString($shorthand); |
| 36 | + } |
| 37 | + $schemaProperty = $property; |
| 38 | + |
| 39 | + switch (true) { |
| 40 | + case \mb_substr($property, -1) === '?': |
| 41 | + $schemaProperty = \mb_substr($property, 0, -1); |
| 42 | + break; |
| 43 | + case $schemaProperty === '$ref': |
| 44 | + if (\count($shorthand) > 1) { |
| 45 | + throw InvalidShorthand::refWithOtherProperties($shorthand); |
| 46 | + } |
| 47 | + |
| 48 | + if (! \is_string($shorthandDefinition)) { |
| 49 | + throw InvalidShorthand::refNotString($shorthand); |
| 50 | + } |
| 51 | + |
| 52 | + $shorthandDefinition = \str_replace('#/definitions/', '', $shorthandDefinition); |
| 53 | + |
| 54 | + return [ |
| 55 | + '$ref' => "#/definitions/$shorthandDefinition", |
| 56 | + ]; |
| 57 | + case $schemaProperty === '$items': |
| 58 | + if (\count($shorthand) > 1) { |
| 59 | + throw InvalidShorthand::itemsWithOtherProperties($shorthand); |
| 60 | + } |
| 61 | + |
| 62 | + if (! \is_string($shorthandDefinition)) { |
| 63 | + throw InvalidShorthand::itemsNotString($shorthand); |
| 64 | + } |
| 65 | + |
| 66 | + if (\mb_substr($shorthandDefinition, -2) !== '[]') { |
| 67 | + $shorthandDefinition .= '[]'; |
| 68 | + } |
| 69 | + |
| 70 | + return self::convertShorthandStringToJsonSchema($shorthandDefinition); |
| 71 | + case $schemaProperty === '$title': |
| 72 | + $schema['title'] = $shorthandDefinition; |
| 73 | + continue 2; |
| 74 | + default: |
| 75 | + $schema['required'][] = $schemaProperty; |
| 76 | + break; |
| 77 | + } |
| 78 | + |
| 79 | + if (\is_array($shorthandDefinition)) { |
| 80 | + $schema['properties'][$schemaProperty] = self::convertToJsonSchema($shorthandDefinition); |
| 81 | + } elseif (\is_string($shorthandDefinition)) { |
| 82 | + $schema['properties'][$schemaProperty] = self::convertShorthandStringToJsonSchema($shorthandDefinition); |
| 83 | + } else { |
| 84 | + throw InvalidShorthand::cannotParseProperty($schemaProperty, $shorthand); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return $schema; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param string $shorthandStr |
| 93 | + * @return array<string, mixed> |
| 94 | + */ |
| 95 | + private static function convertShorthandStringToJsonSchema(string $shorthandStr): array |
| 96 | + { |
| 97 | + if ($shorthandStr === '') { |
| 98 | + return ['type' => 'string']; |
| 99 | + } |
| 100 | + |
| 101 | + $parts = \explode('|', $shorthandStr); |
| 102 | + |
| 103 | + if ($parts[0] === 'enum') { |
| 104 | + return ['enum' => \array_slice($parts, 1)]; |
| 105 | + } |
| 106 | + |
| 107 | + if (\mb_substr($parts[0], -2) === '[]') { |
| 108 | + $itemsParts = [\mb_substr($parts[0], 0, -2)]; |
| 109 | + \array_push($itemsParts, ...\array_slice($parts, 1)); |
| 110 | + |
| 111 | + return [ |
| 112 | + 'type' => 'array', |
| 113 | + 'items' => self::convertShorthandStringToJsonSchema(\implode('|', $itemsParts)), |
| 114 | + ]; |
| 115 | + } |
| 116 | + |
| 117 | + switch ($parts[0]) { |
| 118 | + case 'string': |
| 119 | + case 'integer': |
| 120 | + case 'number': |
| 121 | + case 'boolean': |
| 122 | + $type = $parts[0]; |
| 123 | + |
| 124 | + if (isset($parts[1]) && $parts[1] === 'null') { |
| 125 | + $type = [$type, 'null']; |
| 126 | + |
| 127 | + \array_splice($parts, 1, 1); |
| 128 | + } |
| 129 | + |
| 130 | + $schema = ['type' => $type]; |
| 131 | + |
| 132 | + if (\count($parts) > 1) { |
| 133 | + $parts = \array_slice($parts, 1); |
| 134 | + |
| 135 | + foreach ($parts as $part) { |
| 136 | + [$validationKey, $validationValue] = self::parseShorthandValidation($part); |
| 137 | + |
| 138 | + $schema[$validationKey] = $validationValue; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return $schema; |
| 143 | + default: |
| 144 | + return [ |
| 145 | + '$ref' => '#/definitions/'.$parts[0], |
| 146 | + ]; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @param string $shorthandValidation |
| 152 | + * @return array<mixed> |
| 153 | + */ |
| 154 | + private static function parseShorthandValidation(string $shorthandValidation): array |
| 155 | + { |
| 156 | + $parts = \explode(':', $shorthandValidation); |
| 157 | + |
| 158 | + if (\count($parts) !== 2) { |
| 159 | + throw new LogicException(\sprintf( |
| 160 | + 'Cannot parse shorthand validation: "%s". Expected format "validationKey:value". Please check again!', |
| 161 | + $shorthandValidation |
| 162 | + )); |
| 163 | + } |
| 164 | + |
| 165 | + [$validationKey, $value] = $parts; |
| 166 | + |
| 167 | + if ($value === 'true') { |
| 168 | + return [$validationKey, true]; |
| 169 | + } |
| 170 | + |
| 171 | + if ($value === 'false') { |
| 172 | + return [$validationKey, false]; |
| 173 | + } |
| 174 | + |
| 175 | + if ((string) (int) $value === $value) { |
| 176 | + return [$validationKey, (int) $value]; |
| 177 | + } |
| 178 | + |
| 179 | + if ((string) (float) $value === $value) { |
| 180 | + return [$validationKey, (float) $value]; |
| 181 | + } |
| 182 | + |
| 183 | + return [$validationKey, $value]; |
| 184 | + } |
| 185 | +} |
0 commit comments