HashidsExtension   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 28
rs 10
c 5
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A decode() 0 3 1
A __construct() 0 3 1
A getFilters() 0 5 1
A encode() 0 3 1
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