Major fixes, new JSON schema field for types: 'optional'

This commit is contained in:
Sys 2021-06-18 17:13:05 +02:00
parent d69f2f2ad2
commit d406940453
No known key found for this signature in database
GPG Key ID: 3CD2C29F8AB39BFD
2 changed files with 32 additions and 11 deletions

View File

@ -215,10 +215,9 @@ class Generator
ENT_QUOTES ENT_QUOTES
); );
} else { } else {
$parsedData['description'] = htmlspecialchars_decode( $description = htmlspecialchars_decode(strip_tags($fieldData[2]->innerHtml), ENT_QUOTES);
strip_tags($fieldData[2]->innerHtml), $parsedData['optional'] = str_starts_with($description, 'Optional.');
ENT_QUOTES $parsedData['description'] = $description;
);
} }
$parsedFields[] = $parsedData; $parsedFields[] = $parsedData;
} }

View File

@ -41,6 +41,11 @@ class StubCreator
$this->namespace = $namespace; $this->namespace = $namespace;
} }
private static function toCamelCase(string $str): string
{
return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $str))));
}
/** /**
* @param array $fieldTypes * @param array $fieldTypes
* @param PhpNamespace $phpNamespace * @param PhpNamespace $phpNamespace
@ -107,13 +112,23 @@ class StubCreator
$response = $phpNamespace->addClass('Response') $response = $phpNamespace->addClass('Response')
->setType('class'); ->setType('class');
$response->addProperty('ok') $response->addProperty('ok')
->setPublic(); ->setPublic()
->setType(Type::BOOL);
$response->addProperty('result') $response->addProperty('result')
->setPublic(); ->setPublic()
->setType(Type::MIXED)
->setNullable(true)
->setValue(null);
$response->addProperty('error_code') $response->addProperty('error_code')
->setPublic(); ->setPublic()
->setType(Type::INT)
->setNullable(true)
->setValue(null);
$response->addProperty('description') $response->addProperty('description')
->setPublic(); ->setPublic()
->setType(Type::STRING)
->setNullable(true)
->setValue(null);
return [ return [
'Response' => $file 'Response' => $file
]; ];
@ -136,11 +151,17 @@ class StubCreator
$field['types'], $field['types'],
$phpNamespace $phpNamespace
); );
$type_property = $typeClass->addProperty($field['name']) $fieldName = self::toCamelCase($field['name']);
$typeProperty = $typeClass->addProperty($fieldName)
->setPublic() ->setPublic()
->setType($fieldTypes); ->setType($fieldTypes);
if ($field['optional']) {
$typeProperty->setNullable(true)
->setValue(null);
$fieldComments .= '|null';
}
if (!empty($fieldComments)) { if (!empty($fieldComments)) {
$type_property->addComment($fieldComments); $typeProperty->addComment($fieldComments);
} }
} }
$types[$type['name']] = $file; $types[$type['name']] = $file;
@ -187,7 +208,8 @@ class StubCreator
); );
foreach ($fields as $field) { foreach ($fields as $field) {
$types = $this->parseApiFieldTypes($field['types'], $phpNamespace)['types']; $types = $this->parseApiFieldTypes($field['types'], $phpNamespace)['types'];
$parameter = $function->addParameter($field['name']) $fieldName = self::toCamelCase($field['name']);
$parameter = $function->addParameter($fieldName)
->setType($types); ->setType($types);
if (!$field['required']) { if (!$field['required']) {
$parameter->setNullable() $parameter->setNullable()