ProjectResource.php 3.24 KB
<?php
/**
 * Created by PhpStorm.
 * User: Alex Savenko
 * Date: 09.02.2017
 * Time: 14:38
 */

namespace App\Resources;

use App\Controllers\ProjectController;
use PhalconApi\Constants\HttpMethods;
use PhalconRest\Api\ApiEndpoint;
use PhalconRest\Api\ApiResource;
use App\Model\Project;
use PhalconRest\Transformers\ModelTransformer;
use App\Constants\AclRoles;

class ProjectResource extends ApiResource {

    public function initialize()
    {
        $this
            ->name('Project')
            ->model(Project::class)
            ->expectsJsonData()
            ->transformer(ModelTransformer::class)
            ->itemKey('project')
            ->collectionKey('projects')
            ->deny(AclRoles::ALL_ROLES)
            ->handler(ProjectController::class)

            /** -------------------- [GET] projects ----------------------------- **/
                ->endpoint(ApiEndpoint::factory('/', HttpMethods::GET, 'allAction')
                    ->name(ApiEndpoint::ALL)
                    ->description('Returns all items')
                    ->allow(
                        AclRoles::ADMINISTRATOR,
                        AclRoles::USER,
                        AclRoles::EDITOR,
                        AclRoles::AUTHOR
                    )
                )
            /** ----------------------------------------------------------------- **/

            /** -------------------- [POST] projects ---------------------------- **/
                ->endpoint(ApiEndpoint::factory('/', HttpMethods::POST, 'create')
                    ->name(ApiEndpoint::CREATE)
                    ->description('Creates a new item using the posted data')
                    ->allow(
                        AclRoles::ADMINISTRATOR,
                        AclRoles::USER
                    )
                )
            /** ----------------------------------------------------------------- **/

            /** -------------------- [GET] projects/{id} ------------------------ **/
                ->endpoint(ApiEndpoint::find()
                    ->allow(AclRoles::ADMINISTRATOR)
                )
            /** ----------------------------------------------------------------- **/

            /** -------------------- [PUT] projects/{id} ------------------------ **/
                ->endpoint(ApiEndpoint::factory('/{id}', HttpMethods::PUT, 'update')
                    ->name(ApiEndpoint::UPDATE)
                    ->description('Updates an existing item identified by {id}, using the posted data')
                    ->allow(
                        AclRoles::ADMINISTRATOR,
                        AclRoles::USER
                    )
                )
            /** ----------------------------------------------------------------- **/

            /** -------------------- [DELETE] projects/{id} --------------------- **/
                ->endpoint(ApiEndpoint::factory('/{id}', HttpMethods::DELETE, 'remove')
                    ->name(ApiEndpoint::REMOVE)
                    ->description('Removes the item identified by {id}')
                    ->allow(
                        AclRoles::ADMINISTRATOR,
                        AclRoles::USER
                    )
                );
            /** ----------------------------------------------------------------- **/
    }

}