| 1 |  |  | <?php namespace Knot\Dict; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Knot\Exceptions\WrongArrayPathException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | trait PathOperationsTrait { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 	 * For parsing array path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	public static $ARRAY_PATH_DELIMITER = "."; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	 * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	protected $path = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	abstract public function childParent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 * @param null $add | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 * @return null|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	public function path($add = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		return $add ? $this->path != null ? $this->path . self::$ARRAY_PATH_DELIMITER . $add : $add : $this->path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * @param $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	public static function pathParser($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		return explode(self::$ARRAY_PATH_DELIMITER, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 * @param array $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	public static function pathCombiner(array $path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		return implode(self::$ARRAY_PATH_DELIMITER, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 * @param $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	public function isPath($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		try | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			$this->get($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		catch (WrongArrayPathException $e) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * @param $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * @return array|ChildDict|Mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 * @throws WrongArrayPathException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	public function get($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		$arguments = func_get_args(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		if ( isset( $arguments[1] ) ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			$default_return = $arguments[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		$target_data =& $this->data; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		foreach (static::pathParser($path) as $way) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			if ( ! isset( $target_data[$way] ) ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 				if ( isset( $default_return ) ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 				{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 					$r = $this->set($path, $default_return); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 					return $r; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 				throw new WrongArrayPathException($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 			$target_data = &$target_data[$way]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		if ( is_array($target_data) ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			return new ChildDict($target_data, $this->childParent(), $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		return $target_data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	 * For Get path without parsing default return to data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	 * @param $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 * @return Mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 * @throws WrongArrayPathException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	public function getOnly($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		$arguments = func_get_args(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		if ( isset( $arguments[1] ) ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 			$value = $arguments[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		try | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			return $this->get($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		catch (WrongArrayPathException $e) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 			if ( isset( $value ) ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 				return $this->value($value, [ $path ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 			throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	 * @param $rawPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	 * @param $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	 * @return Mixed|\Knot\Dict\ChildDict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	public function set($rawPath, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		$target_data =& $this->data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 		foreach (static::pathParser($rawPath) as $path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 			// If there is no way to go or this is not an array! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 			if ( ! isset( $target_data[$path] ) || ! is_array($target_data[$path]) ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 				$target_data[$path] = [ ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 			$target_data =& $target_data[$path]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		$value = $this->value($value, [ $rawPath ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 		$target_data = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 		if ( is_array($target_data) ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 			return new ChildDict($target_data, $this->childParent(), $this->path()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 		return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 	 * @param $rawPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 	 * @return $this | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 190 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 191 |  |  | 	public function del($rawPath) | 
            
                                                                        
                            
            
                                    
            
            
                | 192 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 193 |  |  | 		$target_data =& $this->data; | 
            
                                                                        
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 195 |  |  | 		$paths = static::pathParser($rawPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 197 |  |  | 		$target_key = array_pop($paths); | 
            
                                                                        
                            
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 199 |  |  | 		foreach ($paths as $path) | 
            
                                                                        
                            
            
                                    
            
            
                | 200 |  |  | 		{ | 
            
                                                                        
                            
            
                                    
            
            
                | 201 |  |  | 			// If there is no way to go or this is not an array! | 
            
                                                                        
                            
            
                                    
            
            
                | 202 |  |  | 			if ( ! isset( $target_data[$path] ) || ! is_array($target_data[$path]) ) | 
            
                                                                        
                            
            
                                    
            
            
                | 203 |  |  | 			{ | 
            
                                                                        
                            
            
                                    
            
            
                | 204 |  |  | 				return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 205 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 207 |  |  | 			$target_data =& $target_data[$path]; | 
            
                                                                        
                            
            
                                    
            
            
                | 208 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 209 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 210 |  |  | 		if ( isset( $target_data[$target_key] ) ) | 
            
                                                                        
                            
            
                                    
            
            
                | 211 |  |  | 		{ | 
            
                                                                        
                            
            
                                    
            
            
                | 212 |  |  | 			unset( $target_data[$target_key] ); | 
            
                                                                        
                            
            
                                    
            
            
                | 213 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 214 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 215 |  |  | 		return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 216 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  | 	protected function value($value, array $arguments = [ ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  | 		return $value instanceof \Closure ? call_user_func_array($value, $arguments) : $value; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 222 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 223 |  |  | } | 
            
                        
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: