commit 3c62cf98394e3938090cc7175494a9689aeccbf8
parent 62a4e7aeac4519ca018a276acb769fc2170dc731
Author: Jan Dankert <develop@jandankert.de>
Date: Fri, 1 Jul 2022 19:30:30 +0200
New: Data Object for accessing data trees
Diffstat:
7 files changed, 112 insertions(+), 20 deletions(-)
diff --git a/modules/cms/generator/ValueGenerator.class.php b/modules/cms/generator/ValueGenerator.class.php
@@ -35,6 +35,7 @@ use cms\model\Value;
use dsl\context\BaseScriptableObject;
use dsl\DslException;
use dsl\executor\DslInterpreter;
+use dsl\standard\Data;
use logger\Logger;
use LogicException;
use util\Code;
@@ -1078,7 +1079,7 @@ class ValueGenerator extends BaseGenerator
break;
}
- $inhalt = $this->filterValue( $data, $element->code );
+ $inhalt = $this->filterValue( new Data($data), $element->code );
if ( is_array($inhalt) )
$inhalt = YAML::dump( $inhalt );
diff --git a/modules/dsl/ast/DslFunctionCall.class.php b/modules/dsl/ast/DslFunctionCall.class.php
@@ -5,6 +5,9 @@ namespace dsl\ast;
use dsl\DslRuntimeException;
use dsl\DslToken;
use dsl\executor\DslInterpreter;
+use dsl\standard\Number;
+use dsl\standard\StandardArray;
+use dsl\standard\StandardString;
class DslFunctionCall implements DslStatement
{
@@ -30,10 +33,6 @@ class DslFunctionCall implements DslStatement
$function = $this->name->execute( $context );
-
- //if ( ! array_key_exists( $name, $context ) )
- // throw new DslRuntimeException('function \''.$this->name.'\' does not exist.');
-
if ( $this->parameters == null )
$parameterValues = []; // parameterless functions.
else
@@ -60,7 +59,7 @@ class DslFunctionCall implements DslStatement
}
elseif ( is_callable($function) ) {
- return DslExpression::convertValueToStandardObject( call_user_func_array( $function, $parameterValues) );
+ return DslExpression::convertValueToStandardObject( call_user_func_array( $function, $this->toPrimitiveValues($parameterValues)) );
}
else
throw new DslRuntimeException('function is not callable'.var_export($function));
@@ -70,4 +69,25 @@ class DslFunctionCall implements DslStatement
{
$this->statements[] = $tokens;
}
+
+
+ /**
+ * Converts variables to its primitives, because external objects in applications will not be able to handle things like "StandardString".
+ * @param $parameterValues
+ * @return array
+ */
+ private function toPrimitiveValues( $parameterValues )
+ {
+ return array_map( function( $val ) {
+ if ( $val instanceof StandardArray )
+ return $val->getInternalValue();
+ if ( $val instanceof Number )
+ return $val->toNumber();
+ if ( $val instanceof StandardString )
+ return (string)$val;
+
+ return $val;
+
+ },$parameterValues );
+ }
}
\ No newline at end of file
diff --git a/modules/dsl/ast/DslProperty.class.php b/modules/dsl/ast/DslProperty.class.php
@@ -6,6 +6,7 @@ use cms\generator\dsl\DslObject;
use dsl\context\Scriptable;
use dsl\DslRuntimeException;
use dsl\executor\DslInterpreter;
+use dsl\standard\Data;
use dsl\standard\Number;
use dsl\standard\StandardArray;
use dsl\standard\StandardString;
@@ -36,22 +37,32 @@ class DslProperty implements DslStatement
$object = $this->variable->execute( $context );
- $objectContext = [];
-
if ( is_object( $object ) ) {
- $objectContext = get_object_vars( $object );
+ if ( $object instanceof Data ) {
+ $objectContext = $object->getData();
+ }
+ else {
+
+ // object attributes
+ $objectContext = get_object_vars( $object );
+
+ //if ( $object instanceof StandardArray ) {
+ // foreach( $object->getInternalValue() as $key=> $value )
+ // $objectContext[$key] = $value;
+ //}
- // copy object methods to the object context to make them callable.
- foreach( get_class_methods( $object ) as $method ) {
- $objectContext[ $method ] = function() use ($method, $object) {
+ // copy object methods to the object context to make them callable.
+ foreach( get_class_methods( $object ) as $method ) {
+ $objectContext[ $method ] = function() use ($method, $object) {
- // For Security: Do not expose all available objects, they must implement a marker interface.
- if ( DslInterpreter::isSecure() && ! $object instanceof Scriptable )
- throw new DslRuntimeException('Object '.get_class($object).' is not marked as scriptable and therefore not available in secure mode');
+ // For Security: Do not expose all available objects, they must implement a marker interface.
+ if ( DslInterpreter::isSecure() && ! $object instanceof Scriptable )
+ throw new DslRuntimeException('Object '.get_class($object).' is not marked as scriptable and therefore not available in secure mode');
- return call_user_func_array( array($object,$method),func_get_args() );
- };
+ return call_user_func_array( array($object,$method),func_get_args() );
+ };
+ }
}
}
else {
diff --git a/modules/dsl/standard/Data.class.php b/modules/dsl/standard/Data.class.php
@@ -0,0 +1,50 @@
+<?php
+namespace dsl\standard;
+
+use dsl\context\BaseScriptableObject;
+
+class Data extends BaseScriptableObject
+{
+ /**
+ * @var mixed|null
+ */
+ private $value;
+
+ /**
+ * StandardArray constructor.
+ * @param $value
+ */
+ public function __construct($value=null)
+ {
+ $this->value = $value;
+ }
+
+ public function getData() {
+
+ return array_map(
+
+ function( $val ) {
+ if ( is_array($val) )
+ return new Data( $val );
+ else
+ return $val;
+ }
+ ,$this->value);
+ }
+
+
+ public function __toString()
+ {
+ return '['.implode(',',array_map(
+
+ function( $val ) {
+ if ( is_object($val) )
+ return (new Data( get_object_vars($val) ))->__toString();
+ if ( is_array($val) )
+ return (new Data( $val ))->__toString();
+ else
+ return $val;
+ }
+ ,$this->value)).']';
+ }
+}
+\ No newline at end of file
diff --git a/modules/dsl/standard/StandardArray.class.php b/modules/dsl/standard/StandardArray.class.php
@@ -107,13 +107,20 @@ class StandardArray extends BaseScriptableObject
{
return array_unshift( $this->value,$value );
}
- public function slice($from,$end)
+ public function slice($from,$end = null )
{
- return array_slice( $this->value,$from,$end-$from);
+ if ( $end )
+ return array_slice( $this->value,$from,$end-$from);
+ else
+ return array_slice( $this->value,$from);
}
public function sort()
{
return asort( $this->value );
}
+ public function get( $key )
+ {
+ return @$this->value[ $key ];
+ }
}
\ No newline at end of file
diff --git a/modules/dsl/standard/StandardMath.class.php b/modules/dsl/standard/StandardMath.class.php
@@ -37,7 +37,7 @@ class StandardMath extends BaseScriptableObject
public function exp($x) { return exp($x); }
public function expm1($x) { return expm1($x); }
- public function log($x) { return ($x); }
+ public function log($x) { return log($x); }
public function log1p($x) { return log($x); }
public function log10($x) { return log10($x); }
public function max($x,$y) { return max($x,$y); }
diff --git a/modules/dsl/standard/Write.class.php b/modules/dsl/standard/Write.class.php
@@ -11,6 +11,8 @@ class Write
if ( is_object($text ) && !method_exists($text, '__toString') )
// Workaround for external objects, that do not implement __toString()
$this->buffer .= get_class($text);
+ elseif ( is_array($text) )
+ $this->buffer .= '['.implode(',',$text).']';
else
$this->buffer .= $text;
}