add entity_to_array twig function
This commit is contained in:
parent
6bf65ccd2b
commit
ff599a7101
1 changed files with 60 additions and 0 deletions
60
src/core/Twig/Extension/EntityExtension.php
Normal file
60
src/core/Twig/Extension/EntityExtension.php
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace App\Core\Twig\Extension;
|
||||
|
||||
use App\Core\Entity\EntityInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\MakerBundle\Str;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccess;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccessor;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class EntityExtension extends AbstractExtension
|
||||
{
|
||||
protected PropertyAccessor $propertyAccessor;
|
||||
|
||||
public function __construct(EntityManagerInterface $entityManager)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
|
||||
->disableExceptionOnInvalidPropertyPath()
|
||||
->getPropertyAccessor()
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('entity_to_array', [$this, 'toArray']),
|
||||
];
|
||||
}
|
||||
|
||||
public function toArray(EntityInterface $entity): array
|
||||
{
|
||||
$metaData = $this->entityManager->getClassMetadata(get_class($entity));
|
||||
$array = [];
|
||||
|
||||
foreach ($metaData->fieldNames as $field) {
|
||||
try {
|
||||
$value = $this->propertyAccessor->getValue($entity, $field);
|
||||
|
||||
if (is_object($value)) {
|
||||
$value = (string) $value;
|
||||
}
|
||||
|
||||
$array[$field] = [
|
||||
'id' => $field,
|
||||
'name' => Str::asHumanWords($field),
|
||||
'value' => $value,
|
||||
];
|
||||
} catch (\Error $e) {
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue