diff --git a/src/Node.php b/src/Node.php index 73cc1ac..be0dfe9 100644 --- a/src/Node.php +++ b/src/Node.php @@ -241,7 +241,7 @@ public function validateCanHaveParentOfType() return; } - throw new InvalidParentNodeTypeException('Parent does not allow node type of name "' . $this->getNodeTypeName() .'" as child.'); + throw new InvalidParentNodeTypeException('Parent does not allow node type of name "' . $this->getNodeTypeName() . '" as child.'); } /** @@ -1123,6 +1123,26 @@ public function scopeRecentlyCreated($query, $limit = null) return $query; } + /** + * Gets the full url for node + * + * @param string $locale + * @return string + */ + public function getNodeUrl($locale = null) + { + $node = $this; + $uri = ''; + + do + { + $uri = '/' . $node->getTranslationAttribute('node_name', $locale) . $uri; + $node = $node->parent; + } while ( ! is_null($node)); + + return url($uri); + } + /** * Determines the default edit link for node * diff --git a/tests/NodeTest.php b/tests/NodeTest.php index d0c1730..d55ceee 100644 --- a/tests/NodeTest.php +++ b/tests/NodeTest.php @@ -1282,6 +1282,74 @@ function it_scopes_nodes_with_type() ); } + /** @test */ + function it_gets_node_url() + { + $root = $this->getNode(); + $root->fill([ + 'en' => [ + 'title' => 'Root', + 'node_name' => 'root' + ], + 'tr' => [ + 'title' => 'Kök', + 'node_name' => 'kok' + ] + ])->save(); + + $this->assertEquals( + $root->getNodeUrl(), + 'http://localhost/root' + ); + + $this->assertEquals( + $root->getNodeUrl('en'), + 'http://localhost/root' + ); + + $this->assertEquals( + $root->getNodeUrl('tr'), + 'http://localhost/kok' + ); + + $mid = $this->getNode(); + $mid->fill([ + 'title' => 'Mid', + 'node_name' => 'mid' + ]); + $mid->appendToNode($root); + $mid->save(); + + $leaf = $this->getNode(); + $leaf->fill([ + 'en' => [ + 'title' => 'Leaf', + 'node_name' => 'leaf' + ], + 'tr' => [ + 'title' => 'Yaprak', + 'node_name' => 'yaprak' + ] + ]); + $leaf->appendToNode($mid); + $leaf->save(); + + $this->assertEquals( + $leaf->getNodeUrl(), + 'http://localhost/root/mid/leaf' + ); + + $this->assertEquals( + $leaf->getNodeUrl('en'), + 'http://localhost/root/mid/leaf' + ); + + $this->assertEquals( + $leaf->getNodeUrl('tr'), + 'http://localhost/kok/mid/yaprak' + ); + } + /** @test */ function it_makes_the_default_edit_link() {