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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
php-version: '8.3'
coverage: pcov

- name: Validate composer.json
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/update-endpoints.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
php-version: '8.3'

- name: Validate composer.json
run: composer validate
Expand Down
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM ${DEPENDENCY_PROXY}composer:latest AS composer
FROM ${DEPENDENCY_PROXY}php:8.1-fpm-alpine
FROM ${DEPENDENCY_PROXY}php:8.3-fpm-alpine

ARG PUID=1000
ARG PGID=1000
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "library",
"license": "MIT",
"require": {
"php": "^8.1",
"php": "^8.3",
"guzzlehttp/guzzle": "^7.8",
"nesbot/carbon": "^2.72|^3.8",
"psr/simple-cache": "^3.0"
Expand All @@ -14,15 +14,15 @@
"friendsofphp/php-cs-fixer": "^3.35",
"mockery/mockery": "^1.6",
"nette/php-generator": "^4.1",
"orchestra/testbench": "^8.13",
"orchestra/testbench": "^10.0",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-mockery": "^1.1",
"phpstan/phpstan-phpunit": "^1.3",
"phpunit/phpunit": "^10.4",
"phpunit/phpunit": "^11.5",
"rector/rector": "^1.1.1",
"rregeer/phpunit-coverage-check": "^0.3.1",
"symfony/var-dumper": "^6.3",
"symfony/var-dumper": "^7.2",
"webmozart/assert": "^1.12"
},
"autoload": {
Expand Down
11 changes: 8 additions & 3 deletions generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@

use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\ErrorHandler\ErrorHandler;
use Webparking\Logic4Client\Generator\Generator;

require_once __DIR__.'/vendor/autoload.php';

Debug::enable();
ErrorHandler::register();

$versions = Webparking\Logic4Client\Generator\Generator::resolveVersions();

$refresh = in_array('--refresh', $argv ?? [], true);

$versions = array_filter(
Generator::resolveVersions(),
static fn (string $version): bool => Generator::GENERATED_MAJOR_VERSION === explode('.', $version, 2)[0],
\ARRAY_FILTER_USE_KEY
);

foreach ($versions as $version => $url) {
echo "Generating API namespace $version...\n";

$generator = new Webparking\Logic4Client\Generator\Generator();
$generator = new Generator();
$generator->setRefresh($refresh);
$generator->generate($version, $url);
}
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
16 changes: 10 additions & 6 deletions generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class Generator
{
public const string GENERATED_MAJOR_VERSION = '3';

public static string $scalarUrl = 'https://api.logic4server.nl/scalar/';
public static string $baseUrl = 'https://api.logic4server.nl/';
public string $localApi = __DIR__.'/../logic4-api-%s.json';
Expand Down Expand Up @@ -54,7 +56,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 +136,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 +181,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);
}
}
35 changes: 32 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 Expand Up @@ -142,6 +163,14 @@ public function addMethod(string $httpMethod, string $uri, Operation $operation,
PHP
);
}
} elseif ('string' === $returnType) {
$method->setBody(
<<<PHP
return \$this->buildStringResponse(
\$this->getClient()->{$httpMethod}('{$uri}'{$parametersPhp}),
);
PHP
);
} else {
$method->setBody(
<<<PHP
Expand Down
23 changes: 23 additions & 0 deletions src/Data/V30/AddressType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Webparking\Logic4Client\Data\V30;

class AddressType
{
public function __construct(
public int $id,
public ?string $name,
) {
}

/** @param array<mixed> $data */
public static function make(array $data): self
{
return new self(
id: $data['Id'] ?? 0,
name: $data['Name'] ?? null,
);
}
}
23 changes: 23 additions & 0 deletions src/Data/V30/BarcodeWithAmount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Webparking\Logic4Client\Data\V30;

class BarcodeWithAmount
{
public function __construct(
public int $qty,
public ?string $barcode,
) {
}

/** @param array<mixed> $data */
public static function make(array $data): self
{
return new self(
qty: $data['Qty'] ?? 0,
barcode: $data['Barcode'] ?? null,
);
}
}
Loading