1
|
|
|
<?php |
2
|
|
|
/* @description Transformation Style Sheets - Revolutionising PHP templating * |
3
|
|
|
* @author Tom Butler [email protected] * |
4
|
|
|
* @copyright 2017 Tom Butler <[email protected]> | https://r.je/ * |
5
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License * |
6
|
|
|
* @version 1.2 */ |
7
|
|
|
namespace Transphporm\Parser; |
8
|
|
|
/** Parses "string" and function(args) e.g. data(foo) or iteration(bar) */ |
9
|
|
|
class Last { |
10
|
|
|
private $autoLookup; |
11
|
|
|
/* |
12
|
|
|
Stores the last value e.g. |
13
|
|
|
"a" + "b" |
14
|
|
|
Will store "a" before reading the token for the + and perfoming the concatenate operation |
15
|
|
|
*/ |
16
|
|
|
private $last; |
17
|
|
|
private $data; |
18
|
|
|
private $result; |
19
|
|
|
private $traversing = false; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
public function __construct($data, $result, $autoLookup) { |
23
|
|
|
$this->data = $data; |
24
|
|
|
$this->result = $result; |
25
|
|
|
$this->autoLookup = $autoLookup; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
public function traverse() { |
30
|
|
|
return $this->data->traverse($this->last, $this->result); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function clear() { |
34
|
|
|
$this->last = null; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function isEmpty() { |
38
|
|
|
return $this->last === null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function processNested($parser, $token) { |
42
|
|
|
$funcResult = $this->data->parseNested($parser, $token, $this->last); |
43
|
|
|
$this->result->processValue($funcResult); |
44
|
|
|
$this->clear(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function read() { |
48
|
|
|
return $this->last; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function set($value) { |
52
|
|
|
$this->last = $value; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function makeTraversing() { |
56
|
|
|
$this->traversing = true; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
//Applies the current operation to whatever is in $last based on $mode |
60
|
|
|
public function process() { |
61
|
|
|
if ($this->last !== null) { |
62
|
|
|
try { |
63
|
|
|
$value = $this->data->extract($this->last, $this->autoLookup, $this->traversing); |
64
|
|
|
$this->result->processValue($value); |
65
|
|
|
} |
66
|
|
|
catch (\UnexpectedValueException $e) { |
67
|
|
|
$this->processLastUnexpected(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function processLastUnexpected() { |
73
|
|
|
if (!($this->autoLookup || $this->traversing)) { |
74
|
|
|
$this->result->processValue($this->last); |
75
|
|
|
} |
76
|
|
|
else { |
77
|
|
|
$this->result->clear(); |
78
|
|
|
$this->result->processValue(false); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|