65 lines
2.0 KiB
PHP
65 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace Lean\Api;
|
|
|
|
use Lean\Api\Attribute\ContextField;
|
|
use Notes\ObjectReflection;
|
|
|
|
class FormDescriptor
|
|
{
|
|
use DescriptorTrait;
|
|
|
|
public function getForms(...$forms) : array
|
|
{
|
|
$list = [];
|
|
|
|
foreach($forms as $form) {
|
|
$fields = [];
|
|
$formName = is_object($form) ? $form::class : $form;
|
|
$reflector = new ObjectReflection($formName);
|
|
|
|
$context = $reflector->reflectClass()->getAttribute(ContextField::class);
|
|
|
|
foreach($reflector->reflectProperties() as $property) {
|
|
$field = $property->getAttribute(ContextField::class);
|
|
|
|
if ($field && ! $field->object->hidden ) {
|
|
$types = $property->getTypes();
|
|
|
|
$fields[] = [
|
|
'name' => $property->name,
|
|
'description' => $field->object->description,
|
|
'type' => $field->object->type ?? $types,
|
|
'allowNulls' => $property->allowsNull(),
|
|
'regexPattern' => $field->object->regexFormat ?? null,
|
|
'minLength' => $field->object->minLength ?? null,
|
|
'maxLength' => $field->object->maxLength ?? null,
|
|
'example' => $field->object->example ?? null,
|
|
'values' => $this->generateExampleValues($types),
|
|
];
|
|
}
|
|
}
|
|
|
|
$list[$formName] = [
|
|
'className' => $reflector->reflectClass()->getClassName(),
|
|
'description' => $context->object->description ?? null,
|
|
'fields' => $fields,
|
|
];
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
protected function generateExampleValues(array $types) : string|array
|
|
{
|
|
$values = [];
|
|
|
|
foreach($types as $type) {
|
|
if ( enum_exists($type->type) ) {
|
|
$values = array_merge($values, array_map(fn($e) => $e->value, $type->type::cases()));
|
|
}
|
|
}
|
|
|
|
return $values;
|
|
}
|
|
} |