diff --git a/src/ActiveResource/Base.php b/src/ActiveResource/Base.php index 06e9325..51bd65a 100644 --- a/src/ActiveResource/Base.php +++ b/src/ActiveResource/Base.php @@ -12,7 +12,7 @@ use ActiveResource\Connections\Connection; use ActiveResource\Schemas\Schema; -use ActiveResource\Schemas\BasicSchema; +use ActiveResource\Schemas\AttrSchema; use ActiveResource\Responses\Response; use ActiveResource\Ext\Inflector; @@ -93,7 +93,7 @@ public function getSchema() */ protected function initSchema() { - return new BasicSchema( + return new AttrSchema( $this->schemaDefinition() ); } diff --git a/src/ActiveResource/Connections/Connection.php b/src/ActiveResource/Connections/Connection.php index 861bcfd..550060f 100644 --- a/src/ActiveResource/Connections/Connection.php +++ b/src/ActiveResource/Connections/Connection.php @@ -10,32 +10,179 @@ namespace ActiveResource\Connections; +use ActiveResource\Formats\Format as Format; + +/** + * Connection interface describes base connection object + * + * @package ActiveResource + * @subpackage Connections + * @author Konstantin Kudryashov + * @version 1.0.0 + */ interface Connection { + /** + * Returns site pure URL (without path & user parts) + * + * @return string site URL + */ public function getSite(); + + /** + * Sets base site URL + * + * http://site.com/base/path + * https://user@pass:site.com/base/path + * + * @param string $site base site URL + */ public function setSite($site); + /** + * Sets base path + * + * @param string $path base path to resources + */ public function setBasePath($path); + + /** + * Returns base path + * + * @return string base path to resources + */ public function getBasePath(); - + + /** + * Returns connection username + * + * @return string username (login) + */ public function getUsername(); + + /** + * Returns connection password + * + * @return string password + */ public function getPassword(); + + /** + * Returns connection auth type + * + * @return string auth type + */ public function getAuthType(); + + /** + * Sets connection auth routines + * + * @param string $username username + * @param string $password password + * @param string $auth_type auth type ('basic' or 'digest') + */ public function setAuth($username, $password, $auth_type = 'basic'); + /** + * Returns specific connection header + * + * @param string $name header name + * @return string header + */ public function getHeader($name); + + /** + * Sets specific connection headers + * + * @param array $headers hash of headers + */ public function setHeaders(array $headers); + + /** + * Sets specific connection header + * + * @param string $name header name + * @param string $value header + */ public function setHeader($name, $value); + /** + * Returns connection timeout in ms + * + * @return integer connection timeout + */ public function getTimeout(); + + /** + * Sets connection timeout + * + * @param integer $timeout connection timeout + */ public function setTimeout($timeout); + /** + * Returns connection request/response formatter + * + * @return ActiveResource\Formats\Format formatter instance + */ public function getFormat(); - public function setFormat(\ActiveResource\Formats\Format $format); + /** + * Sets connection request/response formatter + * + * @param ActiveResource\Formats\Format $format formatter instance + */ + public function setFormat(Format $format); + + /** + * Sends HEAD request & returns formatted response object + * + * @param string $path resource path + * @param array $headers specific headers hash + * + * @return ActiveResource\Responses\Response response instance + */ public function head($path, array $headers = array()); + + /** + * Sends GET request & returns formatted response object + * + * @param string $path resource path + * @param array $headers specific headers hash + * + * @return ActiveResource\Responses\Response response instance + */ public function get($path, array $headers = array()); + + /** + * Sends DELETE request & returns formatted response object + * + * @param string $path resource path + * @param array $headers specific headers hash + * + * @return ActiveResource\Responses\Response response instance + */ public function delete($path, array $headers = array()); + + /** + * Sends PUT request & returns formatted response object + * + * @param string $path resource path + * @param array $headers specific headers hash + * @param array $body request body + * + * @return ActiveResource\Responses\Response response instance + */ public function put($path, array $body = array(), array $headers = array()); + + /** + * Sends POST request & returns formatted response object + * + * @param string $path resource path + * @param array $headers specific headers hash + * @param array $body request body + * + * @return ActiveResource\Responses\Response response instance + */ public function post($path, array $body = array(), array $headers = array()); } diff --git a/src/ActiveResource/Connections/HTTPR2Connection.php b/src/ActiveResource/Connections/HTTPR2Connection.php index 1311074..494e084 100644 --- a/src/ActiveResource/Connections/HTTPR2Connection.php +++ b/src/ActiveResource/Connections/HTTPR2Connection.php @@ -32,6 +32,14 @@ require_once 'Net/URL2.php'; require_once 'HTTP/Request2.php'; +/** + * HTTPR2Connection implements Connection interface using HTTP_Request2 PEAR library + * + * @package ActiveResource + * @subpackage Connections + * @author Konstantin Kudryashov + * @version 1.0.0 + */ class HTTPR2Connection implements Connection { protected $site; @@ -41,6 +49,12 @@ class HTTPR2Connection implements Connection protected $format; protected $adapter = 'socket'; + /** + * Connection constructor + * + * @param string $site base site URL + * @param ActiveResource\Formats\Format $format formatter instance + */ public function __construct($site, Format $format = null) { if (null === $site || empty($site)) @@ -59,16 +73,31 @@ public function __construct($site, Format $format = null) } } + /** + * Sets connection adapter, used to send requests & recieve responses + * + * @param mixed $adapter adapter name or object + */ public function setAdapter($adapter) { $this->adapter = $adapter; } + /** + * Returns current connection adapter + * + * @return mixed adapter name or object + */ public function getAdapter() { return $this->adapter; } + /** + * Returns site pure URL (without path & user parts) + * + * @see ActiveResource\Connections\Connection::getSite() + */ public function getSite() { $user_info = $this->site->getUserinfo(); @@ -85,6 +114,11 @@ public function getSite() return $site; } + /** + * Sets base site URL + * + * @see ActiveResource\Connections\Connection::setSite() + */ public function setSite($site) { if ($site instanceof \Net_URL2) @@ -97,31 +131,61 @@ public function setSite($site) } } + /** + * Sets base path + * + * @see ActiveResource\Connections\Connection::setBasePath() + */ public function setBasePath($path) { $this->site->setPath($path); } + /** + * Returns base path + * + * @see ActiveResource\Connections\Connection::getBasePath() + */ public function getBasePath() { return $this->site->getPath(); } + /** + * Returns connection username + * + * @see ActiveResource\Connections\Connection::getUsername() + */ public function getUsername() { return $this->site->getUser(); } + /** + * Returns connection password + * + * @see ActiveResource\Connections\Connection::getPassword() + */ public function getPassword() { return $this->site->getPassword(); } + /** + * Returns connection auth type + * + * @see ActiveResource\Connections\Connection::getAuthType() + */ public function getAuthType() { return $this->auth_type; } + /** + * Sets connection auth routines + * + * @see ActiveResource\Connections\Connection::setAuth() + */ public function setAuth($username, $password, $auth_type = 'basic') { if (!in_array($auth_type, array('basic', 'digest'))) @@ -133,16 +197,31 @@ public function setAuth($username, $password, $auth_type = 'basic') $this->auth_type = $auth_type; } + /** + * Sets specific connection headers + * + * @see ActiveResource\Connections\Connection::setHeaders() + */ public function setHeaders(array $headers) { $this->headers = $headers; } + /** + * Sets specific connection headers + * + * @see ActiveResource\Connections\Connection::setHeader() + */ public function setHeader($name, $value) { $this->headers[$name] = $value; } + /** + * Returns specific connection header + * + * @see ActiveResource\Connections\Connection::getHeader() + */ public function getHeader($name) { if (!isset($this->headers[$name])) @@ -153,26 +232,51 @@ public function getHeader($name) return $this->headers[$name]; } + /** + * Returns connection timeout in ms + * + * @see ActiveResource\Connections\Connection::getTimeout() + */ public function getTimeout() { return $this->timeout; } + /** + * Sets connection timeout + * + * @see ActiveResource\Connections\Connection::setTimeout() + */ public function setTimeout($timeout) { $this->timeout = intval($timeout); } + /** + * Returns connection request/response formatter + * + * @see ActiveResource\Connections\Connection::getFormat() + */ public function getFormat() { return $this->format; } + /** + * Sets connection request/response formatter + * + * @see ActiveResource\Connections\Connection::setFormat() + */ public function setFormat(Format $format) { $this->format = $format; } + /** + * Sends GET request & returns formatted response object + * + * @see ActiveResource\Connections\Connection::get() + */ public function get($path, array $headers = array()) { $response = $this->send('get', $path, null, $headers); @@ -180,6 +284,11 @@ public function get($path, array $headers = array()) return $response; } + /** + * Sends HEAD request & returns formatted response object + * + * @see ActiveResource\Connections\Connection::head() + */ public function head($path, array $headers = array()) { $response = $this->send('head', $path, null, $headers); @@ -187,6 +296,11 @@ public function head($path, array $headers = array()) return $response; } + /** + * Sends DELETE request & returns formatted response object + * + * @see ActiveResource\Connections\Connection::delete() + */ public function delete($path, array $headers = array()) { $response = $this->send('delete', $path, null, $headers); @@ -194,6 +308,11 @@ public function delete($path, array $headers = array()) return $response; } + /** + * Sends PUT request & returns formatted response object + * + * @see ActiveResource\Connections\Connection::put() + */ public function put($path, array $body = array(), array $headers = array()) { $body = $this->format->encode($body); @@ -202,6 +321,11 @@ public function put($path, array $body = array(), array $headers = array()) return $response; } + /** + * Sends POST request & returns formatted response object + * + * @see ActiveResource\Connections\Connection::post() + */ public function post($path, array $body = array(), array $headers = array()) { $body = $this->format->encode($body); @@ -210,6 +334,16 @@ public function post($path, array $body = array(), array $headers = array()) return $response; } + /** + * Perform request creation, sending & response preparing + * + * @param string $method method name (GET, HEAD, DELETE, POST, PUT) + * @param string $path resource path + * @param string $body request body + * @param array $headers headers hash + * + * @return ActiveResource\Responses\Response response instance + */ protected function send($method, $path, $body = null, array $headers = array()) { $request = $this->prepareRequest($method, $path, $body, $headers); @@ -291,6 +425,16 @@ protected function send($method, $path, $body = null, array $headers = array()) } } + /** + * Creates & prepares new request object + * + * @param string $method method name (GET, HEAD, DELETE, POST, PUT) + * @param string $path resource path + * @param string $body request body + * @param array $headers headers hash + * + * @return HTTP_Request2 request object + */ protected function prepareRequest($method, $path, $body, array $headers) { $request = new \HTTP_Request2($this->prepareUrl($path)); @@ -325,6 +469,13 @@ protected function prepareRequest($method, $path, $body, array $headers) return $request; } + /** + * Prepares request URL + * + * @param string $path resource path + * + * @return string request URL + */ protected function prepareUrl($path) { $site = new \Net_URL2($this->getSite()); @@ -335,6 +486,13 @@ protected function prepareUrl($path) return $site->getUrl(); } + /** + * Prepares request headers + * + * @param array $headers headers hash + * + * @return array prepared headers + */ protected function prepareHeaders(array $headers = array()) { return array_merge( @@ -347,6 +505,13 @@ protected function prepareHeaders(array $headers = array()) ); } + /** + * Converts HTTP_Request2 response object into ActiveResource's one + * + * @param HTTP_Request2_Response $response recieved response object + * + * @return ActiveResource\Responses\Response response object + */ protected function prepareResponse(\HTTP_Request2_Response $response) { $body = trim($response->getBody()); diff --git a/src/ActiveResource/Resources/ActiveResource.php b/src/ActiveResource/Resources/ActiveResource.php index 1d5d6d9..d982c9e 100644 --- a/src/ActiveResource/Resources/ActiveResource.php +++ b/src/ActiveResource/Resources/ActiveResource.php @@ -25,10 +25,10 @@ class ActiveResource protected $resource_class; protected $connection; - public function __construct($resource_class, Connection $connection) + public function __construct($class, Connection $connection) { - $this->resource_class = $resource_class; - $this->connection = $connection; + $this->resource_class = $class; + $this->connection = $connection; } public function __call($func, array $args) diff --git a/src/ActiveResource/Schemas/BasicSchema.php b/src/ActiveResource/Schemas/BasicSchema.php deleted file mode 100644 index afce9a4..0000000 --- a/src/ActiveResource/Schemas/BasicSchema.php +++ /dev/null @@ -1,92 +0,0 @@ -setAttributes($attrs); - } - - public function setAttributes(array $attrs) - { - foreach($attrs as $name => $type) - { - $this->setAttribute($name, $type); - } - } - - public function setAttribute($name, $type) - { - $this->available_attrs[$name] = $type; - } - - public function isAttributesSet() - { - return !empty($this->available_attrs); - } - - public function hasAttribute($name) - { - if (!$this->isAttributesSet()) - { - return true; - } - - return isset($this->available_attrs[$name]); - } - - public function getAttributeType($name) - { - if ($this->isAttributesSet()) - { - return $thid->default_attr_type; - } - - if (!$this->hasAttribute($name)) - { - throw new \InvalidArgumentException(sprintf('No such attribute: %s', $name)); - } - - return $this->available_attrs[$name]; - } - - public function isAttributeType($name, $type) - { - if ($this->isAttributesSet()) - { - return $type === $thid->default_attr_type; - } - - if (!$this->hasAttribute($name)) - { - throw new \InvalidArgumentException(sprintf('No such attribute: %s', $name)); - } - - return $type === $this->getAttributeType($name); - } - - public function prepareAttrForSet($name, $value) - { - if (!$this->hasAttribute($name)) - { - throw new \InvalidArgumentException(sprintf('No such attribute: %s', $name)); - } - - - } - - public function prepareAttrForGet($name, $value) - { - if (!$this->hasAttribute($name)) - { - throw new \InvalidArgumentException(sprintf('No such attribute: %s', $name)); - } - - - } -} diff --git a/src/ActiveResource/Schemas/Schema.php b/src/ActiveResource/Schemas/Schema.php index 69a5372..d1b1f1e 100644 --- a/src/ActiveResource/Schemas/Schema.php +++ b/src/ActiveResource/Schemas/Schema.php @@ -4,16 +4,17 @@ interface Schema { - public function __construct(array $attrs = array()); public function setAttributes(array $attrs); public function setAttribute($name, $type); - public function isAttributesSet(); + public function isDefined(); public function hasAttribute($name); public function getAttributeType($name); public function isAttributeType($name, $type); - public function prepareAttrForSet($name, $value); - public function prepareAttrForGet($name, $value); + public function set($name, $value = null); + public function get($name); + public function setValues(array $values); + public function getValuesArray(); }