Completed
Push — master ( b59cd0...4a26e0 )
by Richard
02:31
created

Value::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 2
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
21
	private $tokenFuncs = [
22
			Tokenizer::NOT => 'processComparator',
23
			Tokenizer::EQUALS => 'processComparator',
24
			Tokenizer::DOT => 'processDot',
25
			Tokenizer::OPEN_SQUARE_BRACKET => 'processSquareBracket',
26
			Tokenizer::ARG => 'processSeparator',
27
			Tokenizer::CONCAT => 'processSeparator',
28
			Tokenizer::NAME => 'processScalar',
29
			Tokenizer::NUMERIC => 'processString',
30
			Tokenizer::BOOL => 'processString',
31
			Tokenizer::STRING => 'processString',
32
			Tokenizer::OPEN_BRACKET => 'processBrackets'
33
	];
34
35
	public function __construct($data, $autoLookup = false) {
36
		$this->baseData = $data;
37
		$this->autoLookup = $autoLookup;
38
	}
39
40
	public function parse($str) {
41
		$tokenizer = new Tokenizer($str);
42
		$tokens = $tokenizer->getTokens();
43
		$this->result = $this->parseTokens($tokens, $this->baseData);
44
		return $this->result;
45
	}
46
47
	public function parseTokens($tokens, $data = null) {
48
		$this->result = new ValueResult;
49
		$this->data = new ValueData($data ? $data : $this->baseData);
50
		$this->last = null;
51
52
		if (count($tokens) <= 0) return [$data];
53
54
		foreach (new TokenFilterIterator($tokens, [Tokenizer::WHITESPACE, Tokenizer::NEW_LINE]) as $token) {
55
			$this->{$this->tokenFuncs[$token['type']]}($token);
56
		}
57
58
		$this->processLast();
59
		return $this->result->getResult();
60
	}
61
62
	private function processComparator($token) {
63
		$this->processLast();
64
65
		if (!(in_array($this->result->getMode(), array_keys($this->tokenFuncs, 'processComparator')) && $token['type'] == Tokenizer::EQUALS)) {
66
			$this->result->setMode($token['type']);
67
			$this->last = null;
68
		}
69
	}
70
71
	//Reads the last selected value from $data regardless if it's an array or object and overrides $this->data with the new value
72
	//Dot moves $data to the next object in $data foo.bar moves the $data pointer from `foo` to `bar`
73
	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...
74
		if ($this->last !== null) $this->data->traverse($this->last);
75
		else {
76
			//When . is not preceeded by anything, treat it as part of the string instead of an operator
77
			// foo.bar is treated as looking up `bar` in `foo` whereas .foo is treated as the string ".foo"
78
			$lastResult = $this->result->pop();
79
			if ($lastResult) $this->data = new ValueData($lastResult);
80
			else {
81
				$this->processString(['value' => '.']);
82
				$this->result->setMode(Tokenizer::CONCAT);
83
			}
84
		}
85
86
		$this->last = null;
87
	}
88
89
	private function processSquareBracket($token) {
90
		$parser = new Value($this->baseData, $this->autoLookup);
91
		if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) {
92
			$this->callTransphpormFunctions($token);
93
		}
94
		else {
95
			if ($this->last !== null) $this->data->traverse($this->last);
96
			else {
97
				$lastResult = $this->result->pop();
98
				if ($lastResult) $this->data = new ValueData($lastResult);
99
			}
100
			$this->last = $parser->parseTokens($token['value'], null)[0];
101
		}
102
	}
103
104
	private function processSeparator($token) {
105
		$this->result->setMode($token['type']);
106
	}
107
108
	private function processScalar($token) {
109
		$this->last = $token['value'];
110
	}
111
112
	private function processString($token) {
113
		$this->result->processValue($token['value']);
114
	}
115
116
	private function processBrackets($token) {
117
		if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) {
118
			$this->callTransphpormFunctions($token);
119
		}
120
		else {
121
			$this->processNested($token);
122
		}
123
	}
124
125
	private function processNested($token) {
126
		$parser = new Value($this->baseData, $this->autoLookup);
127
		$funcResult = $this->data->parseNested($parser, $token, $this->last);
128
		$this->result->processValue($funcResult);
129
		$this->last = null;
130
	}
131
132
	private function callTransphpormFunctions($token) {
133
		$this->result->processValue($this->baseData->{$this->last}($token['value']));
134
		foreach ($this->result->getResult() as $i => $value) {
135
			if (is_scalar($value)) {
136
				$val = $this->data->read($value);
137
				if ($val) $this->result[$i] = $val;
138
			}
139
		}
140
		$this->last = null;
141
	}
142
143
	//Applies the current operation to whatever is in $last based on $mode
144
	private function processLast() {
145
		if ($this->last !== null) {
146
			try {
147
				$value = $this->data->extract($this->last, $this->autoLookup);
148
				$this->result->processValue($value);
149
			}
150
			catch (\UnexpectedValueException $e) {
151
				if (!$this->autoLookup) {
152
					$this->result->processValue($this->last);
153
				}
154
				else {
155
					$this->result->clear();
156
					$this->result[0] = false;
157
				}
158
			}
159
		}
160
	}
161
}
162