|
| 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 | + if (std_object_has($stdProperty, 'type')) { |
| 38 | + switch (OpenApi3PropertyTypeEnum::from($stdProperty->type)) { |
| 39 | + case OpenApi3PropertyTypeEnum::OBJECT: |
| 40 | + $this->object = new OpenApi3Object(); |
| 41 | + $this->object->fillFromStdObject($stdProperty); |
| 42 | + |
| 43 | + break; |
| 44 | + case OpenApi3PropertyTypeEnum::ARRAY: |
| 45 | + if (std_object_has($stdProperty, 'items')) { |
| 46 | + do_with_all_of($stdProperty->items, function (stdClass $p) use ($propertyName) { |
| 47 | + if (!$this->items && std_object_has($p, 'type')) { |
| 48 | + $this->items = new OpenApi3ObjectProperty(type: $p->type); |
| 49 | + } |
| 50 | + $this->items?->fillFromStdProperty("{$propertyName}.*", $p); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + break; |
| 55 | + default: |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public function getLaravelValidationsAndEnums(array $options, array &$validations = [], array &$enums = [], string $namePrefix = null): array |
| 61 | + { |
| 62 | + $name = "{$namePrefix}{$this->name}"; |
| 63 | + |
| 64 | + if ($this->required) { |
| 65 | + $validations[$name][] = "'required'"; |
| 66 | + } |
| 67 | + if ($this->nullable) { |
| 68 | + $validations[$name][] = "'nullable'"; |
| 69 | + } |
| 70 | + if ($this->enumClass) { |
| 71 | + $validations[$name][] = "new Enum({$this->enumClass}::class)"; |
| 72 | + $enums[$this->enumClass] = true; |
| 73 | + } else { |
| 74 | + [$currentValidations, $currentEnums] = $this->getValidationsAndEnumsByTypeAndFormat($options, $validations, $enums, $name); |
| 75 | + $validations = array_merge($validations, $currentValidations); |
| 76 | + $enums = array_merge($enums, $currentEnums); |
| 77 | + } |
| 78 | + |
| 79 | + return [$validations, $enums]; |
| 80 | + } |
| 81 | + |
| 82 | + protected function getValidationsAndEnumsByTypeAndFormat(array $options, array &$validations, array &$enums, string $name): array |
| 83 | + { |
| 84 | + $type = OpenApi3PropertyTypeEnum::from($this->type); |
| 85 | + $format = OpenApi3PropertyFormatEnum::tryFrom($this->format); |
| 86 | + switch ($type) { |
| 87 | + case OpenApi3PropertyTypeEnum::INTEGER: |
| 88 | + case OpenApi3PropertyTypeEnum::BOOLEAN: |
| 89 | + case OpenApi3PropertyTypeEnum::NUMBER: |
| 90 | + $validations[$name][] = "'{$type->toLaravelValidationRule()->value}'"; |
| 91 | + |
| 92 | + break; |
| 93 | + case OpenApi3PropertyTypeEnum::STRING: |
| 94 | + switch ($format) { |
| 95 | + case OpenApi3PropertyFormatEnum::DATE: |
| 96 | + case OpenApi3PropertyFormatEnum::DATE_TIME: |
| 97 | + case OpenApi3PropertyFormatEnum::PASSWORD: |
| 98 | + case OpenApi3PropertyFormatEnum::EMAIL: |
| 99 | + case OpenApi3PropertyFormatEnum::IPV4: |
| 100 | + case OpenApi3PropertyFormatEnum::IPV6: |
| 101 | + case OpenApi3PropertyFormatEnum::TIMEZONE: |
| 102 | + case OpenApi3PropertyFormatEnum::PHONE: |
| 103 | + case OpenApi3PropertyFormatEnum::URL: |
| 104 | + case OpenApi3PropertyFormatEnum::UUID: |
| 105 | + $validations[$name][] = "'{$format->toLaravelValidationRule()->value}'"; |
| 106 | + |
| 107 | + break; |
| 108 | + case OpenApi3PropertyFormatEnum::BINARY: |
| 109 | + $validations[$name][] = "'". LaravelValidationRuleEnum::FILE->value . "'"; |
| 110 | + |
| 111 | + break; |
| 112 | + default: |
| 113 | + $validations[$name][] = "'{$type->toLaravelValidationRule()->value}'"; |
| 114 | + |
| 115 | + break; |
| 116 | + } |
| 117 | + |
| 118 | + break; |
| 119 | + case OpenApi3PropertyTypeEnum::OBJECT: |
| 120 | + foreach ($this->object->properties ?? [] as $property) { |
| 121 | + [$currentValidations, $currentEnums] = $property->getLaravelValidationsAndEnums($options, $validations, $enums, "{$name}."); |
| 122 | + $validations = array_merge($validations, $currentValidations); |
| 123 | + $enums = array_merge($enums, $currentEnums); |
| 124 | + } |
| 125 | + |
| 126 | + break; |
| 127 | + case OpenApi3PropertyTypeEnum::ARRAY: |
| 128 | + $validations[$name][] = "'{$type->toLaravelValidationRule()->value}'"; |
| 129 | + [$currentValidations, $currentEnums] = $this->items->getLaravelValidationsAndEnums($options, $validations, $enums, "{$name}.*"); |
| 130 | + $validations = array_merge($validations, $currentValidations); |
| 131 | + $enums = array_merge($enums, $currentEnums); |
| 132 | + } |
| 133 | + |
| 134 | + return [$validations, $enums]; |
| 135 | + } |
| 136 | +} |
0 commit comments