Completed
Push — master ( 0b7883...3269d2 )
by Tom
02:16
created

Value::callTransphpormFunctions()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.2
cc 4
eloc 7
nc 4
nop 1
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 Value {
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
	private $tokenFuncs = [
23
			Tokenizer::NOT => 'processComparator',
24
			Tokenizer::EQUALS => 'processComparator',
25
			Tokenizer::DOT => 'processDot',
26
			Tokenizer::OPEN_SQUARE_BRACKET => 'processSquareBracket',
27
			Tokenizer::ARG => 'processSeparator',
28
			Tokenizer::CONCAT => 'processSeparator',
29
			Tokenizer::SUBTRACT => 'processSeparator',
30
			Tokenizer::MULTIPLY => 'processSeparator',
31
			Tokenizer::DIVIDE => 'processSeparator',
32
			Tokenizer::NAME => 'processScalar',
33
			Tokenizer::NUMERIC => 'processString',
34
			Tokenizer::BOOL => 'processString',
35
			Tokenizer::STRING => 'processString',
36
			Tokenizer::OPEN_BRACKET => 'processBrackets'
37
	];
38
39
	public function __construct($data, $autoLookup = false) {
40
		$this->baseData = $data;
41
		$this->autoLookup = $autoLookup;
42
	}
43
44
	public function parse($str) {
45
		$tokenizer = new Tokenizer($str);
46
		$tokens = $tokenizer->getTokens();
47
		$this->result = $this->parseTokens($tokens, $this->baseData);
48
		return $this->result;
49
	}
50
51
	public function parseTokens($tokens, $data = null) {
52
		$this->result = new ValueResult;
53
		$this->data = new ValueData($data ? $data : $this->baseData);
54
		$this->last = null;
55
		$this->traversing = false;
56
57
		if (count($tokens) <= 0) return [$data];
58
59
		foreach (new TokenFilterIterator($tokens, [Tokenizer::WHITESPACE, Tokenizer::NEW_LINE]) as $token) {
60
			$this->{$this->tokenFuncs[$token['type']]}($token);
61
		}
62
63
		$this->processLast();
64
		return $this->result->getResult();
65
	}
66
67
	private function processComparator($token) {
68
		$this->processLast();
69
70
		if (!(in_array($this->result->getMode(), array_keys($this->tokenFuncs, 'processComparator')) && $token['type'] == Tokenizer::EQUALS)) {
71
			$this->result->setMode($token['type']);
72
			$this->last = null;
73
		}
74
	}
75
76
	//Reads the last selected value from $data regardless if it's an array or object and overrides $this->data with the new value
77
	//Dot moves $data to the next object in $data foo.bar moves the $data pointer from `foo` to `bar`
78
	private function processDot($token) {
0 ignored issues
show
Unused Code introduced by
The parameter $token is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
		if ($this->last !== null) $this->data->traverse($this->last);
80
		else {
81
			//When . is not preceeded by anything, treat it as part of the string instead of an operator
82
			// foo.bar is treated as looking up `bar` in `foo` whereas .foo is treated as the string ".foo"
83
			$lastResult = $this->result->pop();
84
			if ($lastResult) {
85
				$this->data = new ValueData($lastResult);
86
				$this->traversing = true;
87
			}
88
			else {
89
				$this->processString(['value' => '.']);
90
				$this->result->setMode(Tokenizer::CONCAT);
91
			}
92
		}
93
94
		$this->last = null;
95
	}
96
97
	private function hasFunction($name) {
98
		return $this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($name);
99
	}
100
101
	private function processSquareBracket($token) {
102
		$parser = new Value($this->baseData, $this->autoLookup);
103
		if ($this->hasFunction($this->last)) {
104
			$this->callTransphpormFunctions($token);
105
		}
106
		else {
107
			if ($this->last !== null) $this->data->traverse($this->last);
108
			else {
109
				$lastResult = $this->result->pop();
110
				if ($lastResult) $this->data = new ValueData($lastResult);
111
			}
112
			$this->last = $parser->parseTokens($token['value'], null)[0];
113
			if (!is_bool($this->last)) $this->traversing = true;
114
		}
115
	}
116
117
	private function processSeparator($token) {
118
		$this->result->setMode($token['type']);
119
	}
120
121
	private function processScalar($token) {
122
		$this->last = $token['value'];
123
	}
124
125
	private function processString($token) {
126
		$this->result->processValue($token['value']);
127
	}
128
129
	private function processBrackets($token) {
130
		if ($this->hasFunction($this->last)
131
			&& !$this->data->methodExists($this->last)) {
132
			$this->callTransphpormFunctions($token);
133
		}
134
		else {
135
			$this->processNested($token);
136
		}
137
	}
138
139
	private function processNested($token) {
140
		$parser = new Value($this->baseData, $this->autoLookup);
141
		$funcResult = $this->data->parseNested($parser, $token, $this->last);
142
		$this->result->processValue($funcResult);
143
		$this->last = null;
144
	}
145
146
	private function callTransphpormFunctions($token) {
147
		$this->result->processValue($this->baseData->{$this->last}($token['value']));
148
		foreach ($this->result->getResult() as $i => $value) {
149
			if (is_scalar($value)) {
150
				$val = $this->data->read($value);
151
				if ($val) $this->result[$i] = $val;
152
			}
153
		}
154
		$this->last = null;
155
	}
156
157
	//Applies the current operation to whatever is in $last based on $mode
158
	private function processLast() {
159
		if ($this->last !== null) {
160
			try {
161
				$value = $this->data->extract($this->last, $this->autoLookup, $this->traversing);
162
				$this->result->processValue($value);
163
			}
164
			catch (\UnexpectedValueException $e) {
165
				$this->processLastUnexpected();
166
			}
167
		}
168
	}
169
170
	private function processLastUnexpected() {
171
		if (!($this->autoLookup || $this->traversing)) {
172
			$this->result->processValue($this->last);
173
		}
174
		else {
175
			$this->result->clear();
176
			$this->result[0] = false;
177
		}
178
	}
179
}
180