Completed
Pull Request — master (#175)
by Tom
05:04 queued 04:06
created

ValueResult::lower()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
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
class ValueResult {
9
	private $result = [];
10
	/*
11
		The next operation to perform. Will be one of the following:
12
			ARG - A new value e.g,  "a","b"  becomes ["a", "b"]
13
			CONCAT - Concat onto the current arg e.g "a" + "b" becomes ["ab"]
14
			NOT - Boolean operation "a" != "b" becomes [true]
15
			EQUALS - Boolean operation "a" = "b" becomes [false]
16
	*/
17
	private $mode = Tokenizer::ARG;
18
19
	//Processes $newValue using $mode. Either concats to the current argument, adds a new argument
20
	//Or usess the two arguments for a boolean comparison
21
	public function processValue($newValue) {
22
		$funcs = [
23
			Tokenizer::ARG => 'arg',
24
			Tokenizer::CONCAT => 'concat',
25
			Tokenizer::EQUALS => 'equals',
26
			Tokenizer::NOT => 'not',
27
			Tokenizer::SUBTRACT => 'sub',
28
			Tokenizer::MULTIPLY => 'mult',
29
			Tokenizer::DIVIDE => 'div',
30
			Tokenizer::GREATER_THAN => 'greater',
31
			Tokenizer::LOWER_THAN => 'lower'
32
		];
33
34
		if ($funcs[$this->mode] === 'concat' && is_numeric($newValue)
35
			&& is_numeric($this->result[count($this->result)-1]))
36
			$this->add($newValue);
37
		else
38
			$this->{$funcs[$this->mode]}($newValue);
39
	}
40
41
	public function arg($value) {
42
		$this->result[] = $value;
43
	}
44
45
	public function concat($value) {
46
		$this->result[count($this->result)-1] .= $value;
47
	}
48
49
	public function not($value) {
50
		$this->result[count($this->result)-1] = $this->result[count($this->result)-1] != $value;
51
	}
52
53
	public function equals($value) {
54
		$this->result[count($this->result)-1] = $this->result[count($this->result)-1] == $value;
55
	}
56
57
	public function greater($value) {
58
		$this->result[count($this->result)-1] = $this->result[count($this->result)-1] > $value;
59
	}
60
61
	public function lower($value) {
62
		$this->result[count($this->result)-1] = $this->result[count($this->result)-1] < $value;
63
	}
64
65
	public function add($value) {
66
		$this->result[count($this->result)-1] += $value;
67
	}
68
69
	public function sub($value) {
70
		$this->result[count($this->result)-1] -= $value;
71
	}
72
73
	public function mult($value) {
74
		$this->result[count($this->result)-1] *= $value;
75
	}
76
77
	public function div($value) {
78
		$this->result[count($this->result)-1] /= $value;
79
	}
80
81
	public function setMode($mode) {
82
		$this->mode = $mode;
83
	}
84
85
	public function getMode() {
86
		return $this->mode;
87
	}
88
89
	public function getResult() {
90
		return $this->result;
91
	}
92
93
	public function pop() {
94
		return array_pop($this->result);
95
	}
96
97
	private function write($index, $value, $allowNull = false) {
98
		if ($value !== null || $allowNull === true) {
99
			$this->result[$index] = $value;
100
		}
101
	}
102
103
	//Postprocessing - replace values with null where allowed, or override a value at position
104
	public function postProcess(ValueData $data, $val, $overrideVal, $allowNull) {
105
		foreach ($this->getResult() as $i => $value) {
106
			if (is_scalar($value)) {
107
				$val = ($overrideVal == $val) ? $data->read($value) : $overrideVal;
108
				$this->write($i, $val, $allowNull);
109
110
			}
111
		}
112
	}
113
114
	public function clear() {
115
		$this->result = [];
116
	}
117
}
118