Browse Source

Make EArrays serializable and json serializable. Allow getting variables based on other variables in templates

Nathaniel van Diepen 6 years ago
parent
commit
121a07a638
2 changed files with 45 additions and 4 deletions
  1. 11 2
      Data/earray.class.php
  2. 34 2
      Data/template.class.php

+ 11 - 2
Data/earray.class.php

@@ -3,7 +3,7 @@
 	require_once(realpath(dirname(__DIR__).'/events.trait.php'));
 	use Juju\Events;
 
-	class EArray implements \ArrayAccess, \Countable, \Iterator {
+	class EArray implements \ArrayAccess, \Countable, \Iterator, \Serializable, \JsonSerializable {
 		use Events;
 		protected $data = [];
 		private $index = 0;
@@ -34,6 +34,15 @@
 		public function count(){
 			return count($this->data);
 		}
+		public function serialize(){
+			return serialize($this->data);
+		}
+		public function unserialize($data){
+			$this->data = unserialize($data);
+		}
+		public function JsonSerialize(){
+			return $this->data;
+		}
 		public function each(callable $fn){
 			foreach($this->data as $offset => $model){
 				$fn($model, $offset);
@@ -63,4 +72,4 @@
 			return array_keys($this->data);
 		}
 	}
-?>
+?>

+ 34 - 2
Data/template.class.php

@@ -30,6 +30,10 @@
 			'literal-attributes'=>true
 		];
 		private static $regex = [
+			'getmatch'=>'/\{!([^#\/?_][^}\n]*?)\}/i',
+			'getparentmatch'=>'/\{!\.\.\/([^#\/?_][^}\n]*?)\}/i',
+			'getrawmatch'=>'/\{!@([^#\/?_][^}\n]*?)\}/i',
+			'getrawparentmatch'=>'/\{!@\.\.\/([^#\/?_][^}\n]*?)\}/i',
 			'match'=>'/\{([^#\/?_][^}\n]*?)\}/i',
 			'parentmatch'=>'/\{\.\.\/([^#\/?_][^}\n]*?)\}/i',
 			'rawmatch'=>'/\{@([^#\/?_][^}\n]*?)\}/i',
@@ -45,7 +49,7 @@
 			'eval'=>'/\{\?([\W\w\S\s]+)\?\}/i',
 			'include'=>'/{#include ([^}]+)}/i',
 			'define'=>'/\{#define ([^}]*)\}([\S\s]*)\{\/define \1\}/i',
-			'widget'=>'/{#widget ([^ }]+)(:? ([^=]+=[^}&]+)*)?}/i'
+			'widget'=>'/{#widget ([^ }]+)(?: ((?:[^=}]+=[^}&]+)*))?}/i'
 		];
 		protected static $parsers;
 		private $template;
@@ -102,7 +106,7 @@
 											$args[urldecode($param[0])] = urldecode($param[1]);
 										}
 									}
-									$widget = "<?php \$widget_parent[] = \$data; \$data = array_merge(\$data, unserialize(".json_encode(serialize($args)).")); ?>";
+									$widget = "<?php \$widget_parent[] = \$data; \$data = array_merge(json_decode(json_encode(\$data), true), json_decode(base64_decode(".var_export(base64_encode(json_encode($args)), true)."), true)); ?>";
 									$widget .= static::compile($widgets[$name]);
 									$widget .= "<?php \$data = array_pop(\$widget_parent); ?>";
 								}else{
@@ -168,6 +172,26 @@
 							return "<?php {$matches[1]}; ?>";
 						}, $output);
 					},
+					'getrawmatch'=>function(&$output){
+						$output = preg_replace_callback(static::$regex['getrawmatch'], function($matches){
+							return "<?=(\$data[\$data[".var_export($matches[1], true)."] ?? ''] ?? '');?>";
+						}, $output);
+					},
+					'getrawparentmatch'=>function(&$output){
+						$output = preg_replace_callback(static::$regex['getrawparentmatch'], function($matches){
+							return "<?=(\$parent[count(\$parent)-1][\$data[".var_export($matches[1], true)."] ?? ''] ?? '');?>";
+						}, $output);
+					},
+					'getmatch'=>function(&$output){
+						$output = preg_replace_callback(static::$regex['getmatch'], function($matches){
+							return "<?=htmlentities(\$data[\$data[".var_export($matches[1], true)."] ?? ''] ?? '');?>";
+						}, $output);
+					},
+					'getparentmatch'=>function(&$output){
+						$output = preg_replace_callback(static::$regex['getparentmatch'], function($matches){
+							return "<?=htmlentities(\$parent[count(\$parent)-1][\$data[".var_export($matches[1], true)."] ?? ''] ?? '');?>";
+						}, $output);
+					},
 					'rawmatch'=>function(&$output){
 						$output = preg_replace_callback(static::$regex['rawmatch'], function($matches){
 							return "<?=(\$data[".var_export($matches[1], true)."] ?? '');?>";
@@ -285,6 +309,14 @@
 			static::$parsers['echo']($output);
 			// Handle {? expression ?}
 			static::$parsers['eval']($output);
+			// Handle {!@../name}
+			static::$parsers['getrawparentmatch']($output);
+			// Handle {!@name}
+			static::$parsers['getrawmatch']($output);
+			// Handle {!../name}
+			static::$parsers['getparentmatch']($output);
+			// Handle {!name}
+			static::$parsers['getmatch']($output);
 			// Handle {@../name}
 			static::$parsers['rawparentmatch']($output);
 			// Handle {@name}