Skip to content
Closed
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'@PHPUnit84Migration:risky' => true,
'ordered_imports' => true,
'phpdoc_line_span' => [
'class' => 'single',
'const' => 'single',
'method' => 'single',
'property' => 'single',
Expand Down
10 changes: 6 additions & 4 deletions generator/ComponentClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ public function resolve(string $name, string $namespace, string $version): strin
$type = $this->resolve($parameter->getReference(), 'Data', $version);
$typedParameters[$parameterName] = $type;
} else {
$type = Helpers::phpType($parameter->type, $parameter->type);
$primaryType = Helpers::primaryType($parameter->type);
$type = Helpers::phpType($parameter->type, $primaryType ?? 'mixed');

if ('string' === $parameter->type && 'date-time' === $parameter->format) {
if ('string' === $primaryType && 'date-time' === $parameter->format) {
$type = Carbon::class;

$dateParameters[] = $parameterName;
Expand All @@ -71,14 +72,15 @@ public function resolve(string $name, string $namespace, string $version): strin
$arrayParameters[$parameterName] = $commentType;
$commentType = '\\'.$commentType;
} else {
$commentType = $parameter->items?->type ?? 'mixed';
$commentType = $parameter->items ? Helpers::primaryType($parameter->items->type) : null;
$commentType ??= 'mixed';
}

$constructor->addComment('@param array<'.$commentType.'> $'.$this->propertyName($parameterName));
}
}

$nullable = !\in_array($parameterName, ['Records', 'ValidationMessages'], true) && (($parameter->nullable ?? false) || class_exists($type));
$nullable = !\in_array($parameterName, ['Records', 'ValidationMessages'], true) && (Helpers::isNullable($parameter) || class_exists($type));

