1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2013-2017 |
4
|
|
|
* |
5
|
|
|
* @category Library |
6
|
|
|
* @package Dwoo\Plugins\Functions |
7
|
|
|
* @author Jordi Boggiano <[email protected]> |
8
|
|
|
* @author David Sanchez <[email protected]> |
9
|
|
|
* @copyright 2008-2013 Jordi Boggiano |
10
|
|
|
* @copyright 2013-2017 David Sanchez |
11
|
|
|
* @license http://dwoo.org/LICENSE LGPLv3 |
12
|
|
|
* @version 1.3.6 |
13
|
|
|
* @date 2017-03-21 |
14
|
|
|
* @link http://dwoo.org/ |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Dwoo\Plugins\Functions; |
18
|
|
|
|
19
|
|
|
use Dwoo\Compiler; |
20
|
|
|
use Dwoo\Core; |
21
|
|
|
use Dwoo\Exception; |
22
|
|
|
use Dwoo\Compilation\Exception as CompilationException; |
23
|
|
|
use Dwoo\ICompilable; |
24
|
|
|
use Dwoo\Plugin; |
25
|
|
|
use Dwoo\Plugins\Blocks\PluginIf; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Ternary if operation. |
29
|
|
|
* It evaluates the first argument and returns the second if it's true, or the third if it's false |
30
|
|
|
* <pre> |
31
|
|
|
* * rest : you can not use named parameters to call this, use it either with three arguments in the correct order |
32
|
|
|
* (expression, true result, false result) or write it as in php (expression ? true result : false result) |
33
|
|
|
* </pre> |
34
|
|
|
* This software is provided 'as-is', without any express or implied warranty. |
35
|
|
|
* In no event will the authors be held liable for any damages arising from the use of this software. |
36
|
|
|
*/ |
37
|
|
|
class PluginTif extends Plugin implements ICompilable |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @param Compiler $compiler |
41
|
|
|
* @param array $rest |
42
|
|
|
* @param array $tokens |
43
|
|
|
* |
44
|
|
|
* @return mixed|string |
45
|
|
|
* @throws CompilationException |
46
|
|
|
*/ |
47
|
|
|
public static function compile(Compiler $compiler, array $rest, array $tokens) |
48
|
|
|
{ |
49
|
|
|
// load if plugin |
50
|
|
|
if (!class_exists(Core::NAMESPACE_PLUGINS_BLOCKS . 'PluginIf')) { |
51
|
|
|
try { |
52
|
|
|
$compiler->getCore()->getLoader()->loadPlugin('if'); |
53
|
|
|
} |
54
|
|
|
catch (Exception $e) { |
55
|
|
|
throw new CompilationException($compiler, 'Tif: the if plugin is required to use Tif'); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (count($rest) == 1) { |
60
|
|
|
return $rest[0]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// fetch false result and remove the ":" if it was present |
64
|
|
|
$falseResult = array_pop($rest); |
65
|
|
|
|
66
|
|
|
if (trim(end($rest), '"\'') === ':') { |
67
|
|
|
// remove the ':' if present |
68
|
|
|
array_pop($rest); |
69
|
|
|
} elseif (trim(end($rest), '"\'') === '?' || count($rest) === 1) { |
70
|
|
|
if ($falseResult === '?' || $falseResult === ':') { |
71
|
|
|
throw new CompilationException($compiler, |
72
|
|
|
'Tif: incomplete tif statement, value missing after ' . $falseResult); |
73
|
|
|
} |
74
|
|
|
// there was in fact no false result provided, so we move it to be the true result instead |
75
|
|
|
$trueResult = $falseResult; |
76
|
|
|
$falseResult = "''"; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// fetch true result if needed |
80
|
|
|
if (!isset($trueResult)) { |
81
|
|
|
$trueResult = array_pop($rest); |
82
|
|
|
// no true result provided so we use the expression arg |
83
|
|
|
if ($trueResult === '?') { |
84
|
|
|
$trueResult = true; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// remove the '?' if present |
89
|
|
|
if (trim(end($rest), '"\'') === '?') { |
90
|
|
|
array_pop($rest); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// check params were correctly provided |
94
|
|
|
if (empty($rest) || $trueResult === null || $falseResult === null) { |
95
|
|
|
throw new CompilationException($compiler, |
96
|
|
|
'Tif: you must provide three parameters serving as <expression> ? <true value> : <false value>'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// parse condition |
100
|
|
|
$condition = PluginIf::replaceKeywords($rest, $tokens, $compiler); |
101
|
|
|
|
102
|
|
|
return '((' . implode(' ', $condition) . ') ? ' . ($trueResult === true ? implode(' ', |
103
|
|
|
$condition) : $trueResult) . ' : ' . $falseResult . ')'; |
104
|
|
|
} |
105
|
|
|
} |