Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

resturcturing

  • Loading branch information...
commit 563ecad323122101f6631b7bab311e6ee7f73a26 1 parent 52099fd
Jonathan Moss authored
View
23 src/classes/freebase/Constants.php
@@ -0,0 +1,23 @@
+<?php
+/**
+ * @package freebase
+ * @copyright 2011 Tangent One
+ * @author Jonathan Moss <jonathan.moss@tangentone.com.au>
+ */
+namespace freebase;
+/**
+ * @package freebase
+ */
+class Constants
+{
+ /**
+ * Response when call successful
+ */
+ const API_RESPONSE_CODE_OK = '/api/status/ok';
+
+ /**
+ * Response when call failed
+ */
+ const API_RESPONSE_CODE_ERROR = '/api/status/error';
+
+}
View
54 src/classes/freebase/DomFactory.php
@@ -0,0 +1,54 @@
+<?php
+/**
+ * @package freebase
+ * @copyright 2011 Tangent One
+ * @author Jonathan Moss <jonathan.moss@tangentone.com.au>
+ */
+namespace freebase;
+/**
+ * The response factory class provides method to generate a Node DOM from freebase responses
+ *
+ * @package freebase
+ */
+class DomFactory
+{
+
+ /**
+ * @param string $json
+ * @return \freebase\Node
+ */
+ public static function createDomFromJson($json)
+ {
+ $node = null;
+ $data = \json_decode($json, true);
+ if (null === $data) {
+ throw new \freebase\exception\InvalidJson();
+ }
+ if (isset($data['code']) && $data['code'] == Constants::API_RESPONSE_CODE_OK ) {
+ $node = self::createNode($data['result'], 'root');
+ } else {
+ $messages = \implode(". ", $data['messages']);
+ throw new \freebase\exception\ApiError($messages);
+ }
+ return $node;
+ }
+
+ /**
+ * @param array $data
+ * @param string $name
+ * @return \freebase\Node
+ */
+ protected static function createNode(array $data, $name = null)
+ {
+ $node = new Node($name);
+ foreach ($data as $key => $value) {
+ if (\is_scalar($value)) {
+ $node->setAttributeValue($key, $value);
+ } else {
+ $node->addChild(self::createNode($value, $key));
+ }
+ }
+ return $node;
+ }
+
+}
View
9 src/classes/freebase/Exception.php
@@ -1,15 +1,14 @@
<?php
/**
- * @package
- * @copyright 2010 Tangent Labs
- * @version SVN: $Id$
+ * @package freebase
+ * @copyright 2011 Tangent One
* @author Jonathan Moss <jonathan.moss@tangentone.com.au>
*/
namespace freebase;
/**
- * Description of Exception
+ * Base class for freebase exceptions
*
- * @package
+ * @package freebase
*/
class Exception extends \Exception
{
View
75 src/classes/freebase/Freebase.php
@@ -0,0 +1,75 @@
+<?php
+/**
+ * @package freebase
+ * @copyright 2011 Tangent One
+ * @author Jonathan Moss <jonathan.moss@tangentone.com.au>
+ */
+namespace freebase;
+/**
+ * Master interface class to freebase
+ *
+ * @package freebase
+ */
+class Freebase
+{
+
+ private $baseFetchUrl;
+
+ private $baseSearchUrl;
+
+ /**
+ * @param string $baseUrl
+ */
+ public function __construct($baseFetchUrl, $baseSearchUrl)
+ {
+ if (\substr($baseFetchUrl, -1) !== '/') {
+ $baseFetchUrl .= '/';
+ }
+ $this->baseFetchUrl = $baseFetchUrl;
+
+ if (\substr($baseSearchUrl, -1) !== '/') {
+ $baseSearchUrl .= '/';
+ }
+ $this->baseSearchUrl = $baseSearchUrl;
+ }
+
+ /**
+ * @param string $id
+ * @return \freebase\Node
+ */
+ public function fetchById($id)
+ {
+ if (\substr($id, 0, 1) === '/') {
+ $id = \substr($id, 1); //strip first / if needed as already insured in constructor
+ }
+ $url = $this->baseFetchUrl . $id;
+ $json = \file_get_contents($url);
+ return DomFactory::createDomFromJson($json);
+ }
+
+ public function fetchByQuery(Query $query)
+ {
+
+ }
+
+ /**
+ * @param string $url
+ * @param string $jsonData
+ * @return string
+ */
+ protected function doRequest($url, $jsonData = null)
+ {
+ $ch = \curl_init();
+ \curl_setopt($ch, \CURLOPT_URL, $url);
+ \curl_setopt($ch, \CURLOPT_HEADER, false);
+ \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true);
+ if (null !== $jsonData) {
+ \curl_setopt($ch, \CURLOPT_POST, true);
+ \curl_setopt($ch, \CURLOPT_POSTFIELDS, $jsonData);
+ }
+ $response = \curl_exec($ch);
+ \curl_close($ch);
+ return $response;
+ }
+
+}
View
47 src/classes/freebase/Node.php
@@ -1,15 +1,14 @@
<?php
/**
- * @package
- * @copyright 2010 Tangent Labs
- * @version SVN: $Id$
+ * @package freebase
+ * @copyright 2011 Tangent One
* @author Jonathan Moss <jonathan.moss@tangentone.com.au>
*/
namespace freebase;
/**
* Description of Node
*
- * @package
+ * @package freebase
*/
class Node implements \IteratorAggregate
{
@@ -22,7 +21,7 @@ class Node implements \IteratorAggregate
/**
* @var array
*/
- protected $nodes = array();
+ protected $children = array();
/**
* @var \freebase\Node
@@ -32,7 +31,7 @@ class Node implements \IteratorAggregate
/**
* @var array
*/
- protected $properties = array();
+ protected $attributes = array();
/**
* @param string $name
@@ -69,15 +68,15 @@ public function setParent(\freebase\Node $parent)
return $this;
}
- /////////////////////////////
- // PROPERTY ACCESS METHODS //
- /////////////////////////////
+ //////////////////////////////
+ // ATTRIBUTE ACCESS METHODS //
+ //////////////////////////////
/**
* @param mixed $value
* @return \freebase\Node
*/
- public function setProperty($key, $value)
+ public function setAttributeValue($key, $value)
{
$this->properties[$key] = $value;
return $this;
@@ -87,7 +86,7 @@ public function setProperty($key, $value)
* @param string $key
* @return mixed
*/
- public function getProperty($key)
+ public function getAttributeValue($key)
{
$value = null;
if (\array_key_exists($key, $this->properties)) {
@@ -101,13 +100,7 @@ public function getProperty($key)
* @return mixed
*/
public function __get($key) {
- $value = null;
- if (\substr($key, 0, 1) == '_') {
- $value = $this->getProperty(\substr($key, 1));
- } else {
- $value = $this->getNodeByName($key);
- }
- return $value;
+ return $this->getAttributeValue($key);
}
/**
@@ -117,9 +110,7 @@ public function __get($key) {
*/
public function __set($key, $value)
{
- if (\substr($key, 0, 1) == '_') {
- $this->setProperty(\substr($key, 1), $value);
- }
+ $this->setAttributeValue($key, $value);
}
/**
@@ -128,7 +119,7 @@ public function __set($key, $value)
*/
public function __isset($key)
{
- return isset($this->properties[$key]);
+ return isset($this->attributes[$key]);
}
/////////////////////////
@@ -139,17 +130,17 @@ public function __isset($key)
* @param \freebase\Node $node
* @return \freebase\Node
*/
- public function addNode(\freebase\Node $node)
+ public function addChild(\freebase\Node $node)
{
$node->setParent($this);
- $this->nodes[$node->getName()] = $node;
+ $this->children[$node->getName()] = $node;
return $this;
}
/**
* @return array
*/
- public function getNodes()
+ public function getChildren()
{
return $this->nodes;
}
@@ -158,7 +149,7 @@ public function getNodes()
* @param string $name
* @return \freebase\Node
*/
- public function getNodeByName($name)
+ public function getChildByName($name)
{
$node = null;
if (isset($this->nodes[$name])) {
@@ -167,7 +158,7 @@ public function getNodeByName($name)
return $node;
}
- public function getNodeByPath($path)
+ public function getChildByPath($path)
{
$nodeList = \explode(".", $path);
$node = $this;
@@ -189,7 +180,7 @@ public function getNodeByPath($path)
public function getIterator()
{
- return new \ArrayIterator($this->nodes);
+ return new \ArrayIterator($this->children);
}
}
View
46 src/classes/freebase/Query.php
@@ -0,0 +1,46 @@
+<?php
+/**
+ * @package freebase
+ * @copyright 2011 Tangent One
+ * @author Jonathan Moss <jonathan.moss@tangentone.com.au>
+ */
+namespace freebase;
+/**
+ * Object of this class represent a search of freebase
+ *
+ * @package freebase
+ */
+class Query
+{
+
+ protected $criteria = array();
+
+ public function addField($name, $value = null)
+ {
+ $this->criteria[$name] = $value;
+ return $this;
+ }
+
+ /**
+ * @return array
+ */
+ public function getCriteria()
+ {
+ return $this->criteria;
+ }
+
+ /**
+ * @return string
+ */
+ public function __toJson()
+ {
+ $query = array(
+ 'q1'=>array(
+ 'query' => $this->criteria
+ )
+ );
+
+ return \json_encode($query);
+ }
+
+}
View
9 src/classes/freebase/exception/ApiError.php
@@ -1,15 +1,16 @@
<?php
/**
- * @package
- * @copyright 2010 Tangent Labs
- * @version SVN: $Id$
+ * @package freebase
+ * @subpackage exception
+ * @copyright 2011 Tangent One
* @author Jonathan Moss <jonathan.moss@tangentone.com.au>
*/
namespace freebase\exception;
/**
* Description of InvalidJson
*
- * @package
+ * @package freebase
+ * @subpackage exception
*/
class ApiError extends \freebase\Exception
{
View
17 src/classes/freebase/exception/InvalidJson.php
@@ -1,17 +1,24 @@
<?php
/**
- * @package
- * @copyright 2010 Tangent Labs
- * @version SVN: $Id$
+ * @package freebase
+ * @subpackage exception
+ * @copyright 2011 Tangent One
* @author Jonathan Moss <jonathan.moss@tangentone.com.au>
*/
namespace freebase\exception;
/**
* Description of InvalidJson
*
- * @package
+ * @package freebase
+ * @subpackage exception
*/
class InvalidJson extends \freebase\Exception
{
-
+
+ const MESSAGE = "JSON response could not be parsed";
+
+ public function __construct()
+ {
+ parent::__construct(self::MESSAGE);
+ }
}
View
5 src/classes/freebase/exception/InvalidPath.php
@@ -1,13 +1,14 @@
<?php
/**
* @package freebase
- * @copyright 2010 Tangent Labs
- * @version SVN: $Id$
+ * @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
{
Please sign in to comment.
Something went wrong with that request. Please try again.