From 5166024bcab8626a5d773971fa2db5f874837bf4 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 14 Feb 2017 17:14:59 +0200 Subject: [PATCH] extended classes --- app/library/App/Mvc/ExtendedApiCollection.php | 47 +++++++++++++++++++++++++++++++++++++++++++++++ app/library/App/Mvc/ExtendedApiEndpoint.php | 37 +++++++++++++++++++++++++++++++++++++ app/library/App/Mvc/ExtendedApiResource.php | 225 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ app/library/App/Resources/GaResource.php | 7 +++++-- 4 files changed, 314 insertions(+), 2 deletions(-) create mode 100644 app/library/App/Mvc/ExtendedApiCollection.php create mode 100644 app/library/App/Mvc/ExtendedApiEndpoint.php create mode 100644 app/library/App/Mvc/ExtendedApiResource.php diff --git a/app/library/App/Mvc/ExtendedApiCollection.php b/app/library/App/Mvc/ExtendedApiCollection.php new file mode 100644 index 0000000..cb6c583 --- /dev/null +++ b/app/library/App/Mvc/ExtendedApiCollection.php @@ -0,0 +1,47 @@ +endpointsByName[$endpoint->getName()] = $endpoint; + + switch ($endpoint->getHttpMethod()) { + + case HttpMethods::GET: + + $this->get($endpoint->getPath(), $endpoint->getHandlerMethod(), $this->createRouteName($endpoint)); + break; + + case HttpMethods::POST: + + $this->post($endpoint->getPath(), $endpoint->getHandlerMethod(), $this->createRouteName($endpoint)); + break; + + case HttpMethods::PUT: + + $this->put($endpoint->getPath(), $endpoint->getHandlerMethod(), $this->createRouteName($endpoint)); + break; + + case HttpMethods::DELETE: + + $this->delete($endpoint->getPath(), $endpoint->getHandlerMethod(), $this->createRouteName($endpoint)); + break; + } + + return $this; + } + +} \ No newline at end of file diff --git a/app/library/App/Mvc/ExtendedApiEndpoint.php b/app/library/App/Mvc/ExtendedApiEndpoint.php new file mode 100644 index 0000000..295808e --- /dev/null +++ b/app/library/App/Mvc/ExtendedApiEndpoint.php @@ -0,0 +1,37 @@ +exampleRequest = $exampleRequest; + return $this; + } + + /** + * @return string Example of the request of the endpoint + */ + public function getExampleRequest() + { + return $this->exampleRequest; + } + +} \ No newline at end of file diff --git a/app/library/App/Mvc/ExtendedApiResource.php b/app/library/App/Mvc/ExtendedApiResource.php new file mode 100644 index 0000000..aeeac0c --- /dev/null +++ b/app/library/App/Mvc/ExtendedApiResource.php @@ -0,0 +1,225 @@ +endpoint(ExtendedApiEndpoint::all()) + ->endpoint(ExtendedApiEndpoint::find()) + ->endpoint(ExtendedApiEndpoint::create()) + ->endpoint(ExtendedApiEndpoint::update()) + ->endpoint(ExtendedApiEndpoint::remove()); + } + + /** + * Returns resource with default values + * + * @param string $prefix Prefix for the resource (e.g. /user) + * @param string $name Name for the resource (e.g. users) (optional) + * + * @return static + */ + public static function factory($prefix, $name = null) + { + $calledClass = get_called_class(); + + /** @var ExtendedApiResource $resource */ + $resource = new $calledClass($prefix); + + if (!$resource->getItemKey()) { + $resource->itemKey('items'); + } + + if (!$resource->getCollectionKey()) { + $resource->collectionKey('items'); + } + + if (!$resource->getTransformer()) { + $resource->transformer(ModelTransformer::class); + } + + if (!$resource->getHandler()) { + $resource->setHandler(CrudResourceController::class); + } + + if (!$resource->getName() && $name) { + $resource->name($name); + } + + if ($name) { + $resource->name($name); + } + + return $resource; + } + + /** + * @param string $model Classname of the model + * + * @return static + */ + public function model($model) + { + $this->model = $model; + return $this; + } + + /** + * @return string|null Classname of the model + */ + public function getModel() + { + return $this->model; + } + + /** + * @return string|null Primary key of the model + */ + public function getModelPrimaryKey() + { + if (!$this->model) { + return null; + } + + if (!$this->_modelPrimaryKey) { + + /** @var \Phalcon\Mvc\Model\MetaData $modelsMetaData */ + $modelsMetaData = Di::getDefault()->get(Services::MODELS_METADATA); + + $modelClass = $this->model; + + $this->_modelPrimaryKey = $modelsMetaData->getIdentityField(new $modelClass); + } + + return $this->_modelPrimaryKey; + } + + /** + * @param string $transformer Classname of the transformer + * + * @return static + */ + public function transformer($transformer) + { + $this->transformer = $transformer; + return $this; + } + + /** + * @return string|null Classname of the transformer + */ + public function getTransformer() + { + return $this->transformer; + } + + /** + * @param string $singleKey Response key for single item + * + * @return static + * + * @deprecated Use itemKey() instead + */ + public function singleKey($singleKey) + { + return $this->itemKey($singleKey); + } + + /** + * @param string $itemKey Response key for single item + * + * @return static + */ + public function itemKey($itemKey) + { + $this->itemKey = $itemKey; + return $this; + } + + /** + * @return string Response key for single item + * + * @deprecated Use getItemKey() instead + */ + public function getSingleKey() + { + return $this->getItemKey(); + } + + /** + * @return string Response key for single item + */ + public function getItemKey() + { + return ($this->itemKey ?: $this->name) ?: 'item'; + } + + /** + * @param string $multipleKey Response key for multiple items + * + * @return static + * + * @deprecated Use collectionKey() instead + */ + public function multipleKey($multipleKey) + { + return $this->collectionKey($multipleKey); + } + + /** + * @param string $collectionKey Response key for multiple items + * + * @return static + */ + public function collectionKey($collectionKey) + { + $this->collectionKey = $collectionKey; + return $this; + } + + /** + * @return string Response key for multiple items + * + * @deprecated Use getCollectionKey() instead + */ + public function getMultipleKey() + { + return $this->getCollectionKey(); + } + + /** + * @return string Response key for multiple items + */ + public function getCollectionKey() + { + return ($this->collectionKey ?: $this->name) ?: 'items'; + } +} diff --git a/app/library/App/Resources/GaResource.php b/app/library/App/Resources/GaResource.php index d87e4a5..838f59d 100644 --- a/app/library/App/Resources/GaResource.php +++ b/app/library/App/Resources/GaResource.php @@ -11,10 +11,12 @@ namespace App\Resources; use App\Constants\AclRoles; use App\Controllers\GaController; +use App\Mvc\ExtendedApiResource; use PhalconRest\Api\ApiEndpoint; +use App\Mvc\ExtendedApiEndpoint; use PhalconRest\Api\ApiResource; -class GaResource extends ApiResource { +class GaResource extends ExtendedApiResource { public function initialize() { @@ -27,7 +29,7 @@ class GaResource extends ApiResource { ->deny(AclRoles::UNAUTHORIZED) ->handler(GaController::class) - ->endpoint(ApiEndpoint::get('', 'getAction') + ->endpoint(ExtendedApiEndpoint::get('', 'getAction') ->allow(AclRoles::USER) ->description('Returns data from Google Analytics Api') ->exampleResponse([ @@ -41,6 +43,7 @@ class GaResource extends ApiResource { ], 'full_docs' => 'https://developers.google.com/analytics/devguides/reporting/core/dimsmets', ]) + ->exampleRequest(['123' => '321']) ) ; -- libgit2 0.21.4