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