CFDBTransformParser

The Contact Form DB CFDBTransformParser class.

Defined (1)

The class is defined in the following location(s).

/CFDBTransformParser.php  
  1. class CFDBTransformParser extends CFDBParserBase { 
  2.  
  3. var $tree = array(); 
  4.  
  5. /** 
  6. * @var array[CFDBDataIterator] 
  7. */ 
  8. var $transformIterators = array(); 
  9.  
  10.  
  11. public function getExpressionTree() { 
  12. return $this->tree; 
  13.  
  14. public function parse($string) { 
  15. $arrayOfANDedStrings = $this->parseANDs($string); // e.g. "xx=yy()&&zz()" -> ["xx=yy(a, b, c)", "zz"] 
  16. foreach ($arrayOfANDedStrings as $expressionString) { 
  17. $rawExpression = $this->parseExpression(trim($expressionString)); // e.g. ["xx" "=" "yy(a, b, c)"] or ["zz"] 
  18. if (empty($rawExpression)) { 
  19. continue; 
  20. $expression = array(); 
  21. $function = null; 
  22. if (count($rawExpression) >= 3) { // e.g. ["xx" "=" "yy(a, b, c)"] 
  23. $expression[] = trim($rawExpression[0]); // field name 
  24. $expression[] = trim($rawExpression[1]); // = 
  25. $function = trim($rawExpression[2]); // function call 
  26. } else { 
  27. $function = trim($rawExpression[0]); // function call 
  28. $function = $this->parseValidFunctionOrClassTransform($function); // ["zz(a, b, c)"] -> ["zz", "a", "b", "c"] 
  29. if (is_array($function)) { 
  30. $expression = array_merge($expression, $function); 
  31. } else { 
  32. $expression[] = $function; 
  33. $this->tree[] = $expression; 
  34.  
  35.  
  36. /** 
  37. * Parse a comparison expression into its three components 
  38. * @param $comparisonExpression string in the form 'value1' . 'operator' . 'value2' where 
  39. * operator is a php comparison operator or '=' 
  40. * @return array of string [ value1, operator, value2 ] 
  41. */ 
  42. public function parseExpression($comparisonExpression) { 
  43. return preg_split('/(=)/', $comparisonExpression, -1,  
  44. PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); 
  45.  
  46.  
  47. public function setupTransforms() { 
  48. if ($this->tree) { 
  49. /** @var $previousTransformIterator CFDBDataIteratorDecorator */ 
  50. $previousTransformIterator = null; 
  51. foreach ($this->tree as $transformArray) { 
  52. // [field, =, func, a1, a2, ...] or [func] or [class] or [class, a1, a2, ...] 
  53. $transform = null; 
  54. if (!empty($transformArray)) { 
  55. $transform = null; 
  56. if (class_exists($transformArray[0])) { 
  57. $reflect = new ReflectionClass($transformArray[0]); 
  58. $args = array_slice($transformArray, 1); 
  59. $instance = $reflect->newInstanceArgs($args); 
  60. $transform = new CFDBTransformByClassIterator(); 
  61. /** @var $instance CFDBTransform */ 
  62. $transform->setTransformObject($instance); 
  63. } else { 
  64. // assume it is a function 
  65. $transform = new CFDBTransformByFunctionIterator(); 
  66. $transform->setFunctionEvaluator($this->functionEvaluator); 
  67. if (count($transformArray) > 1 && $transformArray[1] == '=') { 
  68. // [field_name, =, function_name, arg1, arg2, ...] 
  69. $transform->setFieldToAssign($transformArray[0]); 
  70. $transform->setFunctionArray(array_slice($transformArray, 2)); 
  71. } else { 
  72. // [function_name, arg1, arg2, ...] 
  73. $transform->setFunctionArray($transformArray); 
  74. // Set the data source for each transform as the previous transform 
  75. // to set up a pipeline/decorator pattern. 
  76. // The first transform is left with null data source to be hooked up later 
  77. // to query results. 
  78. $transform->setSource($previousTransformIterator); // is null for first one 
  79. $previousTransformIterator = $transform; 
  80. $this->transformIterators[] = $transform; 
  81. if ($previousTransformIterator) { 
  82. // Stick a CFDBTransformEndpoint at the end of the list of transforms 
  83. $transform = new CFDBTransformEndpoint(); 
  84. $transform->setSource($previousTransformIterator); 
  85. $this->transformIterators[] = $transform; 
  86.  
  87. /** 
  88. * @param $dataSource CFDBDataIterator 
  89. */ 
  90. public function setDataSource($dataSource) { 
  91. if (count($this->transformIterators) > 0) { 
  92. $this->transformIterators[0]->setSource($dataSource); 
  93.  
  94. /** 
  95. * @return CFDBDataIteratorDecorator 
  96. */ 
  97. public function getIterator() { 
  98. return end($this->transformIterators); 
  99.