Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
147007a
commit 52099fd
Showing
10 changed files
with
497 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
nbproject | |||
*.*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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'; | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 | |||
{ | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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; | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 | |||
{ | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 | |||
{ | |||
|
|||
} |
Oops, something went wrong.