Example #1
0
 /**
  * Broke this out to more easily make it recursive
  * @param array $data
  * @param ContextInterface $context
  * @return Tree 
  */
 private function doBuild(array $data, ContextInterface $context)
 {
     $filter = new FilterFactory($context, $data['Condition']);
     $tree = new Tree($filter->build());
     $tree->setSegmentName($data['segmentName']);
     $tree->setSegmentId($data['segmentId']);
     if (is_array($data['children'])) {
         foreach ($data['children'] as $child) {
             $segment = $this->doBuild($child, $context);
             $segment->setParent($tree);
             $tree->addChild($segment);
         }
     }
     return $tree;
 }
Example #2
0
 /**
  * Validate given segment tree (or subtree, whatever it happens to be)
  * @param Tree $tree 
  * @return void
  * @throws RuntimeException
  */
 private function validateNode(Tree $tree)
 {
     $children = $tree->getChildren();
     for ($i = 0, $len = count($children); $i < $len; ++$i) {
         if ($i === $len - 1) {
             break;
         }
         $condition = $children[$i]->getCondition();
         $segmentName = $children[$i]->getSegmentName();
         if (!isset($condition)) {
             throw new RuntimeException('The segment ' . $segmentName . ' is not a default node, but has no valid condition');
         }
         $conditionType = $this->getConditionType($condition);
         if ($conditionType === 'composite') {
             $this->validateComposite($condition, $segmentName);
         } else {
             if ($conditionType === 'comparison') {
                 $this->validateComparison($condition, $segmentName);
             }
         }
         $this->validateNode($children[$i]);
     }
 }
Example #3
0
 /**
  * Private method, does the brunt of getLeafNode, but is separate to allow recursion
  * @param Tree $segment
  * @param array $path
  * @return Tree 
  */
 private function walkTree(Tree $segment, &$path)
 {
     if (!$segment->evaluateCondition()) {
         return null;
     }
     if ($segment->isLeafNode()) {
         return $segment;
     }
     $path[] = $segment;
     /* @var $child Tree */
     foreach ($segment->getChildren() as $child) {
         $val = $this->walkTree($child, $path);
         if (isset($val)) {
             return $val;
         }
     }
     // Branch evaluated to false, pop the last path off
     array_pop($path);
 }
Example #4
0
 /**
  * Test enumerating our tree
  * @return void
  */
 public function testEnumerate()
 {
     $enum = $this->tree->enumerate();
     $this->assertEquals($enum, 4);
 }