| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* @description     Transformation Style Sheets - Revolutionising PHP templating    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author          Tom Butler [email protected]                                             * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright       2017 Tom Butler <[email protected]> | https://r.je/                      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @version         1.2                                                             */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  | 			Tokenizer::IN => 'in' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		if ($funcs[$this->mode] === 'concat' && is_numeric($newValue) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 			&& is_numeric($this->result[count($this->result)-1])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 			$this->add($newValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 			$this->{$funcs[$this->mode]}($newValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	public function in($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		if (!is_array($value)) throw new \Exception(' `in` can only be used with arrays'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		$this->result[count($this->result)-1] = in_array($this->result[count($this->result)-1], $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	public function arg($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$this->result[] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	public function concat($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$this->result[count($this->result)-1] .= $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	public function not($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$this->result[count($this->result)-1] = $this->result[count($this->result)-1] != $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	public function equals($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$this->result[count($this->result)-1] = $this->result[count($this->result)-1] == $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	public function greater($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		$this->result[count($this->result)-1] = $this->result[count($this->result)-1] > $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	public function lower($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$this->result[count($this->result)-1] = $this->result[count($this->result)-1] < $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	public function add($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		$this->result[count($this->result)-1] += $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	public function sub($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		$this->result[count($this->result)-1] -= $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	public function mult($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		$this->result[count($this->result)-1] *= $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	public function div($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		$this->result[count($this->result)-1] /= $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	public function setMode($mode) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$this->mode = $mode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	public function getMode() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		return $this->mode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	public function getResult() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		return $this->result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	public function pop() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		return array_pop($this->result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	private function write($index, $value, $allowNull = false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		if ($value !== null || $allowNull === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			$this->result[$index] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 |  |  | 	//Postprocessing - replace values with null where allowed, or override a value at position | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  | 	public function postProcess(ValueData $data, $val, $overrideVal, $allowNull) { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  | 		foreach ($this->getResult() as $i => $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  | 			if (is_scalar($value)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  | 				$val = ($overrideVal == $val) ? $data->read($value) : $overrideVal; | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  | 				$this->write($i, $val, $allowNull); | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	public function clear() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		$this->result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  |