@@ -77,19 +77,12 @@ public function createVertex($id = NULL, $returnDuplicate = false)
7777 // no ID given
7878 if ($ id === NULL ) {
7979 $ id = $ this ->getNextId ();
80- } elseif (!is_int ($ id ) && !is_string ($ id )) {
81- throw new InvalidArgumentException ('Vertex ID has to be of type integer or string ' );
8280 }
83- if ($ this ->vertices ->hasVertexId ($ id )) {
84- if ($ returnDuplicate ) {
85- return $ this ->vertices ->getVertexId ($ id );
86- }
87- throw new OverflowException ('ID must be unique ' );
81+ if ($ returnDuplicate && $ this ->vertices ->hasVertexId ($ id )) {
82+ return $ this ->vertices ->getVertexId ($ id );
8883 }
89- $ vertex = new Vertex ($ id , $ this );
90- $ this ->verticesStorage [$ id ] = $ vertex ;
9184
92- return $ vertex ;
85+ return new Vertex ( $ this , $ id ) ;
9386 }
9487
9588 /**
@@ -105,12 +98,11 @@ public function createVertexClone(Vertex $originalVertex)
10598 if ($ this ->vertices ->hasVertexId ($ id )) {
10699 throw new RuntimeException ('Id of cloned vertex already exists ' );
107100 }
108- $ newVertex = new Vertex ($ id , $ this );
101+ $ newVertex = new Vertex ($ this , $ id );
109102 // TODO: properly set attributes of vertex
110103 $ newVertex ->setLayout ($ originalVertex ->getLayout ());
111104 $ newVertex ->setBalance ($ originalVertex ->getBalance ());
112105 $ newVertex ->setGroup ($ originalVertex ->getGroup ());
113- $ this ->verticesStorage [$ id ] = $ newVertex ;
114106
115107 return $ newVertex ;
116108 }
@@ -264,7 +256,7 @@ public function createVertices($n)
264256 $ vertices = array ();
265257 if (is_int ($ n ) && $ n >= 0 ) {
266258 for ($ id = $ this ->getNextId (), $ n += $ id ; $ id < $ n ; ++$ id ) {
267- $ vertices [$ id ] = $ this -> verticesStorage [ $ id ] = new Vertex ($ id , $ this );
259+ $ vertices [$ id ] = new Vertex ($ this , $ id );
268260 }
269261 } elseif (is_array ($ n )) {
270262 // array given => check to make sure all given IDs are available (atomic operation)
@@ -283,7 +275,7 @@ public function createVertices($n)
283275
284276 // actually create all requested vertices
285277 foreach ($ n as $ id ) {
286- $ vertices [$ id ] = $ this -> verticesStorage [ $ id ] = new Vertex ($ id , $ this );
278+ $ vertices [$ id ] = new Vertex ($ this , $ id );
287279 }
288280 } else {
289281 throw new InvalidArgumentException ('Invalid number of vertices given. Must be non-negative integer or an array of Vertex IDs ' );
@@ -332,6 +324,22 @@ public function hasVertex($id)
332324 return $ this ->vertices ->hasVertexId ($ id );
333325 }
334326
327+ /**
328+ * adds a new Vertex to the Graph (MUST NOT be called manually!)
329+ *
330+ * @param Vertex $vertex instance of the new Vertex
331+ * @return void
332+ * @private
333+ * @see self::createVertex() instead!
334+ */
335+ public function addVertex (Vertex $ vertex )
336+ {
337+ if (isset ($ this ->verticesStorage [$ vertex ->getId ()])) {
338+ throw new OverflowException ('ID must be unique ' );
339+ }
340+ $ this ->verticesStorage [$ vertex ->getId ()] = $ vertex ;
341+ }
342+
335343 /**
336344 * adds a new Edge to the Graph (MUST NOT be called manually!)
337345 *
0 commit comments