Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

invalid paths and child names now return an instance of EmptyNode rat…

…her than throwing an exception
  • Loading branch information...
commit 1eeb84bd54998da47b6be52b372cf95e8aa27075 1 parent e4044fe
Jonathan Moss authored
View
4 src/bootstrap.php
@@ -6,7 +6,7 @@
Phar::mapPhar();
-if (php_sapi_name() == 'cli' && isset ($argv[1])) {
+if (php_sapi_name() == 'cli' && isset ($argv[1]) && strtolower(substr($argv[0], -4)) == 'phar') {
$runner = new FreebaseCliRunner();
$runner->processParameters($argv);
} else {
@@ -29,7 +29,6 @@ class FreebaseAutoLoader
//exceptions
'freebase\\exception\\ApiError' => 'phar://Freebase/src/freebase/exception/ApiError.php',
'freebase\\exception\\InvalidJson' => 'phar://Freebase/src/freebase/exception/InvalidJson.php',
- 'freebase\\exception\\InvalidPath' => 'phar://Freebase/src/freebase/exception/InvalidPath.php',
'freebase\\exception\\InvalidQuery' => 'phar://Freebase/src/freebase/exception/InvalidQuery.php',
//real stuff
@@ -38,6 +37,7 @@ class FreebaseAutoLoader
'freebase\\Exception' => 'phar://Freebase/src/freebase/Exception.php',
'freebase\\Freebase' => 'phar://Freebase/src/freebase/Freebase.php',
'freebase\\Node' => 'phar://Freebase/src/freebase/Node.php',
+ 'freebase\\Node' => 'phar://Freebase/src/freebase/EmptyNode.php',
'freebase\\Query' => 'phar://Freebase/src/freebase/Query.php',
);
View
62 src/freebase/EmptyNode.php
@@ -0,0 +1,62 @@
+<?php
+/**
+ * @package freebase
+ * @copyright 2011 Tangent Labs
+ * @version SVN: $Id$
+ * @author Jonathan Moss <jonathan.moss@tangentone.com.au>
+ */
+namespace freebase;
+/**
+ * @package freebase
+ */
+class EmptyNode extends Node
+{
+
+
+ /**
+ * Returns all attributes as an array keyed by the attribute name
+ *
+ * @return array
+ */
+ public function getAttributes()
+ {
+ return array();
+ }
+
+ /**
+ * @param string $key
+ * @return mixed
+ */
+ public function getAttributeValue($key)
+ {
+ return null;
+ }
+
+
+
+ /**
+ * @return array
+ */
+ public function getChildren()
+ {
+ return array($this);
+ }
+
+ /**
+ * @param string $name
+ * @return \freebase\Node
+ */
+ public function getChildByName($name)
+ {
+ return $this;
+ }
+
+ /**
+ * @param string $path Names should be separated by '.' characters
+ * @return \freebase\Node
+ */
+ public function getChildByPath($path)
+ {
+ return $this;
+ }
+}
View
4 src/freebase/Node.php
@@ -161,7 +161,7 @@ public function getChildren()
*/
public function getChildByName($name)
{
- $node = null;
+ $node = new EmptyNode();
if (isset($this->children[$name])) {
$node = $this->children[$name];
}
@@ -183,7 +183,7 @@ public function getChildByPath($path)
$node = $node->getChildByName($name);
}
if (null === $node) {
- throw new exception\InvalidPath($path);
+ $node = new EmptyNode();
}
return $node;
}
View
26 src/freebase/exception/InvalidPath.php
@@ -1,26 +0,0 @@
-<?php
-/**
- * @package freebase
- * @subpackage exception
- * @copyright 2011 Tangent One
- * @author Jonathan Moss <jonathan.moss@tangentone.com.au>
- */
-namespace freebase\exception;
-/**
- * @package freebase
- * @subpackage exception
- */
-class InvalidPath extends \freebase\Exception
-{
-
- const TEMPLATE = "The path %s could not be found in this document";
-
- /**
- * @param string $path
- */
- public function __construct($path)
- {
- parent::__construct(\sprintf(self::TEMPLATE, $path));
- }
-
-}
View
1  tests/unit/freebase/TestDomFactory.php
@@ -9,7 +9,6 @@
require_once \dirname(__FILE__) . '/../../../src/freebase/Exception.php';
require_once \dirname(__FILE__) . '/../../../src/freebase/exception/InvalidJson.php';
-require_once \dirname(__FILE__) . '/../../../src/freebase/exception/InvalidPath.php';
require_once \dirname(__FILE__) . '/../../../src/freebase/Constants.php';
require_once \dirname(__FILE__) . '/../../../src/freebase/Node.php';
require_once \dirname(__FILE__) . '/../../../src/freebase/DomFactory.php';
View
5 tests/unit/freebase/TestNode.php
@@ -8,8 +8,8 @@
namespace freebase;
require_once \dirname(__FILE__) . '/../../../src/freebase/Exception.php';
-require_once \dirname(__FILE__) . '/../../../src/freebase/exception/InvalidPath.php';
require_once \dirname(__FILE__) . '/../../../src/freebase/Node.php';
+require_once \dirname(__FILE__) . '/../../../src/freebase/EmptyNode.php';
/**
* Description of TestQuery
@@ -94,8 +94,7 @@ public function testRetrievingChildByPath()
$this->assertSame($c2, $node->getChildByPath("one.two"));
- $this->setExpectedException('\\freebase\\exception\\InvalidPath');
- $node->getChildByPath("one.zombies.eat.brains");
+ $this->assertTrue($node->getChildByPath("one.zombies.eat.brains") instanceof \freebase\EmptyNode);
}
public function testThatICanGetANodesParent()
Please sign in to comment.
Something went wrong with that request. Please try again.