Passed
Push — master ( 4a36e4...d17d8f )
by Asmir
08:07 queued 05:11
created

SerializerExtension::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Twig;
6
7
use JMS\Serializer\SerializationContext;
8
use JMS\Serializer\SerializerInterface;
9
use Twig\TwigFilter;
10
use Twig\TwigFunction;
11
12
/**
13
 * Serializer helper twig extension
14
 *
15
 * Basically provides access to JMSSerializer from Twig
16
 */
17
class SerializerExtension extends SerializerBaseExtension
18
{
19 1
    /**
20
     * @var SerializerInterface
21 1
     */
22
    protected $serializer;
23
24 1
    public function __construct(SerializerInterface $serializer, string $serializationFunctionsPrefix = '')
25
    {
26 1
        $this->serializer = $serializer;
27 1
        parent::__construct($serializationFunctionsPrefix);
28
    }
29 1
30
    /**
31
     * @return TwigFilter[]
32 1
     *
33
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
34
     */
35
    public function getFilters()
36 1
    {
37
        return [
38
            new TwigFilter($this->serializationFunctionsPrefix . 'serialize', [$this, 'serialize']),
39 1
        ];
40
    }
41
42
    /**
43
     * @return TwigFunction[]
44
     *
45
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
46
     */
47
    public function getFunctions()
48 1
    {
49
        return [
50 1
            new TwigFunction($this->serializationFunctionsPrefix . 'serialization_context', '\JMS\Serializer\SerializationContext::create'),
51
        ];
52
    }
53
54
    public function serialize(object $object, string $type = 'json', ?SerializationContext $context = null): string
55
    {
56
        return $this->serializer->serialize($object, $type, $context);
57
    }
58
}
59