$constructor
->addPromotedParameter($this->propertyName($parameterName))
Expand Down
14 changes: 8 additions & 6 deletions generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public static function resolveVersions(): array
$versions = [];
foreach ($config['sources'] as $source) {
if (preg_match('/Version v(\d+\.\d+)/', $source['title'], $versionMatch)
&& !str_starts_with($versionMatch[1], '3.')
) {
$versions[$versionMatch[1]] = self::$baseUrl.$source['url'];
}
Expand Down Expand Up @@ -135,21 +134,22 @@ className: $namespace.'Request',
$responseReference = $operation->responses['200']->content['application/json']->schema ?? null;

if ($responseReference instanceof Schema) {
$returnType = $responseReference->type;
$returnType = Helpers::primaryType($responseReference->type);

$arrayType = null;
if ('array' === $returnType) {
if ('array' === $returnType && $responseReference->items instanceof Reference) {
$arrayType = $this->componentClassGenerator->resolve(
$responseReference->items->getReference(),
'Responses',
$this->getVersion($version)
);
} elseif ('array' === $returnType && $responseReference->items instanceof Schema) {
$itemType = Helpers::primaryType($responseReference->items->type);
$arrayType = Helpers::phpType($itemType, $itemType ?? 'mixed');
}

$classMethod = $requestGenerator->addMethod($method, $uri, $operation, $returnType, arrayType: $arrayType);
} else {
Assert::isInstanceOf($responseReference, Reference::class);

} elseif ($responseReference instanceof Reference) {
$requestSchema = $requestProperties instanceof Reference
? $this->resolveReference($requestProperties->getReference())->properties
: [];
Expand Down Expand Up @@ -179,6 +179,8 @@ className: $namespace.'Request',
}

$classMethod = $requestGenerator->addMethod($method, $uri, $operation, returnType: $returnType, paginated: $paginatedResponse ?: null);
} else {
$classMethod = $requestGenerator->addMethod($method, $uri, $operation, void: true);
}

if ($operation->description) {
Expand Down
40 changes: 37 additions & 3 deletions generator/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public static function resolveParameterType(Schema|Reference $property): string
$property = $property->resolve();
}

if ('array' === $property->type) {
$primary = self::primaryType($property->type);

if ('array' === $primary) {
if ($property->items instanceof Reference) {
$property = clone $property;

Expand All @@ -60,15 +62,15 @@ public static function resolveParameterType(Schema|Reference $property): string
} else {
$type = 'array<mixed>';
}
} elseif ('object' === $property->type) {
} elseif ('object' === $primary) {
$properties = [];
foreach ($property->properties as $parameterName => $propertyValue) {
$properties[] = \sprintf('%s?: %s', $parameterName, self::resolveParameterType($propertyValue));
}

$type = \sprintf('array{%s}', implode(', ', $properties));
} else {
$type = $property->type.($property->nullable ? '|null' : '');
$type = ($primary ?? 'mixed').(self::isNullable($property) ? '|null' : '');
}

return $type;
Expand Down Expand Up @@ -97,11 +99,43 @@ public static function makePhpDoc(array $properties, string $format): array

public static function phpType(mixed $type, string $default = 'mixed'): string
{
if (\is_array($type)) {
$type = self::primaryType($type);
}

return match ($type) {
'integer' => 'int',
'number' => 'float',
'boolean' => 'bool',
default => $default,
};
}

public static function primaryType(mixed $type): ?string
{
if (\is_array($type)) {
foreach ($type as $candidate) {
if (\is_string($candidate) && 'null' !== $candidate) {
return $candidate;
}
}

return null;
}

return \is_string($type) ? $type : null;
}

public static function isNullable(Schema|Reference $property): bool
{
if ($property instanceof Reference) {
return false;
}

if (\is_array($property->type) && \in_array('null', $property->type, true)) {
return true;
}

return (bool) ($property->nullable ?? false);
}
}
27 changes: 24 additions & 3 deletions generator/RequestClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(
$this->class->setExtends(Request::class);
}

public function addMethod(string $httpMethod, string $uri, Operation $operation, ?string $returnType = null, ?PaginateType $paginated = null, ?string $arrayType = null): Method
public function addMethod(string $httpMethod, string $uri, Operation $operation, ?string $returnType = null, ?PaginateType $paginated = null, ?string $arrayType = null, bool $void = false): Method
{
$action = last(explode('/', ltrim($uri, '/')));
$requestSchema = $operation->requestBody?->content['application/json']?->schema;
Expand All @@ -44,7 +44,7 @@ public function addMethod(string $httpMethod, string $uri, Operation $operation,
continue;
}

$parameterType = Helpers::phpType($parameter->schema->type, $parameter->schema->type);
$parameterType = Helpers::phpType($parameter->schema->type, Helpers::primaryType($parameter->schema->type) ?? 'mixed');

$method
->addParameter($parameter->name)
Expand All @@ -61,7 +61,8 @@ public function addMethod(string $httpMethod, string $uri, Operation $operation,
$parameterDoc = "array{\n".implode("\n", Helpers::makePhpDoc($requestProperties, ' %s,'))."\n}";
} else {
if ($requestSchema->items instanceof Schema) {
$parameterDoc = "array<{$requestSchema->items->type}>";
$itemType = Helpers::primaryType($requestSchema->items->type) ?? 'mixed';
$parameterDoc = "array<{$itemType}>";
} else {
$requestProperties = $requestSchema->items->resolve()->properties;

Expand Down Expand Up @@ -92,6 +93,17 @@ public function addMethod(string $httpMethod, string $uri, Operation $operation,
? ', '.preg_replace('/\'\{\$(.*)\}\'/', '\$$1', var_export($requestParameters, true))
: '';

if ($void) {
$method->setReturnType('void');
$method->setBody(
<<<PHP
\$this->getClient()->{$httpMethod}('{$uri}'{$parametersPhp});
PHP
);

return $method;
}

$returnType = Helpers::phpType($returnType, $returnType ?? 'mixed');

if ($paginated && class_exists($returnType)) {
Expand All @@ -115,6 +127,15 @@ public function addMethod(string $httpMethod, string $uri, Operation $operation,
PHP
);

} elseif ('array' === $returnType && $arrayType) {
$method->addComment("\n@return array<array-key, {$arrayType}>");
$method->setBody(
<<<PHP
return \$this->buildResponse(
\$this->getClient()->{$httpMethod}('{$uri}'{$parametersPhp}),
);
PHP
);
} elseif (class_exists($returnType)) {
if ($paginated) {
$paginateMethod = match ($paginated) {
Expand Down
2 changes: 1 addition & 1 deletion src/Data/V10/BuyOrderDeliveryRead.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
public ?string $remarks,
public ?string $description,
public ?int $branchId,
public array $rows,
public ?array $rows,
public ?string $pickingListNumber,
) {
}
Expand Down
4 changes: 2 additions & 2 deletions src/Data/V10/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 string $contactType,
public ?ContactTypeEnum $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: $data['ContactType'] ?? '',
contactType: isset($data['ContactType']) ? ContactTypeEnum::make($data['ContactType']) : null,
relationCharacteristic: $data['RelationCharacteristic'] ?? null,
);
}
Expand Down
19 changes: 19 additions & 0 deletions src/Data/V10/ContactTypeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Webparking\Logic4Client\Data\V10;

class ContactTypeEnum
{
public function __construct()
{
}

/** @param array<mixed> $data */
public static function make(array $data): self
{
return new self(
);
}
}
4 changes: 2 additions & 2 deletions src/Data/V10/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Customer
/** @param array<int> $pricelistIds */
public function __construct(
public int $id,
public ?string $loginName,
public ?int $relationGroupId,
public ?int $relationGroup2Id,
public bool $hide,
Expand All @@ -29,6 +28,7 @@ public function __construct(
public ?string $firstName,
public ?string $lastName,
public ?string $emailAddress,
public ?string $loginName,
public ?int $paymentMethodId,
public ?array $pricelistIds,
public ?string $telephoneNumber,
Expand Down Expand Up @@ -65,7 +65,6 @@ public static function make(array $data): self
{
return new self(
id: $data['Id'] ?? 0,
loginName: $data['LoginName'] ?? null,
relationGroupId: $data['RelationGroupId'] ?? null,
relationGroup2Id: $data['RelationGroup2Id'] ?? null,
hide: $data['Hide'] ?? false,
Expand All @@ -85,6 +84,7 @@ public static function make(array $data): self
firstName: $data['FirstName'] ?? null,
lastName: $data['LastName'] ?? null,
emailAddress: $data['EmailAddress'] ?? null,
loginName: $data['LoginName'] ?? null,
paymentMethodId: $data['PaymentMethodId'] ?? null,
pricelistIds: $data['PricelistIds'] ?? null,
telephoneNumber: $data['TelephoneNumber'] ?? null,
Expand Down
2 changes: 1 addition & 1 deletion src/Data/V10/EmailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
public ?\Carbon\Carbon $isReplyedOn,
public ?\Carbon\Carbon $isForwardedOn,
public ?EmailMessageStatus $status,
public array $toEmailAddresses,
public ?array $toEmailAddresses,
public ?EmailAddress $fromEmailAddress,
public ?array $CCEmailAddresses,
public ?array $BCCEmailAddresses,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Webparking\Logic4Client\Data\V10;

class Int32DecimalKeyValuePair
class KeyValuePairOfintAnddecimal
{
public function __construct(
public int $key,
Expand Down
8 changes: 7 additions & 1 deletion src/Data/V10/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public function __construct(
public ?array $payments,
public ?DebtorCostCentre $costCentre,
public ?CustomerAddress $accountAddress,
public array $orderRows,
public ?array $orderRows,
public ?int $deliveryProvinceId,
public ?int $invoiceProvinceId,
public ?int $accountProvinceId,
public ?string $lastClickGclIdValues,
public ?string $lastClickUtmValues,
public ?CustomerAddress $deliveryAddress,
Expand Down Expand Up @@ -73,6 +76,9 @@ public static function make(array $data): self
costCentre: isset($data['CostCentre']) ? DebtorCostCentre::make($data['CostCentre']) : null,
accountAddress: isset($data['AccountAddress']) ? CustomerAddress::make($data['AccountAddress']) : null,
orderRows: array_map(static fn (array $item) => OrderRow::make($item), $data['OrderRows'] ?? []),
deliveryProvinceId: $data['DeliveryProvinceId'] ?? null,
invoiceProvinceId: $data['InvoiceProvinceId'] ?? null,
accountProvinceId: $data['AccountProvinceId'] ?? null,
lastClickGclIdValues: $data['LastClickGclIdValues'] ?? null,
lastClickUtmValues: $data['LastClickUtmValues'] ?? null,
deliveryAddress: isset($data['DeliveryAddress']) ? CustomerAddress::make($data['DeliveryAddress']) : null,
Expand Down
4 changes: 4 additions & 0 deletions src/Data/V10/OrderShipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class OrderShipment
{
public function __construct(
public int $id,
public ?int $deliveryId,
public ?\Carbon\Carbon $dateTimeAdded,
public ?bool $sendEmail,
public int $orderId,
public int $shipperId,
public ?string $barcode,
Expand All @@ -21,7 +23,9 @@ public static function make(array $data): self
{
return new self(
id: $data['Id'] ?? 0,
deliveryId: $data['DeliveryId'] ?? null,
dateTimeAdded: isset($data['DateTimeAdded']) ? \Carbon\Carbon::parse($data['DateTimeAdded']) : null,
sendEmail: $data['SendEmail'] ?? null,
orderId: $data['OrderId'] ?? 0,
shipperId: $data['ShipperId'] ?? 0,
barcode: $data['Barcode'] ?? null,
Expand Down
4 changes: 2 additions & 2 deletions src/Data/V10/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function __construct(
public ?int $orderId,
public ?int $invoiceId,
public float $amountIncl,
public string $description,
public ?string $description,
public int $bookingId,
public int $matchingLedgerId,
public ?\Carbon\Carbon $dateTime,
Expand All @@ -25,7 +25,7 @@ public static function make(array $data): self
orderId: $data['OrderId'] ?? null,
invoiceId: $data['InvoiceId'] ?? null,
amountIncl: $data['AmountIncl'] ?? 0.0,
description: $data['Description'] ?? '',
description: $data['Description'] ?? null,
bookingId: $data['BookingId'] ?? 0,
matchingLedgerId: $data['MatchingLedgerId'] ?? 0,
dateTime: isset($data['DateTime']) ? \Carbon\Carbon::parse($data['DateTime']) : null,
Expand Down
Loading