src/Service/Common/Owner/Render.php line 21
<?php
namespace App\Service\Common\Owner;
use App\Model\Common\GetOwnerInterface;
use Twig\Environment;
class Render
{
private OwnerProvider $provider;
private Environment $environment;
public function __construct(OwnerProvider $provider, Environment $environment)
{
$this->provider = $provider;
$this->environment = $environment;
}
public function render(GetOwnerInterface $item, string $target, array $context = []): ?string
{
$obj = $this->provider->findOwner($item->getOwner());
if (!$obj) {
return null;
}
$renderParams = $obj->getRender();
// dump($renderParams);
if (!array_key_exists($target, $renderParams)) {
throw new \Exception(sprintf("invalid render target %s, owner %s", $target, get_class($obj->getEntity())));
}
switch ($renderParams[$target]['type']) {
case 'template':
{
return $this->environment->render(
$renderParams[$target]['name'],
[
'owner' => $obj->getEntity(),
'item' => $item,
'context' => $context,
]
);
break;
}
}
throw new \Exception(
sprintf(
"can`t render %s, target: %s, owner %s",
$renderParams[$target]['type'],
$target,
get_class($obj->getEntity())
)
);
}
}