Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding base structure

  • Loading branch information...
commit 52099fd6eefb8d3351d46e25054f19fb212e85c5 1 parent 147007a
Jonathan Moss authored
View
2  .gitignore
@@ -0,0 +1,2 @@
+nbproject
+*.*~
View
22 src/classes/freebase/Enum.php
@@ -0,0 +1,22 @@
+<?php
+/**
+ * @package freebase
+ * @copyright 2010 Tangent Labs
+ * @version SVN: $Id$
+ * @author Jonathan Moss <jonathan.moss@tangentone.com.au>
+ */
+namespace freebase;
+/**
+ * @package freebase
+ */
+class Enum
+{
+ const API_RESPONSE_CODE_OK = '/api/status/ok';
+ const API_RESPONSE_CODE_ERROR = '/api/status/error';
+
+ /**
+ * Attribute types that need special handling
+ */
+ const ATTRIB_TYPE = 'type';
+ const ATTRIB_EXPECTED_TYPE = 'expected_type';
+}
View
17 src/classes/freebase/Exception.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * @package
+ * @copyright 2010 Tangent Labs
+ * @version SVN: $Id$
+ * @author Jonathan Moss <jonathan.moss@tangentone.com.au>
+ */
+namespace freebase;
+/**
+ * Description of Exception
+ *
+ * @package
+ */
+class Exception extends \Exception
+{
+
+}
View
55 src/classes/freebase/Factory.php
@@ -0,0 +1,55 @@
+<?php
+/**
+ * @package
+ * @copyright 2010 Tangent Labs
+ * @version SVN: $Id$
+ * @author Jonathan Moss <jonathan.moss@tangentone.com.au>
+ */
+namespace freebase;
+/**
+ * Description of Factory
+ *
+ * @package
+ */
+class Factory
+{
+
+ /**
+ * @param string $json
+ * @return \freebase\Node
+ */
+ public static function loadFromJson($json)
+ {
+ $node = null;
+ $data = \json_decode($json, true);
+ if (null === $data) {
+ throw new \freebase\exception\InvalidJson();
+ }
+ if (isset($data['code']) && $data['code'] == Enum::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->setProperty($key, $value);
+ } else {
+ $node->addNode(self::createNode($value, $key));
+ }
+ }
+ return $node;
+ }
+
+}
View
195 src/classes/freebase/Node.php
@@ -0,0 +1,195 @@
+<?php
+/**
+ * @package
+ * @copyright 2010 Tangent Labs
+ * @version SVN: $Id$
+ * @author Jonathan Moss <jonathan.moss@tangentone.com.au>
+ */
+namespace freebase;
+/**
+ * Description of Node
+ *
+ * @package
+ */
+class Node implements \IteratorAggregate
+{
+
+ /**
+ * @var string
+ */
+ protected $name;
+
+ /**
+ * @var array
+ */
+ protected $nodes = array();
+
+ /**
+ * @var \freebase\Node
+ */
+ protected $parent = null;
+
+ /**
+ * @var array
+ */
+ protected $properties = array();
+
+ /**
+ * @param string $name
+ */
+ public function __construct($name = null)
+ {
+ $this->name = $name;
+ }
+
+ /**
+ * @return string
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
+ * @return \freebase\Node
+ */
+ public function getParent()
+ {
+ return $this->parent;
+ }
+
+
+ /**
+ * @param \freebase\Node $parent
+ * @return \freebase\Node
+ */
+ public function setParent(\freebase\Node $parent)
+ {
+ $this->parent = $parent;
+ return $this;
+ }
+
+ /////////////////////////////
+ // PROPERTY ACCESS METHODS //
+ /////////////////////////////
+
+ /**
+ * @param mixed $value
+ * @return \freebase\Node
+ */
+ public function setProperty($key, $value)
+ {
+ $this->properties[$key] = $value;
+ return $this;
+ }
+
+ /**
+ * @param string $key
+ * @return mixed
+ */
+ public function getProperty($key)
+ {
+ $value = null;
+ if (\array_key_exists($key, $this->properties)) {
+ $value = $this->properties[$key];
+ }
+ return $value;
+ }
+
+ /**
+ * @param string $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;
+ }
+
+ /**
+ * @param string $key
+ * @param mixed $value
+ * @return void
+ */
+ public function __set($key, $value)
+ {
+ if (\substr($key, 0, 1) == '_') {
+ $this->setProperty(\substr($key, 1), $value);
+ }
+ }
+
+ /**
+ * @param string $key
+ * @return boolean
+ */
+ public function __isset($key)
+ {
+ return isset($this->properties[$key]);
+ }
+
+ /////////////////////////
+ // NODE ACCESS METHODS //
+ /////////////////////////
+
+ /**
+ * @param \freebase\Node $node
+ * @return \freebase\Node
+ */
+ public function addNode(\freebase\Node $node)
+ {
+ $node->setParent($this);
+ $this->nodes[$node->getName()] = $node;
+ return $this;
+ }
+
+ /**
+ * @return array
+ */
+ public function getNodes()
+ {
+ return $this->nodes;
+ }
+
+ /**
+ * @param string $name
+ * @return \freebase\Node
+ */
+ public function getNodeByName($name)
+ {
+ $node = null;
+ if (isset($this->nodes[$name])) {
+ $node = $this->nodes[$name];
+ }
+ return $node;
+ }
+
+ public function getNodeByPath($path)
+ {
+ $nodeList = \explode(".", $path);
+ $node = $this;
+ foreach ($nodeList as $name) {
+ if (null === $node) {
+ break;
+ }
+ $node = $node->getNodeByName($name);
+ }
+ if (null === $node) {
+ throw new exception\InvalidPath($path);
+ }
+ return $node;
+ }
+
+ /////////////////////////////////
+ // IteratorAggregate INTERFACE //
+ /////////////////////////////////
+
+ public function getIterator()
+ {
+ return new \ArrayIterator($this->nodes);
+ }
+
+}
View
37 src/classes/freebase/Transport.php
@@ -0,0 +1,37 @@
+<?php
+/**
+ * @package
+ * @copyright 2010 Tangent Labs
+ * @version SVN: $Id$
+ * @author Jonathan Moss <jonathan.moss@tangentone.com.au>
+ */
+namespace freebase;
+/**
+ * Description of Transport
+ *
+ * @package
+ */
+class Transport
+{
+
+ private $baseUrl;
+
+ /**
+ * @param string $baseUrl
+ */
+ public function __construct($baseUrl)
+ {
+ if (\substr($baseUrl, -1) !== '/') {
+ $baseUrl .= '/';
+ }
+ $this->baseUrl = $baseUrl;
+ }
+
+ public function query($uri)
+ {
+ $url = $this->baseUrl . $uri;
+ $json = \file_get_contents($url);
+ return Factory::loadFromJson($json);
+ }
+
+}
View
17 src/classes/freebase/exception/ApiError.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * @package
+ * @copyright 2010 Tangent Labs
+ * @version SVN: $Id$
+ * @author Jonathan Moss <jonathan.moss@tangentone.com.au>
+ */
+namespace freebase\exception;
+/**
+ * Description of InvalidJson
+ *
+ * @package
+ */
+class ApiError extends \freebase\Exception
+{
+
+}
View
17 src/classes/freebase/exception/InvalidJson.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * @package
+ * @copyright 2010 Tangent Labs
+ * @version SVN: $Id$
+ * @author Jonathan Moss <jonathan.moss@tangentone.com.au>
+ */
+namespace freebase\exception;
+/**
+ * Description of InvalidJson
+ *
+ * @package
+ */
+class InvalidJson extends \freebase\Exception
+{
+
+}
View
25 src/classes/freebase/exception/InvalidPath.php
@@ -0,0 +1,25 @@
+<?php
+/**
+ * @package freebase
+ * @copyright 2010 Tangent Labs
+ * @version SVN: $Id$
+ * @author Jonathan Moss <jonathan.moss@tangentone.com.au>
+ */
+namespace freebase\exception;
+/**
+ * @package freebase
+ */
+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(\sprint_f(self::TEMPLATE, $path));
+ }
+
+}
View
110 tests/freebase/topic.json
@@ -0,0 +1,110 @@
+{
+ "code": "/api/status/ok",
+ "result": {
+ "alias": [],
+ "id": "/soft/isbn/9780006476153/best",
+ "properties": {
+ "/book/book_edition/book": {
+ "expected_type": {
+ "id": "/book/book",
+ "text": "Book"
+ },
+ "text": "Edition Of",
+ "values": [
+ {
+ "id": "/en/along_came_a_spider",
+ "text": "Along Came a Spider",
+ "url": "http://www.freebase.com/view/en/along_came_a_spider"
+ }
+ ]
+ },
+ "/book/book_edition/dewey_decimal_number": {
+ "expected_type": {
+ "id": "/type/text",
+ "text": "Text"
+ },
+ "text": "Dewey decimal number",
+ "values": [
+ {
+ "text": "813",
+ "value": "813"
+ }
+ ]
+ },
+ "/book/book_edition/isbn": {
+ "expected_type": {
+ "id": "/book/isbn",
+ "text": "ISBN"
+ },
+ "text": "ISBN",
+ "values": [
+ {
+ "id": "/soft/isbn/9780006476153",
+ "text": "9780006476153",
+ "url": "http://www.freebase.com/view/soft/isbn/9780006476153"
+ }
+ ]
+ },
+ "/book/book_edition/number_of_pages": {
+ "expected_type": {
+ "id": "/book/pagination",
+ "text": "Pagination"
+ },
+ "properties": [
+ {
+ "expected_type": {
+ "id": "/type/int",
+ "text": "Integer"
+ },
+ "id": "/book/pagination/numbered_pages",
+ "text": "Numbered pages"
+ }
+ ],
+ "text": "Number of pages",
+ "values": [
+ {
+ "/book/pagination/numbered_pages": {
+ "expected_type": {
+ "id": "/type/int",
+ "text": "Integer"
+ },
+ "text": "Numbered pages",
+ "values": [
+ {
+ "text": "435",
+ "value": 435
+ }
+ ]
+ },
+ "id": "/m/04vd7l_",
+ "text": "435"
+ }
+ ]
+ },
+ "/book/book_edition/publication_date": {
+ "expected_type": {
+ "id": "/type/datetime",
+ "text": "Date/Time"
+ },
+ "text": "Publication date",
+ "values": [
+ {
+ "text": "1994",
+ "value": "1994"
+ }
+ ]
+ }
+ },
+ "text": "Along came a spider",
+ "type": [
+ {
+ "id": "/book/book_edition",
+ "text": "Book Edition"
+ }
+ ],
+ "url": "http://www.freebase.com/view/soft/isbn/9780006476153/best",
+ "webpage": []
+ },
+ "status": "200 OK",
+ "transaction_id": "cache;cache02.p01.sjc1:8101;2011-02-23T23:51:28Z;0011"
+}
Please sign in to comment.
Something went wrong with that request. Please try again.