- First commit

This commit is contained in:
Dave Mc Nicoll committed 2024-10-14 15:28:35 +00:00
commit f5317f906d
9 files changed
+484

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
<?php
namespace Lean\Api;
use Lean\Api\Attribute\ContextField;
use Notes\ObjectReflection;
class FormDescriptor
{
use DescriptorTrait;
public string $formTitle = <<<HTML
<div style="margin-left:15px">
<h4 style="background:#e0ecf7" class='form-name'>%s</h4>
<div class='description'>%s</div>
<hr style="margin-top:15px">
<h5>Champs</h5>
<ol class='fields'>%s</ol>
</div>
HTML;
public string $formField = <<<HTML
<li class="odd-even">
<div style="display:flex; justify-content: space-between">
<div><strong style="font-family:monospace">$%s</strong> <span style='margin-left:15px'>%s</span></div>
</div>
<div class="field-desc" style="margin-top:10px;;background:#fff;padding:5px;font-size:0.9em">
<div style="padding:5px"><u>Variable POST / champ JSON</u> : %s</div>
<div style="padding:5px"><u>Type</u> : %s</div>
<div style="padding:5px"><u>Nullable</u> : %s</div>
<div style="padding:5px"><u>Pattern regex</u> : %s</div>
<div style="padding:5px"><u>Taille min.</u> : %s</div>
<div style="padding:5px"><u>Taille max.</u> : %s</div>
<div style="padding:5px"><u>Exemple</u> : %s</div>
</div>
</li>
HTML;
/**
* @return Returns HTML describing given controller's routes
*/
public function describe(...$forms) : string
{
$html = "";
foreach($forms as $form) {
$propertyHtml = "";
$entityName = is_object($form) ? $form::class : $form;
$reflector = new ObjectReflection($entityName);
$context = $reflector->reflectClass()->getAttribute(ContextField::class);
foreach($reflector->reflectProperties() as $property) {
$field = $property->getAttribute(ContextField::class);
if ($field) {
$types = $property->getTypes();
$propertyHtml .= sprintf(
$this->formField,
$property->name,
$field->object->description,
$property->name,
$field->object->type ?? implode(' | ', array_map(fn($e) => $e->type, $types)),
$this->yesOrNo($property->allowsNull()),
$field->object->regexFormat ?? "aucun",
$field->object->minLength ?? "aucune",
$field->object->maxLength ?? "aucune",
$field->object->example
);
}
}
$html .= sprintf($this->formTitle, $reflector->reflectClass()->getClassName(), $context->object->description ?? null, $propertyHtml, );
}
return $html;
}
}