mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 13:54:23 +00:00
111 lines
3.0 KiB
PHP
111 lines
3.0 KiB
PHP
<?php
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* Copyright (C) 2019, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
namespace Chill\MainBundle\CRUD\Resolver;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\PropertyAccess\PropertyAccess;
|
|
|
|
/**
|
|
*
|
|
*
|
|
*/
|
|
class Resolver
|
|
{
|
|
/**
|
|
*
|
|
* @var EntityManagerInterface
|
|
*/
|
|
protected $em;
|
|
|
|
/**
|
|
*
|
|
* @var \Symfony\Component\PropertyAccess\PropertyAccessor
|
|
*/
|
|
protected $propertyAccess;
|
|
|
|
function __construct(EntityManagerInterface $em)
|
|
{
|
|
$this->em = $em;
|
|
|
|
$this->buildPropertyAccess();
|
|
}
|
|
|
|
|
|
private function buildPropertyAccess()
|
|
{
|
|
$this->propertyAccess = PropertyAccess::createPropertyAccessorBuilder()
|
|
->enableExceptionOnInvalidIndex()
|
|
->getPropertyAccessor();
|
|
}
|
|
|
|
/**
|
|
* Return the data at given path.
|
|
*
|
|
* Path are given to
|
|
*
|
|
* @param object $entity
|
|
* @param string $path
|
|
*/
|
|
public function getData($entity, $path)
|
|
{
|
|
return $this->propertyAccess->getValue($entity, $path);
|
|
}
|
|
|
|
public function getTwigTemplate($entity, $path): string
|
|
{
|
|
list($focusEntity, $subPath) = $this->getFocusedEntity($entity, $path);
|
|
|
|
$classMetadata = $this->em->getClassMetadata(\get_class($focusEntity));
|
|
$type = $classMetadata->getTypeOfField($subPath);
|
|
dump($type);
|
|
switch ($type) {
|
|
|
|
default:
|
|
return '@ChillMain/CRUD/_inc/default.html.twig';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the object on which the path apply
|
|
*
|
|
* This methods recursively parse the path and entity and return the entity
|
|
* which will deliver the info, and the last path.
|
|
*
|
|
* @param object $entity
|
|
* @param string $path
|
|
* @return array [$focusedEntity, $lastPath]
|
|
*/
|
|
private function getFocusedEntity($entity, $path)
|
|
{
|
|
if (\strpos($path, '.') === FALSE) {
|
|
return [$entity, $path];
|
|
}
|
|
|
|
$exploded = \explode('.', $path);
|
|
|
|
$subEntity = $this->propertyAccess
|
|
->getValue($entity, $exploded[0]);
|
|
|
|
return $this->getFocusedEntity($subEntity,
|
|
\implode('.', \array_slice($exploded, 1)));
|
|
}
|
|
}
|