arrayexplorer.php 2.96 KB
Newer Older
sreg's avatar
sreg committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
<?php
	class ArrayExplorer extends QArray
	{

	}
	class ArrayExplorer2 implements ArrayAccess,Countable,Iterator
	{
		protected $array;
		protected $position;

		public function __construct($array=array())
		{
			$this->fromArray($array);
		}

		public function clear()
		{
			$this->position=0;
			$this->array=array();
		}

		public function fromArray($array) //FIXME: changer par copy()?
		{
			$this->clear();
			if(is_array($array)) $this->array=$array;
		}

		public function toArray()
		{
			return $this->array;
		}

		public function toJSON()
		{
			return json_encode($this->array);
		}

		public function fromJSON($serialized)
		{
			$this->position=0;
			$this->array=json_decode($serialized,true);
			if(!is_array($this->array))
			{
				$this->array=array();
				return false;
			}
			return true;
		}

		/* Interface ArrayAccess */
		public function offsetExists($offset)
		{
			return true;
		}
		public function offsetGet($offset)
		{
			$default='';
			if(preg_match('#^(.*?)([:,])(.*)$#muis',$offset,$m))
			{
				$offset=$m[1];
				$selector=$m[2];
				$default=$m[3];
				if($selector==',') $default=eval("return ($default);");
			}
			return $this->get($offset,$default);
		}
		public function offsetSet($offset,$value)
		{
			if(is_null($offset))
				$this->array[]=$value;
			else
				$this->array[$offset]=$value;
		}
		public function offsetUnset($field)
		{
			$array=&$this->array;
			foreach(explode('/',$field) as $nodeName)
			{
				$offset=null;
				if(preg_match('#^(.*?)(\[(\d+)\])?$#',$nodeName,$m))
				{
					$nodeName=$m[1];
					if(array_key_exists(3,$m)) $offset=$m[3];
				}
				if(array_key_exists($nodeName,$array))
				{
					if(is_null($offset))
						unset($array[$nodeName]);
					else
						unset($array[$nodeName][$offset]);
					return;
				} else
					$array=&$array[$nodeName];
			}
		}

		/* Interface Countable */
		public function count()
		{
			return count($this->array);
		}

		/* Interface Iterator */
		public function current()
		{
			return $this->offsetGet($this->position);
		}
		public function key()
		{
			return $this->position;
		}
		public function next()
		{
			$this->position++;
		}
		public function rewind()
		{
			$this->position=0;
		}
		public function valid()
		{
			return isset($this->array[$this->position]);
		}

		public function get($field,$default='')
		{
			$res=$this->getPosition($field);
			if($res===false) $res=$default;
			return is_array($res)?new ArrayExplorer($res):$res;
		}

		protected function &getPosition($field)
		{
			$array=&$this->array;
			foreach(explode('/',$field) as $nodeName)
			{
				$offset=null;
				if(preg_match('#^(.*?)(\[(.+?)\])?$#',$nodeName,$m))
				{
					$nodeName=$m[1];
					if(array_key_exists(3,$m)) $offset=$m[3];
				}
				if(array_key_exists($nodeName,$array))
				{
					$array=&$array[$nodeName];
					if(!is_null($offset))
						if(array_key_exists($offset,$array))
							$array=&$array[$offset];
						else
							return false;
				} else
					return false;
			}
			return $array;
		}
	}
?>