/** * This method is called after initializing the data. * * @param array $options - Options. * * @throws ValidationException * * @return void */ public function validate(array $options = []) { $options = array_replace(['validateMinChildren' => true, 'validateMaxChildren' => true, 'validateParentMandatory' => true, 'validateParentMustNotBeSet' => true], $options); $countChildren = $this->countChildren(); if ($options['validateMinChildren'] && ($min = $this->getValidationConfig('minChildren')) !== null && $min > $countChildren) { throw ValidationException::create('Children must be, at least, ' . $min . '. Currently, node "' . $this->getId() . '" has ' . $countChildren . ' children.'); } if ($options['validateMaxChildren'] && ($max = $this->getValidationConfig('maxChildren')) !== null && $max < $countChildren) { throw ValidationException::create('Children cannot exceed a maximum of ' . $max . '. Currently, node "' . $this->getId() . '" has ' . $countChildren . ' children.'); } if ($options['validateParentMandatory'] && $this->getValidationConfig('parentMandatory') && $this->getParent() === null) { throw ValidationException::create('Node "' . $this->getId() . '" must have a Parent!'); } if ($options['validateParentMustNotBeSet'] && $this->getValidationConfig('parentMustNotBeSet') && $this->getParent() !== null) { throw ValidationException::create('Node "' . $this->getId() . '" must NOT have a Parent!'); } }
/** * Creates a Node instance through the node factory. * * @param array $data - Node Data. * @param array $options - Options. * * @throws ValidationException * * @return NodeInterface */ protected function createNodeInstance(array $data, array $options = []) : NodeInterface { $factory = $this->getNodeFactory(); $node = $factory($data, $options); if (!is_object($node) || !$node instanceof NodeInterface) { throw ValidationException::create('Node Factory must return an instance of NodeInterface.'); } $this->_nodeRegistry[$node->getId()] = $node; return $node; }
/** * Writes the data into a graphviz image. * * @param array $data - Data. * @param array $options - Options. * * @throws ValidationException * @throws ExportException * * @return void */ public function write(array $data, array $options) { if (!$this->getUtils()->isDotInstalled()) { throw ExportException::create('Can\'t export to a Graphviz image because "dot" binary is not available. Verify that the ' . '"graphviz" package is installed on your system.'); } if (!isset($data['node']) || !$data['node'] instanceof NodeInterface) { throw ValidationException::create('Data attribute "node" must be an instance of IronEdge\\Component\\Graphs\\Node\\NodeInterface.'); } if (!isset($options['path']) || !is_string($options['path']) || $options['path'] === '') { throw ValidationException::create('To be able to export this graph to a graphviz image, you must set option "path" with a string ' . 'with the path to the target file.'); } /** @var NodeInterface $graph */ $graph = $data['node']; $file = $options['path']; $graphvizCode = 'digraph ' . $graph->getId() . ' {' . PHP_EOL; $graphAttributes = $graph->getMetadataAttr('graphviz.nodeAttributes', []); if ($graphAttributes) { foreach ($graphAttributes as $k => $v) { $graphvizCode .= ' ' . $k . '=' . $v . ';' . PHP_EOL; } $graphvizCode .= PHP_EOL; } $graphvizCode .= $this->generateNodeCode($graph); $graphvizCode .= '}'; $this->generateGraphvizOutputFile($graphvizCode, $file, ['targetType' => 'png']); }