DecrementTag   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 43
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A render() 0 16 3
1
<?php
2
3
/**
4
 * Platine Template
5
 *
6
 * Platine Template is a template engine that has taken a lot of inspiration from Django.
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Template
11
 * Copyright (c) 2014 Guz Alexander, http://guzalexander.com
12
 * Copyright (c) 2011, 2012 Harald Hanek, http://www.delacap.com
13
 * Copyright (c) 2006 Mateo Murphy
14
 *
15
 * Permission is hereby granted, free of charge, to any person obtaining a copy
16
 * of this software and associated documentation files (the "Software"), to deal
17
 * in the Software without restriction, including without limitation the rights
18
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
 * copies of the Software, and to permit persons to whom the Software is
20
 * furnished to do so, subject to the following conditions:
21
 *
22
 * The above copyright notice and this permission notice shall be included in all
23
 * copies or substantial portions of the Software.
24
 *
25
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
 * SOFTWARE.
32
 */
33
34
/**
35
 *  @file DecrementTag.php
36
 *
37
 *  The "decrement" Template tag class
38
 *
39
 *  @package    Platine\Template\Tag
40
 *  @author Platine Developers Team
41
 *  @copyright  Copyright (c) 2020
42
 *  @license    http://opensource.org/licenses/MIT  MIT License
43
 *  @link   https://www.platine-php.com
44
 *  @version 1.0.0
45
 *  @filesource
46
 */
47
48
declare(strict_types=1);
49
50
namespace Platine\Template\Tag;
51
52
use Platine\Template\Exception\ParseException;
53
use Platine\Template\Parser\AbstractTag;
54
use Platine\Template\Parser\Context;
55
use Platine\Template\Parser\Lexer;
56
use Platine\Template\Parser\Parser;
57
use Platine\Template\Parser\Token;
58
59
/**
60
 * @class DecrementTag
61
 * @package Platine\Template\Tag
62
 */
63
class DecrementTag extends AbstractTag
64
{
65
    /**
66
     * Name of the variable to decrement
67
     * @var string
68
     */
69
    protected string $name;
70
71
    /**
72
    * {@inheritdoc}
73
    */
74
    public function __construct(string $markup, array &$tokens, Parser $parser)
75
    {
76
        $lexer = new Lexer('/(' . Token::VARIABLE_NAME . ')/');
77
        if ($lexer->match($markup)) {
78
            $this->name = $lexer->getStringMatch(0);
79
        } else {
80
            throw new ParseException(sprintf(
81
                'Syntax Error in "%s" - Valid syntax: decrement [var]',
82
                'decrement'
83
            ));
84
        }
85
    }
86
87
    /**
88
    * {@inheritdoc}
89
    */
90
    public function render(Context $context): string
91
    {
92
        // if the value is not set in the environment check to see if it
93
        // exists in the context, and if not set it to 0
94
        if ($context->hasEnvironment($this->name) === false) {
95
            // check for a context value
96
            $fromContext = $context->get($this->name);
97
98
            $context->setEnvironment($this->name, $fromContext ? $fromContext : 0);
99
        }
100
        // decrement the environment value
101
        $currentValue = $context->getEnvironment($this->name);
102
103
        $context->setEnvironment($this->name, --$currentValue);
104
105
        return '';
106
    }
107
}
108