-
-
Save weaverryan/61f09456bcdace4fb857b55106eba439 to your computer and use it in GitHub Desktop.
Serializer Extension from Vue part 2 Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Twig; | |
use ApiPlatform\Core\Api\IriConverterInterface; | |
use Twig\Extension\AbstractExtension; | |
use Twig\TwigFilter; | |
use Symfony\Component\Serializer\SerializerInterface; | |
class SerializerExtension extends AbstractExtension | |
{ | |
private $serializer; | |
private $iriConverter; | |
public function __construct(SerializerInterface $serializer, IriConverterInterface $iriConverter) | |
{ | |
$this->serializer = $serializer; | |
$this->iriConverter = $iriConverter; | |
} | |
public function getFilters(): array | |
{ | |
return [ | |
new TwigFilter('jsonld', [$this, 'serializeToJsonLd'], ['is_safe' => ['html']]), | |
new TwigFilter('iri', [$this, 'convertToIri'], ['is_safe' => ['html']]), | |
]; | |
} | |
public function serializeToJsonLd($data): string | |
{ | |
return $this->serializer->serialize($data, 'jsonld'); | |
} | |
public function convertToIri(object $item): string | |
{ | |
return $this->iriConverter->getIriFromItem($item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment