Exemplo n.º 1
0
 public function testSourceDataGetter()
 {
     $source = uniqid('testSource', true);
     $data = new GeneratorData($source);
     $this->assertSame($source, $data->getSource());
     $newSource = uniqid('newTestSource', true);
     $data->setSource($newSource);
     $this->assertSame($newSource, $data->getSource());
 }
 /**
  * {@inheritdoc}
  */
 protected function prepare(GeneratorData $data, VisitorCollection $visitorCollection)
 {
     $source = $data->getSource();
     if (is_array($source)) {
         if (isset($source[self::NODE_ACTIONS])) {
             // traversing through actions, looking for "@addTree" action
             $actions = $source[self::NODE_ACTIONS];
             foreach ($actions as $nodeNo => $actionDefinition) {
                 // do not validate syntax, error will be thrown afterwards
                 $actionName = is_array($actionDefinition) ? key($actionDefinition) : '';
                 if (self::ACTION_ADD_TREE === $actionName) {
                     $path = self::NODE_ACTIONS . '.' . $nodeNo;
                     $actionNode = reset($actionDefinition);
                     // looking for items, parent and tree it self
                     if (!isset($actionNode[self::NODE_ITEMS], $actionNode[self::NODE_TREE])) {
                         throw new SyntaxException('expected array with keys "items" and "tree"', $actionDefinition, $path);
                     }
                     $transformedActions = [];
                     $treeParent = key($actionNode[self::NODE_TREE]);
                     $tree = current($actionNode[self::NODE_TREE]);
                     try {
                         $this->processTree($transformedActions, $tree, $treeParent, $actionNode[self::NODE_ITEMS]);
                     } catch (\LogicException $e) {
                         throw new SyntaxException('invalid tree definition. ' . $e->getMessage(), $actionDefinition, $path);
                     }
                     // pre-generate "path" option in order to show correct path if validation error will occur
                     array_walk($transformedActions, function (&$val) use($path) {
                         $val[self::PATH_ATTR] = $path;
                     });
                     $source[self::NODE_ACTIONS] = array_merge($source[self::NODE_ACTIONS], $transformedActions);
                     // unset processed "@addTree" action
                     unset($source[self::NODE_ACTIONS][$nodeNo]);
                 }
             }
         }
         // apply extensions
         foreach ($this->extensions as $extension) {
             $extension->prepare($source, $visitorCollection);
         }
     }
     $data->setSource($source);
 }