Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Graph to support formulas in Parse-tree notation

  • Loading branch information...
commit db61aea20aaa7293857134123d19f81ba8790cb3 1 parent 20c21ff
@l90r authored
View
17 jazzy-forms/core/Graph.php
@@ -159,11 +159,18 @@ function get_email_formulas() {
function get_dependencies($formula) {
$deps = array();
- if(is_array($formula)) foreach($formula as $token) {
- if($token[0] == 'v') {
- $id = $token[1];
- if(!in_array($id, $deps)) {
- $deps[] = $id;
+ $nodes = array($formula);
+ while(count($nodes)) {
+ $node = array_pop($nodes);
+ if(is_array($node)) {
+ if($node[0] == 'v') {
+ if(!in_array($node[1], $deps)) {
+ $deps[] = $node[1];
+ }
+ } elseif($node[0] == 'f' || $node[0] == 'o') {
+ for($i=2; $i<count($node); $i++) {
+ $nodes[] = $node[$i];
+ }
}
}
}
View
4 jazzy-forms/core/Template_Parser.php
@@ -40,8 +40,8 @@ function jzzf_chunk_is_formula($chunk) {
}
function jzzf_chunk_name($chunk, &$counter, $prefix) {
- if(count($chunk)==1 && $chunk[0][0]=='v') {
- $name = $chunk[0][1];
+ if($chunk[0]=='v') {
+ $name = $chunk[1];
} else {
$name = "_inline_${counter}_${prefix}";
$counter++;
View
36 test/php/Graph_Test.php
@@ -45,7 +45,7 @@ function test_single_dependency() {
$graph = $this->get_graph_from_elements($elements);
$this->assertEquals(array('num'=>'n', 'result'=>'f'), $graph->types);
$this->assertEquals(array('num'=>array('result')), $graph->dependencies);
- $this->assertEquals(array('result'=>array(array('v', 'num'), array('n', '2'), array('o', '*'))), $graph->formulas);
+ $this->assertEquals(array('result'=>array('o', '*', array('v', 'num'), array('n', '2'))), $graph->formulas);
}
function test_repeated_dependency() {
@@ -56,7 +56,7 @@ function test_repeated_dependency() {
$graph = $this->get_graph_from_elements($elements);
$this->assertEquals(array('num'=>'n', 'result'=>'f'), $graph->types);
$this->assertEquals(array('num'=>array('result')), $graph->dependencies);
- $this->assertEquals(array('result'=>array(array('v', 'num'), array('v', 'num'), array('v', 'num'), array('o', '*'), array('o', '*'))), $graph->formulas);
+ $this->assertEquals(array('result'=>array('o', '*', array('v', 'num'), array('o', '*', array('v', 'num'), array('v', 'num')))), $graph->formulas);
}
function test_single_dependency_ugly_characters() {
@@ -67,7 +67,7 @@ function test_single_dependency_ugly_characters() {
$graph = $this->get_graph_from_elements($elements);
$this->assertEquals(array('num1'=>'n', 'out_put'=>'f'), $graph->types);
$this->assertEquals(array('num1'=>array('out_put')), $graph->dependencies);
- $this->assertEquals(array('out_put'=>array(array('v', 'num1'), array('n', '2'), array('o', '*'))), $graph->formulas);
+ $this->assertEquals(array('out_put'=>array('o', '*', array('v', 'num1'), array('n', '2'))), $graph->formulas);
}
function test_multiple_dependency() {
@@ -81,8 +81,8 @@ function test_multiple_dependency() {
$this->assertEquals(array('num'=>array('subtotal', 'total')), $graph->dependencies);
$this->assertEquals(
array(
- 'subtotal' => array(array('v', 'num'), array('n', '2'), array('o', '*')),
- 'total'=> array(array('v', 'subtotal'), array('n', '1'), array('o', '+'))),
+ 'subtotal' => array('o', '*', array('v', 'num'), array('n', '2')),
+ 'total'=> array('o', '+', array('v', 'subtotal'), array('n', '1'))),
$graph->formulas
);
}
@@ -98,9 +98,9 @@ function test_dependency_no_input() {
$this->assertEquals(array(), $graph->dependencies);
$this->assertEquals(
array(
- 'num' => array(array('n', 10)),
- 'subtotal' => array(array('v', 'num'), array('n', '2'), array('o', '*')),
- 'total'=> array(array('v', 'subtotal'), array('n', '1'), array('o', '+'))),
+ 'num' => array('n', 10),
+ 'subtotal' => array('o', '*', array('v', 'num'), array('n', '2')),
+ 'total'=> array('o', '+', array('v', 'subtotal'), array('n', '1'))),
$graph->formulas
);
}
@@ -116,8 +116,8 @@ function test_dependency_circular_dependency() {
$this->assertEquals(array('num'=>array('subtotal', 'total')), $graph->dependencies);
$this->assertEquals(
array(
- 'subtotal' => array(array('v', 'num'), array('v', 'total'), array('o', '+')),
- 'total'=> array(array('v', 'subtotal'), array('n', '1'), array('o', '+'))),
+ 'subtotal' => array('o', '+', array('v', 'num'), array('v', 'total')),
+ 'total'=> array('o', '+', array('v', 'subtotal'), array('n', '1'))),
$graph->formulas
);
}
@@ -202,12 +202,12 @@ function test_email() {
$graph = jzzf_get_graph($form);
$email = $graph->email;
$this->assertEquals(json_decode('{
- "user": [["v", "user"]],
- "id": [["v", "id"]],
- "user2": [["v", "user2"]],
- "_inline_0_bcc": [["v", "a"], ["v", "b"], ["o", "+"]],
- "count": [["v", "count"]],
- "_inline_0_message": [["v", "price1"], ["v", "price2"], ["o", "+"]]
+ "user": ["v", "user"],
+ "id": ["v", "id"],
+ "user2": ["v", "user2"],
+ "_inline_0_bcc": ["o", "+", ["v", "a"], ["v", "b"]],
+ "count": ["v", "count"],
+ "_inline_0_message": ["o", "+", ["v", "price1"], ["v", "price2"]]
}', true), $email);
}
@@ -227,7 +227,7 @@ function test_template_variable_only() {
$graph = $this->get_graph_from_elements($elements);
$templates = $graph->templates;
$this->assertEquals(json_decode('{
- "out": [[["v", "a"]]]}', true), $graph->templates);
+ "out": [["v", "a"]]}', true), $graph->templates);
$this->assertEquals(json_decode('{
"a": ["out"]}', true), $graph->dependencies);
}
@@ -240,7 +240,7 @@ function test_template_mixed() {
$graph = $this->get_graph_from_elements($elements);
$templates = $graph->templates;
$this->assertEquals(json_decode('{
- "out": ["The result is ", [["v", "a"]]]}', true), $graph->templates);
+ "out": ["The result is ", ["v", "a"]]}', true), $graph->templates);
$this->assertEquals(json_decode('{
"a": ["out"]}', true), $graph->dependencies);
}
Please sign in to comment.
Something went wrong with that request. Please try again.