<?php
namespace App\Services\Api;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
class BaseApiManager
{
protected $em;
protected $security;
protected $translator;
function __construct(
EntityManagerInterface $em,
Security $security,
TranslatorInterface $translator
) {
$this->em = $em;
$this->security = $security;
$this->translator = $translator;
}
protected function setValues($item, $data)
{
foreach ($data as $ind => $d) {
if ($d instanceof UploadedFile)
$ind .= "File";
$attr = "set" . (ucfirst($ind));
if (method_exists($item, $attr))
$item->$attr($d);
$class = ucfirst($ind);
if (is_array($d)) {
$attr = "add" . ($class);
} else {
$attr = "set" . ($class);
}
if (method_exists($item, $attr)) {
if (is_array($d)) {
foreach ($d as $t) {
$className = "App\\Entity\\{$class}";
$aux = $this->em->getRepository($className)->find(['id' => $t['id']]);
$item->$attr($aux);
}
} else {
$item->$attr($d);
}
}
}
return $item;
}
public function filterNotRemove($class, ?array $filter = [])
{
$filter['deletedAt'] = NULL;
return $this->em->getRepository($class)->findBy($filter);
}
}