diff --git a/generator/ComponentClassGenerator.php b/generator/ComponentClassGenerator.php index 2a3b747..014b7b7 100644 --- a/generator/ComponentClassGenerator.php +++ b/generator/ComponentClassGenerator.php @@ -51,9 +51,17 @@ public function resolve(string $name, string $namespace, string $version): strin $typedParameters = []; $arrayParameters = []; $dateParameters = []; + $enumParameters = []; foreach ($component->properties as $parameterName => $parameter) { - if ($parameter instanceof Reference) { + $referenced = $parameter instanceof Reference + ? $this->referencedComponent($parameter) + : null; + + if ($referenced instanceof Schema && Helpers::isEnum($referenced)) { + $type = Helpers::enumPhpType($referenced); + $enumParameters[] = $parameterName; + } elseif ($parameter instanceof Reference) { $type = $this->resolve($parameter->getReference(), 'Data', $version); $typedParameters[$parameterName] = $type; } else { @@ -80,7 +88,7 @@ public function resolve(string $name, string $namespace, string $version): strin } } - $nullable = !\in_array($parameterName, ['Records', 'ValidationMessages'], true) && (Helpers::isNullable($parameter) || class_exists($type)); + $nullable = !\in_array($parameterName, ['Records', 'ValidationMessages'], true) && (Helpers::isNullable($parameter) || class_exists($type) || \in_array($parameterName, $enumParameters, true)); $constructor ->addPromotedParameter($this->propertyName($parameterName)) @@ -150,6 +158,13 @@ public function resolve(string $name, string $namespace, string $version): strin return $className; } + private function referencedComponent(Reference $reference): ?Schema + { + $componentName = str_replace('#/components/schemas/', '', $reference->getReference()); + + return $this->components[$componentName] ?? null; + } + private function propertyName(string $name): string { $name = str_replace('_', '', $name); diff --git a/generator/Helpers.php b/generator/Helpers.php index f975504..2b29b17 100644 --- a/generator/Helpers.php +++ b/generator/Helpers.php @@ -97,6 +97,40 @@ public static function makePhpDoc(array $properties, string $format): array return $parameters; } + public static function isEnum(Schema $schema): bool + { + return [] !== ($schema->enum ?? []); + } + + public static function enumPhpType(Schema $schema): string + { + $primary = self::primaryType($schema->type); + + if (null !== $primary) { + return self::phpType($primary, $primary); + } + + foreach ($schema->enum ?? [] as $value) { + if (\is_int($value)) { + return 'int'; + } + + if (\is_float($value)) { + return 'float'; + } + + if (\is_bool($value)) { + return 'bool'; + } + + if (\is_string($value)) { + return 'string'; + } + } + + return 'string'; + } + public static function phpType(mixed $type, string $default = 'mixed'): string { if (\is_array($type)) { diff --git a/src/Data/V30/ContactTypeEnum.php b/src/Data/V30/ContactTypeEnum.php deleted file mode 100644 index eb09c2d..0000000 --- a/src/Data/V30/ContactTypeEnum.php +++ /dev/null @@ -1,19 +0,0 @@ - $data */ - public static function make(array $data): self - { - return new self( - ); - } -} diff --git a/src/Data/V30/DatabankViewOption.php b/src/Data/V30/DatabankViewOption.php deleted file mode 100644 index 84b4ce1..0000000 --- a/src/Data/V30/DatabankViewOption.php +++ /dev/null @@ -1,19 +0,0 @@ - $data */ - public static function make(array $data): self - { - return new self( - ); - } -} diff --git a/src/Data/V30/ProductResource.php b/src/Data/V30/ProductResource.php index 45eaea5..997870c 100644 --- a/src/Data/V30/ProductResource.php +++ b/src/Data/V30/ProductResource.php @@ -8,7 +8,7 @@ class ProductResource { public function __construct( public ?string $fileName, - public ?DatabankViewOption $type, + public ?string $type, public ?string $description, public ?int $sorting, public ?string $url, @@ -20,7 +20,7 @@ public static function make(array $data): self { return new self( fileName: $data['FileName'] ?? null, - type: isset($data['Type']) ? DatabankViewOption::make($data['Type']) : null, + type: $data['Type'] ?? null, description: $data['Description'] ?? null, sorting: $data['Sorting'] ?? null, url: $data['Url'] ?? null, diff --git a/src/Data/V30/WebsiteProductListType.php b/src/Data/V30/WebsiteProductListType.php deleted file mode 100644 index 4e96424..0000000 --- a/src/Data/V30/WebsiteProductListType.php +++ /dev/null @@ -1,19 +0,0 @@ - $data */ - public static function make(array $data): self - { - return new self( - ); - } -} diff --git a/src/Responses/V30/ContactCharacteristic.php b/src/Responses/V30/ContactCharacteristic.php index 7825c78..70c0fb6 100644 --- a/src/Responses/V30/ContactCharacteristic.php +++ b/src/Responses/V30/ContactCharacteristic.php @@ -9,7 +9,7 @@ class ContactCharacteristic public function __construct( public int $contactId, public ?int $debtorId, - public ?\Webparking\Logic4Client\Data\V30\ContactTypeEnum $contactType, + public ?string $contactType, public ?string $relationCharacteristic, ) { } @@ -20,7 +20,7 @@ public static function make(array $data): self return new self( contactId: $data['ContactId'] ?? 0, debtorId: $data['DebtorId'] ?? null, - contactType: isset($data['ContactType']) ? \Webparking\Logic4Client\Data\V30\ContactTypeEnum::make($data['ContactType']) : null, + contactType: $data['ContactType'] ?? null, relationCharacteristic: $data['RelationCharacteristic'] ?? null, ); } diff --git a/src/Responses/V30/WebshopVisitorBehaviour.php b/src/Responses/V30/WebshopVisitorBehaviour.php index 13b82ef..858ebb6 100644 --- a/src/Responses/V30/WebshopVisitorBehaviour.php +++ b/src/Responses/V30/WebshopVisitorBehaviour.php @@ -14,7 +14,7 @@ public function __construct( public ?int $composedProductParentId, public ?float $qtyDec, public ?string $commission, - public ?\Webparking\Logic4Client\Data\V30\WebsiteProductListType $webshopUserProductListType, + public ?string $webshopUserProductListType, public ?int $debtorId, public ?int $websiteDomainId, ) { @@ -31,7 +31,7 @@ public static function make(array $data): self composedProductParentId: $data['ComposedProductParentId'] ?? null, qtyDec: $data['QtyDec'] ?? null, commission: $data['Commission'] ?? null, - webshopUserProductListType: isset($data['WebshopUserProductListType']) ? \Webparking\Logic4Client\Data\V30\WebsiteProductListType::make($data['WebshopUserProductListType']) : null, + webshopUserProductListType: $data['WebshopUserProductListType'] ?? null, debtorId: $data['DebtorId'] ?? null, websiteDomainId: $data['WebsiteDomainId'] ?? null, ); diff --git a/tests/Feature/ProductMediaTest.php b/tests/Feature/ProductMediaTest.php new file mode 100644 index 0000000..00b0095 --- /dev/null +++ b/tests/Feature/ProductMediaTest.php @@ -0,0 +1,52 @@ +shouldReceive('post') + ->once() + ->with('/v3/Products/GetProductMedia', ['json' => ['ProductIds' => [123]]]) + ->andReturn(new Response(body: json_encode([ + [ + 'ProductId' => 123, + 'Resources' => [ + [ + 'FileName' => 'photo.jpg', + 'Type' => 'Photo', + 'Description' => 'Front', + 'Sorting' => 1, + 'Url' => 'https://example.test/photo.jpg', + ], + ], + ], + ], \JSON_THROW_ON_ERROR))); + }); + + $clientFactory = \Mockery::mock(ClientFactory::class, static function (MockInterface $mock) use ($client): void { + $mock->shouldReceive('make') + ->once() + ->andReturn($client); + }); + + $mediaFiles = (new ProductRequest($clientFactory))->getProductMedia(['ProductIds' => [123]]); + + static::assertCount(1, $mediaFiles); + static::assertSame(123, $mediaFiles[0]->productId); + static::assertNotNull($mediaFiles[0]->resources); + static::assertSame('Photo', $mediaFiles[0]->resources[0]->type); + static::assertSame('photo.jpg', $mediaFiles[0]->resources[0]->fileName); + } +}