- First commit
This commit is contained in:
commit
f5317f906d
9 files changed
+484
No files matched your search
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace Lean\Api;
|
||||
|
||||
use Lean\Factory\HttpFactory;
|
||||
use Notes\ObjectReflection;
|
||||
use Notes\Route\Attribute\Method\Route;
|
||||
use Ulmus\Attribute\Obj\Table;
|
||||
use Ulmus\Attribute\Property\Field;
|
||||
use Ulmus\SearchRequest\Attribute\SearchParameter;
|
||||
use Ulmus\SearchRequest\Attribute\SearchRequestParameter;
|
||||
|
||||
class EntityDescriptor
|
||||
{
|
||||
use DescriptorTrait;
|
||||
|
||||
public string $entityTitle = <<<HTML
|
||||
<div style="margin-left:15px">
|
||||
<h4 class='entity-name'>%s</h4>
|
||||
<div class='description'>%s</div>
|
||||
<hr style="margin-top:15px">
|
||||
<h5>Champs</h5>
|
||||
<ol class='fields'>%s</ol>
|
||||
<hr style="margin-top:15px;">
|
||||
<h4 class='request-name'>%s</h4>
|
||||
<h5>Requêtes</h5>
|
||||
<div class='description' style="margin-bottom:10px">%s</div>
|
||||
<ol class='requests'>%s</ol>
|
||||
</div>
|
||||
HTML;
|
||||
|
||||
public string $entityField = <<<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>Champ SQL</u> : %s</div>
|
||||
<div style="padding:5px"><u>Attribut</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>Taille</u> : %s</div>
|
||||
<div style="padding:5px"><u>Lecture seule</u> : %s</div>
|
||||
</div>
|
||||
</li>
|
||||
HTML;
|
||||
|
||||
public string $searchRequestField = <<<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>Paramètre de requête (GET)</u> : %s</div>
|
||||
<div style="padding:5px"><u>Attribut</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>Valeur par défault</u> : %s</div>
|
||||
</div>
|
||||
</li>
|
||||
HTML;
|
||||
|
||||
|
||||
public function __construct() {}
|
||||
|
||||
/**
|
||||
* @return Returns HTML describing entity fields
|
||||
*/
|
||||
public function describe(...$entities) : string
|
||||
{
|
||||
$html = "";
|
||||
|
||||
foreach($entities as $entity) {
|
||||
$propertyHtml = $searchRequestHtml = "";
|
||||
$entityName = is_object($entity) ? $entity::class : $entity;
|
||||
$reflector = new ObjectReflection($entityName);
|
||||
|
||||
$table = $reflector->reflectClass()->getAttribute(Table::class);
|
||||
|
||||
foreach($reflector->reflectProperties() as $property) {
|
||||
$field = $property->getAttribute(Field::class);
|
||||
|
||||
if ($field) {
|
||||
$types = $property->getTypes();
|
||||
$propertyHtml .= sprintf(
|
||||
$this->entityField,
|
||||
$property->name,
|
||||
$field->object->description,
|
||||
$field->object->name ?: $property->name,
|
||||
$field->tag,
|
||||
$field->object->type ?? implode(' | ', array_map(fn($e) => $e->type, $types)),
|
||||
$this->yesOrNo($property->allowsNull()),
|
||||
$this->length($field->object->length ?? null),
|
||||
$this->yesOrNo($field->object->readonly),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$searchRequestReflector = new ObjectReflection($entityName::searchRequest()::class);
|
||||
|
||||
$searchRequestParameter = $searchRequestReflector->reflectClass()->getAttribute(SearchRequestParameter::class);
|
||||
|
||||
foreach($searchRequestReflector->reflectProperties() as $property) {
|
||||
$field = $property->getAttribute(SearchParameter::class);
|
||||
|
||||
if ($field) {
|
||||
$types = $property->getTypes();
|
||||
|
||||
$searchRequestHtml .= sprintf(
|
||||
$this->searchRequestField,
|
||||
$property->name,
|
||||
$field->object->description,
|
||||
$field->object->parameter ? implode(', ', $field->object->getParameters()) : $property->name,
|
||||
$field->tag,
|
||||
implode(' | ', array_map(fn($e) => $e->type, $types)),
|
||||
$this->yesOrNo($property->allowsNull()),
|
||||
$this->displayValue($property->value ?? "<i>aucune</i>")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$html .= sprintf($this->entityTitle, $reflector->reflectClass()->getClassName(), $table->object->description ?? null, $propertyHtml, $reflector->reflectClass()->getClassName() . "::searchRequest()", $searchRequestParameter->object->description ?? "", $searchRequestHtml);
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user