Skip to content
Browse files

correct issue with Node::getChildByPath

  • Loading branch information...
1 parent 0deecf5 commit 5da09c6155704f35576b7caa40e089f6dc69bcb0 Jonathan Moss committed Feb 25, 2011
View
46 src/classes/freebase/DomFactory.php
@@ -17,27 +17,55 @@ class DomFactory
* @param string $json
* @return \freebase\Node
*/
- public static function createDomFromJson($json)
+ public static function createDomFromJson($json, $id = null)
{
$node = null;
$data = \json_decode($json, true);
if (null === $data) {
- throw new \freebase\exception\InvalidJson();
+ throw new \freebase\exception\InvalidJson($json);
}
- if (isset($data['code']) && $data['code'] == Constants::API_RESPONSE_CODE_OK ) {
- $node = self::createNode($data['result'], 'root');
- } else {
- if (\is_array($data['messages'][0])) {
- $message = $data['messages'][0]['message'];
+ if (self::getResponseCode($data, $id) == Constants::API_RESPONSE_CODE_OK ) {
+ if (null === $id) {
+ $node = self::createNode(self::getResultNode($data), 'root');
} else {
- $message = \implode(". ", $data['messages']);
+ $node = self::createNode(self::getResultNode($data, $id), $id);
}
- throw new \freebase\exception\ApiError($message);
+
+ } else {
+ throw new \freebase\exception\ApiError("");
}
return $node;
}
/**
+ *
+ * @param array $data
+ * @param string $id
+ * @return string
+ */
+ protected static function getResponseCode(array $data, $id = null)
+ {
+ $code = null;
+ if (null == $id) {
+ $code = isset($data['code']) ? $data['code'] : null;
+ } else {
+ $code = isset($data[$id]['code']) ? $data[$id]['code'] : null;
+ }
+ return $code;
+ }
+
+ protected static function getResultNode(array $data, $id = null)
+ {
+ $result = null;
+ if (null == $id) {
+ $result = $data['result'];
+ } else {
+ $result = $data[$id]['result'];
+ }
+ return $result;
+ }
+
+ /**
* @param array $data
* @param string $name
* @return \freebase\Node
View
12 src/classes/freebase/Freebase.php
@@ -13,7 +13,7 @@
class Freebase
{
- const PATH_TOPIC = 'experimental/topic/standard/';
+ const PATH_TOPIC = 'experimental/topic/standard/?id=';
const PATH_MQLREAD = 'service/mqlread';
@@ -37,14 +37,14 @@ public function __construct($baseUrl = "http://api.freebase.com/api/")
* @param string $id
* @return \freebase\Node
*/
- public function fetchByTopicId($id)
+ public function fetchByTopicId($id, array $domains = array())
{
- if (\substr($id, 0, 1) === '/') {
- $id = \substr($id, 1); //strip first / if needed as already insured in constructor
- }
$url = $this->baseUrl . self::PATH_TOPIC . $id;
+ if (!empty($domains)) {
+ $url .= '?' . \implode(',', $domains);
+ }
$json = $this->doRequest($url);
- return DomFactory::createDomFromJson($json);
+ return DomFactory::createDomFromJson($json, $id);
}
/**
View
6 src/classes/freebase/Node.php
@@ -152,7 +152,7 @@ public function addChild(\freebase\Node $node)
*/
public function getChildren()
{
- return $this->nodes;
+ return $this->children;
}
/**
@@ -162,8 +162,8 @@ public function getChildren()
public function getChildByName($name)
{
$node = null;
- if (isset($this->nodes[$name])) {
- $node = $this->nodes[$name];
+ if (isset($this->children[$name])) {
+ $node = $this->children[$name];
}
return $node;
}
View
4 src/classes/freebase/exception/InvalidJson.php
@@ -17,8 +17,8 @@ class InvalidJson extends \freebase\Exception
const MESSAGE = "JSON response could not be parsed";
- public function __construct()
+ public function __construct($json)
{
- parent::__construct(self::MESSAGE);
+ parent::__construct(self::MESSAGE . $json);
}
}
View
2 src/classes/freebase/exception/InvalidPath.php
@@ -20,7 +20,7 @@ class InvalidPath extends \freebase\Exception
*/
public function __construct($path)
{
- parent::__construct(\sprint_f(self::TEMPLATE, $path));
+ parent::__construct(\sprintf(self::TEMPLATE, $path));
}
}

0 comments on commit 5da09c6

Please sign in to comment.
Something went wrong with that request. Please try again.