ThumbnailTokenParser::parse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Twig\TokenParser;
15
16
use Sonata\MediaBundle\Twig\Node\ThumbnailNode;
17
use Twig\Node\Expression\ArrayExpression;
18
use Twig\Token;
19
use Twig\TokenParser\AbstractTokenParser;
20
21
/**
22
 * @final since sonata-project/media-bundle 3.21.0
23
 */
24
class ThumbnailTokenParser extends AbstractTokenParser
25
{
26
    /**
27
     * @var string
28
     */
29
    protected $extensionName;
30
31
    /**
32
     * @param string $extensionName
33
     */
34
    public function __construct($extensionName)
35
    {
36
        $this->extensionName = $extensionName;
37
    }
38
39
    public function parse(Token $token)
40
    {
41
        $media = $this->parser->getExpressionParser()->parseExpression();
42
43
        $this->parser->getStream()->next();
44
45
        $format = $this->parser->getExpressionParser()->parseExpression();
46
47
        // attributes
48
        if ($this->parser->getStream()->test(Token::NAME_TYPE, 'with')) {
49
            $this->parser->getStream()->next();
50
51
            $attributes = $this->parser->getExpressionParser()->parseExpression();
52
        } else {
53
            $attributes = new ArrayExpression([], $token->getLine());
54
        }
55
56
        $this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
57
58
        return new ThumbnailNode($this->extensionName, $media, $format, $attributes, $token->getLine(), $this->getTag());
59
    }
60
61
    public function getTag()
62
    {
63
        return 'thumbnail';
64
    }
65
}
66