Skip to content

Commit

Permalink
adding base structure
Browse files Browse the repository at this point in the history
  • Loading branch information
a-musing-moose committed Feb 24, 2011
1 parent 147007a commit 52099fd
Show file tree
Hide file tree
Showing 10 changed files with 497 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
nbproject
*.*~
22 changes: 22 additions & 0 deletions 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';
}
17 changes: 17 additions & 0 deletions 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
{

}
55 changes: 55 additions & 0 deletions 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;
}

}
195 changes: 195 additions & 0 deletions 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);
}

}
37 changes: 37 additions & 0 deletions 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);
}

}
17 changes: 17 additions & 0 deletions 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
{

}
17 changes: 17 additions & 0 deletions 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
{

}

0 comments on commit 52099fd

Please sign in to comment.