|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ensi\LaravelOpenApiServerGenerator\Data\OpenApi3; |
| 4 | + |
| 5 | +use Ensi\LaravelOpenApiServerGenerator\Enums\LaravelValidationRuleEnum; |
| 6 | +use Ensi\LaravelOpenApiServerGenerator\Enums\OpenApi3PropertyFormatEnum; |
| 7 | +use Ensi\LaravelOpenApiServerGenerator\Enums\OpenApi3PropertyTypeEnum; |
| 8 | +use stdClass; |
| 9 | + |
| 10 | +class OpenApi3ObjectProperty |
| 11 | +{ |
| 12 | + public function __construct( |
| 13 | + public string $type, |
| 14 | + public ?string $name = null, |
| 15 | + public ?string $format = null, |
| 16 | + public bool $required = false, |
| 17 | + public bool $nullable = false, |
| 18 | + public ?string $enumClass = null, |
| 19 | + public ?OpenApi3Object $object = null, |
| 20 | + public ?OpenApi3ObjectProperty $items = null, |
| 21 | + ) { |
| 22 | + // |
| 23 | + } |
| 24 | + |
| 25 | + public function fillFromStdProperty(string $propertyName, stdClass $stdProperty): void |
| 26 | + { |
| 27 | + if (std_object_has($stdProperty, 'nullable')) { |
| 28 | + $this->nullable = true; |
| 29 | + } |
| 30 | + if (std_object_has($stdProperty, 'format')) { |
| 31 | + $this->format = $stdProperty->format; |
| 32 | + } |
| 33 | + if (std_object_has($stdProperty, 'x-lg-enum-class')) { |
| 34 | + $this->enumClass = $stdProperty->{'x-lg-enum-class'}; |
| 35 | + } |
| 36 | + |
| 37 | + switch (OpenApi3PropertyTypeEnum::from($stdProperty->type)) { |
| 38 | + case OpenApi3PropertyTypeEnum::OBJECT: |
| 39 | + $this->object = new OpenApi3Object(); |
| 40 | + $this->object->fillFromStdObject($stdProperty); |
| 41 | + |
| 42 | + break; |
| 43 | + case OpenApi3PropertyTypeEnum::ARRAY: |
| 44 | + $this->items = new OpenApi3ObjectProperty(type: $stdProperty->items->type); |
| 45 | + $this->items->fillFromStdProperty("{$propertyName}.*", $stdProperty->items); |
| 46 | + |
| 47 | + break; |
| 48 | + default: |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public function getLaravelValidationsAndEnums(array $options, array &$validations = [], array &$enums = [], string $namePrefix = null): array |
| 53 | + { |
| 54 | + $name = "{$namePrefix}{$this->name}"; |
| 55 | + |
| 56 | + if ($this->required) { |
| 57 | + $validations[$name][] = "'required'"; |
| 58 | + } |
| 59 | + if ($this->nullable) { |
| 60 | + $validations[$name][] = "'nullable'"; |
| 61 | + } |
| 62 | + if ($this->enumClass) { |
| 63 | + $validations[$name][] = "new Enum({$this->enumClass}::class)"; |
| 64 | + $enums[$this->enumClass] = true; |
| 65 | + } else { |
| 66 | + [$currentValidations, $currentEnums] = $this->getValidationsAndEnumsByTypeAndFormat($options, $validations, $enums, $name); |
| 67 | + $validations = array_merge($validations, $currentValidations); |
| 68 | + $enums = array_merge($enums, $currentEnums); |
| 69 | + } |
| 70 | + |
| 71 | + return [$validations, $enums]; |
| 72 | + } |
| 73 | + |
| 74 | + protected function getValidationsAndEnumsByTypeAndFormat(array $options, array &$validations, array &$enums, string $name): array |
| 75 | + { |
| 76 | + $type = OpenApi3PropertyTypeEnum::from($this->type); |
| 77 | + $format = OpenApi3PropertyFormatEnum::tryFrom($this->format); |
| 78 | + switch ($type) { |
| 79 | + case OpenApi3PropertyTypeEnum::INTEGER: |
| 80 | + case OpenApi3PropertyTypeEnum::BOOLEAN: |
| 81 | + case OpenApi3PropertyTypeEnum::NUMBER: |
| 82 | + $validations[$name][] = "'{$type->toLaravelValidationRule()->value}'"; |
| 83 | + |
| 84 | + break; |
| 85 | + case OpenApi3PropertyTypeEnum::STRING: |
| 86 | + switch ($format) { |
| 87 | + case OpenApi3PropertyFormatEnum::DATE: |
| 88 | + case OpenApi3PropertyFormatEnum::DATE_TIME: |
| 89 | + case OpenApi3PropertyFormatEnum::PASSWORD: |
| 90 | + case OpenApi3PropertyFormatEnum::EMAIL: |
| 91 | + case OpenApi3PropertyFormatEnum::IPV4: |
| 92 | + case OpenApi3PropertyFormatEnum::IPV6: |
| 93 | + case OpenApi3PropertyFormatEnum::TIMEZONE: |
| 94 | + case OpenApi3PropertyFormatEnum::PHONE: |
| 95 | + case OpenApi3PropertyFormatEnum::URL: |
| 96 | + case OpenApi3PropertyFormatEnum::UUID: |
| 97 | + $validations[$name][] = "'{$format->toLaravelValidationRule()->value}'"; |
| 98 | + |
| 99 | + break; |
| 100 | + case OpenApi3PropertyFormatEnum::BINARY: |
| 101 | + $validations[$name][] = "'". LaravelValidationRuleEnum::FILE->value . "'"; |
| 102 | + |
| 103 | + break; |
| 104 | + default: |
| 105 | + $validations[$name][] = "'{$type->toLaravelValidationRule()->value}'"; |
| 106 | + |
| 107 | + break; |
| 108 | + } |
| 109 | + |
| 110 | + break; |
| 111 | + case OpenApi3PropertyTypeEnum::OBJECT: |
| 112 | + foreach ($this->object->properties ?? [] as $property) { |
| 113 | + [$currentValidations, $currentEnums] = $property->getLaravelValidationsAndEnums($options, $validations, $enums, "{$name}."); |
| 114 | + $validations = array_merge($validations, $currentValidations); |
| 115 | + $enums = array_merge($enums, $currentEnums); |
| 116 | + } |
| 117 | + |
| 118 | + break; |
| 119 | + case OpenApi3PropertyTypeEnum::ARRAY: |
| 120 | + $validations[$name][] = "'{$type->toLaravelValidationRule()->value}'"; |
| 121 | + [$currentValidations, $currentEnums] = $this->items->getLaravelValidationsAndEnums($options, $validations, $enums, "{$name}.*"); |
| 122 | + $validations = array_merge($validations, $currentValidations); |
| 123 | + $enums = array_merge($enums, $currentEnums); |
| 124 | + } |
| 125 | + |
| 126 | + return [$validations, $enums]; |
| 127 | + } |
| 128 | +} |
0 commit comments