HashidsExtension::encode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 3
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roukmoute\HashidsBundle\Twig;
6
7
use Hashids\HashidsInterface;
8
use Twig\Extension\AbstractExtension;
9
use Twig\TwigFilter;
10
11
class HashidsExtension extends AbstractExtension
12
{
13
    private HashidsInterface $hashids;
14
15
    public function __construct(HashidsInterface $hashids)
16
    {
17
        $this->hashids = $hashids;
18
    }
19
20
    public function getFilters(): array
21
    {
22
        return [
23
            new TwigFilter('hashids_encode', [$this, 'encode']),
24
            new TwigFilter('hashids_decode', [$this, 'decode']),
25
        ];
26
    }
27
28
    public function encode(int $number): string
29
    {
30
        return $this->hashids->encode($number);
31
    }
32
33
    /**
34
     * @return array<int, ?int>
35
     */
36
    public function decode(string $hash): array
37
    {
38
        return $this->hashids->decode($hash);
39
    }
40
}
41