|
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 CycleTag.php |
|
36
|
|
|
* |
|
37
|
|
|
* The "cycle" 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 CycleTag |
|
61
|
|
|
* @package Platine\Template\Tag |
|
62
|
|
|
*/ |
|
63
|
|
|
class CycleTag extends AbstractTag |
|
64
|
|
|
{ |
|
65
|
|
|
/** |
|
66
|
|
|
* The name of the cycle; if none is given one |
|
67
|
|
|
* is created using the value list |
|
68
|
|
|
* @var string |
|
69
|
|
|
*/ |
|
70
|
|
|
protected string $name; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* The variables to cycle between |
|
74
|
|
|
* @var array<int, string> |
|
75
|
|
|
*/ |
|
76
|
|
|
protected array $variables = []; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
public function __construct(string $markup, array &$tokens, Parser $parser) |
|
82
|
|
|
{ |
|
83
|
|
|
$lexerSimple = new Lexer('/' . Token::QUOTED_FRAGMENT . '/'); |
|
84
|
|
|
$lexerNamed = new Lexer('/(' . Token::QUOTED_FRAGMENT . ')\s*\:\s*(.*)/'); |
|
85
|
|
|
if ($lexerNamed->match($markup)) { |
|
86
|
|
|
$this->variables = $this->variablesFromString($lexerNamed->getStringMatch(2)); |
|
87
|
|
|
$this->name = $lexerNamed->getStringMatch(1); |
|
88
|
|
|
} elseif ($lexerSimple->match($markup)) { |
|
89
|
|
|
$this->variables = $this->variablesFromString($markup); |
|
90
|
|
|
$this->name = "'" . implode('', $this->variables) . "'"; |
|
91
|
|
|
} else { |
|
92
|
|
|
throw new ParseException(sprintf( |
|
93
|
|
|
'Syntax Error in "%s" - Valid syntax: cycle [name :] var [, var2, var3 ...]', |
|
94
|
|
|
'cycle' |
|
95
|
|
|
)); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function render(Context $context): string |
|
103
|
|
|
{ |
|
104
|
|
|
$context->push(); |
|
105
|
|
|
|
|
106
|
|
|
$iteration = 0; |
|
107
|
|
|
|
|
108
|
|
|
$key = $context->get($this->name); |
|
109
|
|
|
if ($context->hasRegister('cycle')) { |
|
110
|
|
|
$cycle = $context->getRegister('cycle'); |
|
111
|
|
|
if (isset($cycle[$key])) { |
|
112
|
|
|
$iteration = $cycle[$key]; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$result = $context->get($this->variables[$iteration]); |
|
117
|
|
|
|
|
118
|
|
|
if ($result === null) { |
|
119
|
|
|
$result = ''; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$iteration++; |
|
123
|
|
|
|
|
124
|
|
|
if ($iteration >= count($this->variables)) { |
|
125
|
|
|
$iteration = 0; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$context->setRegister('cycle', [$key => $iteration]); |
|
129
|
|
|
$context->pop(); |
|
130
|
|
|
|
|
131
|
|
|
return $result; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $markup |
|
137
|
|
|
* @return array<int,string> |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function variablesFromString(string $markup): array |
|
140
|
|
|
{ |
|
141
|
|
|
$lexer = new Lexer('/\s*(' . Token::QUOTED_FRAGMENT . ')\s*/'); |
|
142
|
|
|
$parts = explode(',', $markup); |
|
143
|
|
|
$result = []; |
|
144
|
|
|
foreach ($parts as $part) { |
|
145
|
|
|
$lexer->match($part); |
|
146
|
|
|
|
|
147
|
|
|
if ($lexer->getStringMatch(1)) { |
|
148
|
|
|
$result[] = $lexer->getStringMatch(1); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $result; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|