AbstractTag::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 6
rs 10
c 1
b 0
f 0
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 AbstractTag.php
36
 *
37
 *  The base Template tag class
38
 *
39
 *  @package    Platine\Template\Parser
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\Parser;
51
52
use ReflectionClass;
53
54
/**
55
 * @class AbstractTag
56
 * @package Platine\Template\Parser
57
 */
58
abstract class AbstractTag
59
{
60
    /**
61
     * The name of this class tag
62
     * @var string
63
     */
64
    protected string $name;
65
66
    /**
67
     * The tag markup
68
     * @var string
69
     */
70
    protected string $markup;
71
72
    /**
73
     * The parser instance to use
74
     * @var Parser
75
     */
76
    protected Parser $parser;
77
78
    /**
79
     * The list of tag attributes
80
     * @var array<string, mixed>
81
     */
82
    protected array $attributes = [];
83
84
    /**
85
     * Create new instance
86
     * @param string $markup
87
     * @param array<int, string> $tokens
88
     * @param Parser $parser
89
     */
90
    public function __construct(string $markup, array &$tokens, Parser $parser)
91
    {
92
        $this->markup = $markup;
93
        $this->parser = $parser;
94
95
        $this->parse($tokens);
96
    }
97
98
    /**
99
     * Parse the token
100
     * @param array<int, string> $tokens
101
     * @return void
102
     */
103
    public function parse(array &$tokens): void
104
    {
105
        //Do nothing now
106
    }
107
108
    /**
109
     * Render the tag with the given context.
110
     * @param Context $context
111
     * @return string
112
     */
113
    abstract public function render(Context $context): string;
114
115
    /**
116
     * Extracts tag attributes from a markup string.
117
     * @param string $markup
118
     * @return void
119
     */
120
    protected function extractAttributes(string $markup): void
121
    {
122
        $this->attributes = [];
123
124
        $lexer = new Lexer(Token::TAG_ATTRIBUTES);
125
126
        $matches = $lexer->scan($markup);
127
128
        foreach ($matches as $match) {
129
            $this->attributes[$match[0]] = $match[1];
130
        }
131
    }
132
133
    /**
134
     * Return the name of this tag
135
     * @return string
136
     */
137
    protected function getTagName(): string
138
    {
139
        if (!empty($this->name)) {
140
            return $this->name;
141
        }
142
143
        $reflection = new ReflectionClass($this);
144
145
        return str_replace('tag', '', strtolower($reflection->getShortName()));
146
    }
147
148
    /**
149
    * Returns the class name of the tag.
150
    *
151
    * @return string
152
    */
153
    protected function getName(): string
154
    {
155
        return strtolower(get_class($this));
156
    }
157
}
158