Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions generator/ComponentClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
Expand Down Expand Up @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions generator/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
19 changes: 0 additions & 19 deletions src/Data/V30/ContactTypeEnum.php

This file was deleted.

19 changes: 0 additions & 19 deletions src/Data/V30/DatabankViewOption.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Data/V30/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
19 changes: 0 additions & 19 deletions src/Data/V30/WebsiteProductListType.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Responses/V30/ContactCharacteristic.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}
Expand All @@ -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,
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Responses/V30/WebshopVisitorBehaviour.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand All @@ -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,
);
Expand Down
52 changes: 52 additions & 0 deletions tests/Feature/ProductMediaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Webparking\Logic4Client\Tests\Feature;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Mockery\MockInterface;
use Webparking\Logic4Client\ClientFactory;
use Webparking\Logic4Client\Requests\V30\ProductRequest;
use Webparking\Logic4Client\Tests\TestCase;

final class ProductMediaTest extends TestCase
{
public function testGetProductMediaDecodesStringResourceType(): void
{
$client = \Mockery::mock(Client::class, static function (MockInterface $mock): void {
$mock->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);
}
}