Completed
Push — master ( a3a7c9...543a8a )
by Richard
01:50
created

ValueResult::processValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 13
nc 2
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 implements \ArrayAccess {
9
	private $result = [];
10
11
	/*
12
		The next operation to perform. Will be one of the following:
13
			ARG - A new value e.g,  "a","b"  becomes ["a", "b"]
14
			CONCAT - Concat onto the current arg e.g "a" + "b" becomes ["ab"]
15
			NOT - Boolean operation "a" != "b" becomes [true]
16
			EQUALS - Boolean operation "a" = "b" becomes [false]
17
	*/
18
	private $mode = Tokenizer::ARG;
19
20
	//Processes $newValue using $mode. Either concats to the current argument, adds a new argument
21
	//Or usess the two arguments for a boolean comparison
22
	public function processValue($newValue) {
23
		$funcs = [
24
			Tokenizer::ARG => 'arg',
25
			Tokenizer::CONCAT => 'concat',
26
			Tokenizer::EQUALS => 'equals',
27
			Tokenizer::NOT => 'not',
28
			Tokenizer::SUBTRACT => 'sub',
29
			Tokenizer::MULTIPLY => 'mult',
30
			Tokenizer::DIVIDE => 'div'
31
		];
32
33
		if (is_numeric($newValue) && $funcs[$this->mode] === 'concat')
34
			$this->add($newValue);
35
		else
36
			$this->{$funcs[$this->mode]}($newValue);
37
	}
38
39
	public function arg($value) {
40
		$this->result[] = $value;
41
	}
42
43
	public function concat($value) {
44
		$this->result[count($this->result)-1] .= $value;
45
	}
46
47
	public function not($value) {
48
		$this->result[count($this->result)-1] = $this->result[count($this->result)-1] != $value;
49
	}
50
51
	public function equals($value) {
52
		$this->result[count($this->result)-1] = $this->result[count($this->result)-1] == $value;
53
	}
54
55
	public function add($value) {
56
		$this->result[count($this->result)-1] += $value;
57
	}
58
59
	public function sub($value) {
60
		$this->result[count($this->result)-1] -= $value;
61
	}
62
63
	public function mult($value) {
64
		$this->result[count($this->result)-1] *= $value;
65
	}
66
67
	public function div($value) {
68
		$this->result[count($this->result)-1] /= $value;
69
	}
70
71
	public function setMode($mode) {
72
		$this->mode = $mode;
73
	}
74
75
	public function getMode() {
76
		return $this->mode;
77
	}
78
79
	public function getResult() {
80
		return $this->result;
81
	}
82
83
	public function pop() {
84
		return array_pop($this->result);
85
	}
86
87
	public function offsetSet($key, $value) {
88
		$this->result[$key] = $value;
89
	}
90
91
	public function offsetGet($key) {
92
		return $this->result[$key];
93
	}
94
95
	public function offsetUnset($key) {
96
		unset($this->result[$key]);
97
	}
98
99
	public function offsetExists($key) {
100
		return isset($this->result[$key]);
101
	}
102
103
	public function clear() {
104
		$this->result = [];
105
	}
106
}
107