Skip to content

Commit

Permalink
Merge pull request #37 from CircleOfNice/api-v1
Browse files Browse the repository at this point in the history
API v1
  • Loading branch information
TobiasHauck authored Aug 11, 2016
2 parents 3503a2d + b4fa00a commit 3e97248
Show file tree
Hide file tree
Showing 92 changed files with 2,715 additions and 665 deletions.
56 changes: 56 additions & 0 deletions Annotations/DataSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* This file is part of DoctrineRestDriver.
*
* DoctrineRestDriver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DoctrineRestDriver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DoctrineRestDriver. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Circle\DoctrineRestDriver\Annotations;

/**
* Contract for all data source annotations
*
* @author Tobias Hauck <[email protected]>
* @copyright 2015 TeeAge-Beatz UG
*/
interface DataSource {

/**
* returns the route
*
* @return string
*/
public function getRoute();

/**
* returns the status code
*
* @return int|null
*/
public function getStatusCode();

/**
* returns the method
*
* @return null|string
*/
public function getMethod();

/**
* returns the options
*
* @return array|null
*/
public function getOptions();
}
2 changes: 1 addition & 1 deletion Annotations/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
*
* @Annotation
*/
class Delete {
class Delete implements DataSource{
use Route;
}
2 changes: 1 addition & 1 deletion Annotations/Fetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
*
* @Annotation
*/
class Fetch {
class Fetch implements DataSource {
use Route;
}
2 changes: 1 addition & 1 deletion Annotations/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
*
* @Annotation
*/
class Insert {
class Insert implements DataSource {
use Route;
}
2 changes: 1 addition & 1 deletion Annotations/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ public function __construct() {
public function read(\ReflectionClass $class, $namespace) {
$annotation = $this->annotationReader->getClassAnnotation($class, $namespace);

return $annotation instanceof $namespace ? $annotation->getRoute() : null;
return $annotation instanceof $namespace ? $annotation : null;
}
}
58 changes: 53 additions & 5 deletions Annotations/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

namespace Circle\DoctrineRestDriver\Annotations;

use Circle\DoctrineRestDriver\Exceptions\Exceptions;
use Circle\DoctrineRestDriver\Validation\Assertions;
use Circle\DoctrineRestDriver\Types\MaybeInt;
use Circle\DoctrineRestDriver\Types\MaybeList;
use Circle\DoctrineRestDriver\Types\MaybeString;
use Circle\DoctrineRestDriver\Types\Url;
use Doctrine\Common\Collections\ArrayCollection;

/**
* Trait for all annotations regarding routes
Expand All @@ -36,6 +39,21 @@ trait Route {
*/
private $route;

/**
* @var string
*/
private $method;

/**
* @var int
*/
private $statusCode;

/**
* @var array
*/
private $options = [];

/**
* Constructor
*
Expand All @@ -44,9 +62,12 @@ trait Route {
* @SuppressWarnings("PHPMD.StaticAccess")
*/
public function __construct(array $values) {
$route = $values['value'];
if (!Assertions::isUrl($route)) return Exceptions::InvalidTypeException('Url', 'route', $route);
$this->route = $route;
$settings = new ArrayCollection($values);

$this->route = Url::assert($settings->get('value'), 'value');
$this->statusCode = MaybeInt::assert($settings->get('statusCode'), 'statusCode');
$this->method = MaybeString::assert($settings->get('method'), 'method');
$this->options = MaybeList::assert($settings->get('options'), 'options');
}

/**
Expand All @@ -57,4 +78,31 @@ public function __construct(array $values) {
public function getRoute() {
return $this->route;
}

/**
* returns the status code
*
* @return int|null
*/
public function getStatusCode() {
return $this->statusCode;
}

/**
* returns the method
*
* @return string|null
*/
public function getMethod() {
return $this->method;
}

/**
* returns the options
*
* @return array|null
*/
public function getOptions() {
return $this->options;
}
}
10 changes: 5 additions & 5 deletions Annotations/Routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function __construct($namespace) {
/**
* returns the post route
*
* @return string|null
* @return \Circle\DoctrineRestDriver\Annotations\Insert|null
*/
public function post() {
return $this->post;
Expand All @@ -86,7 +86,7 @@ public function post() {
/**
* returns the get route
*
* @return string|null
* @return \Circle\DoctrineRestDriver\Annotations\Select|null
*/
public function get() {
return $this->get;
Expand All @@ -95,7 +95,7 @@ public function get() {
/**
* returns the put route
*
* @return string|null
* @return \Circle\DoctrineRestDriver\Annotations\Update|null
*/
public function put() {
return $this->put;
Expand All @@ -104,7 +104,7 @@ public function put() {
/**
* returns the delete route
*
* @return string|null
* @return \Circle\DoctrineRestDriver\Annotations\Delete|null
*/
public function delete() {
return $this->delete;
Expand All @@ -113,7 +113,7 @@ public function delete() {
/**
* returns the get all route
*
* @return string|null
* @return \Circle\DoctrineRestDriver\Annotations\Fetch|null
*/
public function getAll() {
return $this->getAll;
Expand Down
5 changes: 2 additions & 3 deletions Annotations/RoutingTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

namespace Circle\DoctrineRestDriver\Annotations;

use Circle\DoctrineRestDriver\Validation\Assertions;
use Doctrine\Common\Annotations\AnnotationReader;
use Circle\DoctrineRestDriver\Types\HashMap;

/**
* Contains all routing information about all entities
Expand All @@ -42,7 +41,7 @@ class RoutingTable {
* @SuppressWarnings("PHPMD.StaticAccess")
*/
public function __construct(array $entities) {
Assertions::assertHashMap('entities', $entities);
HashMap::assert($entities, 'entities');

$aliases = array_flip($entities);
$this->routingTable = array_reduce($entities, function ($carry, $namespace) use ($aliases) {
Expand Down
2 changes: 1 addition & 1 deletion Annotations/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
*
* @Annotation
*/
class Select {
class Select implements DataSource {
use Route;
}
2 changes: 1 addition & 1 deletion Annotations/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
*
* @Annotation
*/
class Update {
class Update implements DataSource {
use Route;
}
27 changes: 27 additions & 0 deletions Exceptions/DoctrineRestDriverException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* This file is part of DoctrineRestDriver.
*
* DoctrineRestDriver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DoctrineRestDriver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DoctrineRestDriver. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Circle\DoctrineRestDriver\Exceptions;

/**
* Generic driver exception
*
* @author Tobias Hauck <[email protected]>
* @copyright 2015 TeeAge-Beatz UG
*/
class DoctrineRestDriverException extends \Exception {}
12 changes: 11 additions & 1 deletion Exceptions/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Exceptions {
* @throws InvalidTypeException
*/
public static function InvalidTypeException($expectedType, $key, $value) {
throw new InvalidTypeException($expectedType, $key, $value);
throw new InvalidTypeException($expectedType, $key, is_array($value) ? serialize($value) : $value);
}

/**
Expand Down Expand Up @@ -85,6 +85,16 @@ public static function InvalidAuthStrategyException($class) {
throw new InvalidAuthStrategyException($class);
}

/**
* throws an invalid format exception
*
* @param string $class
* @throws InvalidFormatException
*/
public static function InvalidFormatException($class) {
throw new InvalidFormatException($class);
}

/**
* throws a method not implemented exception
*
Expand Down
2 changes: 1 addition & 1 deletion Exceptions/InvalidAuthStrategyException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
* @SuppressWarnings("PHPMD.StaticAccess")
*/
class InvalidAuthStrategyException extends \Exception {
class InvalidAuthStrategyException extends DoctrineRestDriverException {

/**
* InvalidAuthStrategyException constructor
Expand Down
39 changes: 39 additions & 0 deletions Exceptions/InvalidFormatException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* This file is part of DoctrineRestDriver.
*
* DoctrineRestDriver is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DoctrineRestDriver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DoctrineRestDriver. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Circle\DoctrineRestDriver\Exceptions;

/**
* Exception class for invalid format
*
* @author Tobias Hauck <[email protected]>
* @copyright 2015 TeeAge-Beatz UG
*
* @SuppressWarnings("PHPMD.StaticAccess")
*/
class InvalidFormatException extends DoctrineRestDriverException {

/**
* InvalidFormatException constructor
*
* @param string $class
*/
public function __construct($class) {
parent::__construct('Class ' . $class . ' must implement Circle\DoctrineRestDriver\Formatters\Formatter to act as formatter');
}
}
2 changes: 1 addition & 1 deletion Exceptions/InvalidSqlOperationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
* @SuppressWarnings("PHPMD.StaticAccess")
*/
class InvalidSqlOperationException extends \Exception {
class InvalidSqlOperationException extends DoctrineRestDriverException {

/**
* InvalidSqlOperationException constructor
Expand Down
2 changes: 1 addition & 1 deletion Exceptions/MethodNotImplementedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* @SuppressWarnings("PHPMD.StaticAccess")
*/
class MethodNotImplementedException extends \Exception {
class MethodNotImplementedException extends DoctrineRestDriverException {

/**
* InvalidSqlOperationException constructor
Expand Down
4 changes: 2 additions & 2 deletions Exceptions/RequestFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*
* @SuppressWarnings("PHPMD.StaticAccess")
*/
class RequestFailedException extends \Exception {
class RequestFailedException extends DoctrineRestDriverException {

/**
* RequestFailedException constructor
Expand All @@ -39,6 +39,6 @@ class RequestFailedException extends \Exception {
* @param string $errorMessage
*/
public function __construct(Request $request, $errorCode, $errorMessage) {
parent::__construct('Execution failed for request: ' . $request . ': HTTPCode ' . $errorCode . ', body ' . $errorMessage);
parent::__construct('Execution failed for request: ' . $request . ': HTTPCode ' . $errorCode . ', body ' . $errorMessage, $errorCode);
}
}
Loading

0 comments on commit 3e97248

Please sign in to comment.