<?php
namespace EADPlataforma\Util;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use EADPlataforma\Services\GeneralService;
use \ReflectionClass;
use \ReflectionProperty;
use \Exception;
class EntityUtil{
/**
*
* @var Entity
*
*/
private $entity;
/**
* @var SchoolEntityManager
*/
protected $em;
/**
*
* @var ValidatorInterface $validator
*
*/
private $validator;
/**
* Constructor
*
* @param GeneralService $generalService
* @param ValidatorInterface $validator
*/
public function __construct(GeneralService $generalService, ValidatorInterface $validator)
{
$this->em = $generalService->getService('SchoolEntityManager');
$this->validator = $validator;
}
public function setEntity($entity)
{
$this->entity = $entity;
return $this;
}
public function json() {
if($this->entity){
$encoders = [new JsonEncoder()];
$normalizers = [new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);
$jsonContent = $serializer->serialize($this->entity, 'json', [
'circular_reference_handler' => function ($object) {
return $object->getId();
}
]);
return $jsonContent;
}else{
throw new Exception("Entity not found", 1);
}
}
public function getClassName($onlyName = false)
{
if($this->entity){
$className = get_class($this->entity);
if($onlyName){
$className = explode("\\", $className);
$className = end($className);
}
return $className;
}else{
throw new Exception("Entity not found", 1);
}
}
public function getTableName()
{
if($this->entity){
return $this->em->getClassMetadata($this->getClassName())->getTableName();
}else{
throw new Exception("Entity not found", 1);
}
}
public function getFieldsName()
{
if($this->entity){
$reflection = new ReflectionClass($this->entity);
$entityProperties = $reflection->getProperties(ReflectionProperty::IS_PRIVATE);
$fields = [];
foreach ($entityProperties as $key => $property) {
$propName = $property->getName();
$fields[] = $propName;
}
return $fields;
}else{
throw new Exception("Entity not found", 1);
}
}
public function setAllFields(array $data)
{
if($this->entity){
foreach ($data as $property => $value) {
if (property_exists($this->entity, $property)) {
$setFunc = "set" . ucfirst($property);
if(method_exists($this->entity, $setFunc)){
$this->entity->{$setFunc}($value);
}
}
}
return $this->entity;
}else{
throw new Exception("Entity not found", 1);
}
}
public function validateEntity($groups = [])
{
$groups[] = "Default";
$errors = $this->validator->validate($this->entity, null, $groups);
if (count($errors) > 0) {
$fields = [];
$className = $this->getClassName(true);
foreach ($errors as $violation) {
$fieldName = $violation->getPropertyPath();
$fieldArray = explode('.', $fieldName);
if(count($fieldArray) == 1){
$fieldName = array_shift($fieldArray);
$fields[] = $fieldName;
}
}
$fields = array_unique($fields);
sort($fields);
return $fields;
}
return false;
}
}