Procházet zdrojové kódy

* Keep template each calls from overwriting data when compiled
* Implement Iterator interface on earray

Nathaniel van Diepen před 7 roky
rodič
revize
69f35376b1
2 změnil soubory, kde provedl 23 přidání a 3 odebrání
  1. 21 1
      Data/earray.class.php
  2. 2 2
      Data/template.class.php

+ 21 - 1
Data/earray.class.php

@@ -3,9 +3,10 @@
 	require_once(realpath(dirname(__DIR__).'/events.trait.php'));
 	use Juju\Events;
 
-	class EArray implements \ArrayAccess, \Countable {
+	class EArray implements \ArrayAccess, \Countable, \Iterator {
 		use Events;
 		protected $data = [];
+		private $index = 0;
 		public static function from(array $data){
 			return new static($data);
 		}
@@ -42,5 +43,24 @@
 		public function to_array(){
 			return $this->data;
 		}
+		public function rewind(){
+			$this->index = 0;
+		}
+		public function current(){
+			return $this[$this->keys()[$this->index]];
+		}
+		public function key(){
+			return $this->keys()[$this->index];
+		}
+		public function next(){
+			$keys = $this->keys();
+			return isset($keys[++$this->index]) ? $this->list[$keys[$this->index]] : false;
+		}
+		public function valid(){
+			return isset($this->keys()[$this->index]);
+		}
+		public function keys(){
+			return array_keys($this->data);
+		}
 	}
 ?>

+ 2 - 2
Data/template.class.php

@@ -72,9 +72,9 @@
 			$output = preg_replace_callback(static::$regex['each'], function($matches){
 				$output = "<?php if(isset(\$data[".var_export($matches[1], true)."])): ";
 				$output .= "foreach(\$data[".var_export($matches[1], true)."] as \$item): ";
-				$output .= "\$parent = \$data; ?>";
+				$output .= "\$parent[] = \$data; ?>";
 				$output .= static::compile($matches[2]);
-				$output .= "<?php \$data = \$parent;";
+				$output .= "<?php \$data = array_pop(\$parent);";
 				$output .= "endforeach;endif; ?>";
 				return $output;
 			}, $output);