Hoa central
Graph.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Graph;
38 
39 use Hoa\Core;
40 
52 abstract class Graph
53 {
59  const TYPE_ADJACENCYLIST = 'AdjacencyList';
60 
66  const ALLOW_LOOP = true;
67 
73  const DISALLOW_LOOP = false;
74 
80  const DELETE_CASCADE = true;
81 
87  const DELETE_RESTRICT = false;
88 
94  protected $nodes = [];
95 
102  protected $loop = self::DISALLOW_LOOP;
103 
104 
105 
112  protected function __construct($loop = self::DISALLOW_LOOP)
113  {
114  $this->allowLoop($loop);
115 
116  return;
117  }
118 
126  public static function getInstance($type = self::TYPE_ADJACENCYLIST)
127  {
128  if ($type != self::TYPE_ADJACENCYLIST) {
129  throw new Exception(
130  'Type %s is not supported. Only self:TYPE_ADJACENCYLIST is ' .
131  'supported.',
132  0,
133  $type
134  );
135  }
136 
137  $arguments = func_get_args();
138  array_shift($arguments);
139 
140  return dnew('Hoa\Graph\\' . $type, $arguments);
141  }
142 
151  abstract public function addNode(IGraph\Node $node, $parent = []);
152 
159  abstract public function nodeExists($nodeId);
160 
168  abstract public function getNode($nodeId);
169 
175  protected function getNodes()
176  {
177  return $this->nodes;
178  }
179 
187  abstract public function getParent($nodeId);
188 
196  abstract public function getChild($nodeId);
197 
206  abstract public function deleteNode($nodeId, $propagate = self::DELETE_RESTRICT);
207 
215  abstract public function isLeaf($nodeId);
216 
224  abstract public function isRoot($nodeId);
225 
232  public function allowLoop($loop = self::DISALLOW_LOOP)
233  {
234  $old = $this->loop;
235  $this->loop = $loop;
236 
237  return $old;
238  }
239 
245  public function isLoopAllow()
246  {
247  return $this->loop;
248  }
249 
255  abstract public function __toString();
256 }
257 
261 Core\Consistency::flexEntity('Hoa\Graph\Graph');
getNode($nodeId)
nodeExists($nodeId)
const TYPE_ADJACENCYLIST
Definition: Graph.php:59
static getInstance($type=self::TYPE_ADJACENCYLIST)
Definition: Graph.php:126
const DELETE_CASCADE
Definition: Graph.php:80
const ALLOW_LOOP
Definition: Graph.php:66
const DISALLOW_LOOP
Definition: Graph.php:73
getChild($nodeId)
addNode(IGraph\Node $node, $parent=[])
__construct($loop=self::DISALLOW_LOOP)
Definition: Graph.php:112
getParent($nodeId)
const DELETE_RESTRICT
Definition: Graph.php:87
deleteNode($nodeId, $propagate=self::DELETE_RESTRICT)
isRoot($nodeId)
isLeaf($nodeId)
allowLoop($loop=self::DISALLOW_LOOP)
Definition: Graph.php:232