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