-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUuidTwigExtension.php
69 lines (62 loc) · 1.5 KB
/
UuidTwigExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
declare(strict_types=1);
namespace App\Utils\Templating;
use App\Utils\Traits\UuidHelperTrait;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
/*
* Class UuidTwigExtension.
*
* Create a Twig filter extension to encode uuid directly in template.
*
* @see encoded uuid inspired from:
* https://medium.com/@galopintitouan/auto-increment-is-the-devil-using-uuids-in-symfony-and-doctrine-71763721b9a9
*/
class UuidTwigExtension extends AbstractExtension
{
use UuidHelperTrait;
/**
* Get Twig filter.
*
* @return array
*/
public function getFilters(): array
{
return [
new TwigFilter(
'uuid_encode',
[$this, 'encodeUuid'],
['is_safe' => ['html']]
),
new TwigFilter(
'uuid_from_string',
[$this, 'getUuidFromString'],
['is_safe' => ['html']]
),
];
}
/**
* Encode a uuid as string.
*
* @param UuidInterface $uuid
*
* @return string
*/
public function encodeUuid(UuidInterface $uuid): string
{
return $this->encode($uuid);
}
/**
* Get a UuidInterface implementation instance from a string representation.
*
* @param string $uuid
*
* @return UuidInterface
*/
public function getUuidFromString(string $uuid): UuidInterface
{
return Uuid::fromString($uuid);
}
}