PHP code example of paysera / util-raml-code-generator

1. Go to this page and download the library: Download paysera/util-raml-code-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

paysera / util-raml-code-generator example snippets




namespace Paysera\Test\CategoryClient;

use Paysera\Test\CategoryClient\Entity as Entities;
use Fig\Http\Message\RequestMethodInterface;
use Paysera\Component\RestClientCommon\Client\ApiClient;

class CategoryClient
{
    private $apiClient;

    public function __construct(ApiClient $apiClient)
    {
        $this->apiClient = $apiClient;
    }

    public function withOptions(array $options)
    {
        return new CategoryClient($this->apiClient->withOptions($options));
    }

    /**
     * Enable category
     * PUT /categories/{id}/enable
     *
     * @param string $id
     * @return Entities\Category
     */
    public function enableCategory($id)
    {
        $request = $this->apiClient->createRequest(
            RequestMethodInterface::METHOD_PUT,
            sprintf('categories/%s/enable', urlencode($id)),
            null
        );
        $data = $this->apiClient->makeRequest($request);

        return new Entities\Category($data);
    }

    /**
     * Disable category
     * PUT /categories/{id}/disable
     *
     * @param string $id
     * @return Entities\Category
     */
    public function disableCategory($id)
    {
        $request = $this->apiClient->createRequest(
            RequestMethodInterface::METHOD_PUT,
            sprintf('categories/%s/disable', urlencode($id)),
            null
        );
        $data = $this->apiClient->makeRequest($request);

        return new Entities\Category($data);
    }

    /**
     * Update category
     * PUT /categories/{id}
     *
     * @param string $id
     * @param Entities\Category $category
     * @return Entities\Category
     */
    public function updateCategory($id, Entities\Category $category)
    {
        $request = $this->apiClient->createRequest(
            RequestMethodInterface::METHOD_PUT,
            sprintf('categories/%s', urlencode($id)),
            $category
        );
        $data = $this->apiClient->makeRequest($request);

        return new Entities\Category($data);
    }

    /**
     * Delete category
     * DELETE /categories/{id}
     *
     * @param string $id
     * @return null
     */
    public function deleteCategory($id)
    {
        $request = $this->apiClient->createRequest(
            RequestMethodInterface::METHOD_DELETE,
            sprintf('categories/%s', urlencode($id)),
            null
        );
        $data = $this->apiClient->makeRequest($request);

        return null;
    }

    /**
     * Standard SQL-style Result filtering
     * GET /categories
     *
     * @param Entities\CategoryFilter $categoryFilter
     * @return null
     */
    public function getCategories(Entities\CategoryFilter $categoryFilter)
    {
        $request = $this->apiClient->createRequest(
            RequestMethodInterface::METHOD_GET,
            'categories',
            $categoryFilter
        );
        $data = $this->apiClient->makeRequest($request);

        return null;
    }

    /**
     * Create category
     * POST /categories
     *
     * @param Entities\Category $category
     * @return Entities\CategoryResult
     */
    public function createCategory(Entities\Category $category)
    {
        $request = $this->apiClient->createRequest(
            RequestMethodInterface::METHOD_POST,
            'categories',
            $category
        );
        $data = $this->apiClient->makeRequest($request);

        return new Entities\CategoryResult($data, 'items');
    }
}




namespace Paysera\Test\CategoryClient;

use Paysera\Component\RestClientCommon\Util\ClientFactoryAbstract;
use Paysera\Component\RestClientCommon\Client\ApiClient;

class ClientFactory extends ClientFactoryAbstract
{
    const DEFAULT_BASE_URL = 'https://example.com/category/rest/v1/';

    private $apiClient;

    public function __construct($options)
    {
        if ($options instanceof ApiClient) {
            $this->apiClient = $options;
            return;
        }

        $defaultUrlParameters = [];
        
        $options['url_parameters'] = $this->resolveDefaultUrlParameters($defaultUrlParameters, $options);
        $this->apiClient = $this->createApiClient($options);
    }

    public function getAClient()
    {
        return new CategoryClient($this->apiClient);
    }

    private function resolveDefaultUrlParameters(array $defaults, array $options)
    {
        $params = [];
        if (isset($options['url_parameters'])) {
            $params = $options['url_parameters'];
        }

        return $params + $defaults;
    }
}




namespace Paysera\Test\CategoryClient\Entity;

use Paysera\Component\RestClientCommon\Entity\Entity;

class Category extends Entity
{
    const STATUS_ACTIVE = 'active';
    const STATUS_INACTIVE = 'inactive';

    public function __construct(array $data = [])
    {
        parent::__construct($data);
    }

    /**
     * @return string|null
     */
    public function getId()
    {
        return $this->get('id');
    }
    /**
     * @param string $id
     * @return $this
     */
    public function setId($id)
    {
        $this->set('id', $id);
        return $this;
    }
    /**
     * @return string|null
     */
    public function getParentId()
    {
        return $this->get('parent_id');
    }
    /**
     * @param string $parentId
     * @return $this
     */
    public function setParentId($parentId)
    {
        $this->set('parent_id', $parentId);
        return $this;
    }
    /**
     * @return string
     */
    public function getName()
    {
        return $this->get('name');
    }
    /**
     * @param string $name
     * @return $this
     */
    public function setName($name)
    {
        $this->set('name', $name);
        return $this;
    }
    /**
     * @return string|null
     */
    public function getStatus()
    {
        return $this->get('status');
    }
    /**
     * @param string $status
     * @return $this
     */
    public function setStatus($status)
    {
        $this->set('status', $status);
        return $this;
    }
}




namespace Paysera\Test\CategoryClient\Entity;

use Paysera\Component\RestClientCommon\Entity\Filter;

class CategoryFilter extends Filter
{
    /**
     * @return string|null
     */
    public function getParentId()
    {
        return $this->get('parent_id');
    }
    /**
     * @param string $parentId
     * @return $this
     */
    public function setParentId($parentId)
    {
        $this->set('parent_id', $parentId);
        return $this;
    }
}




namespace Paysera\Test\CategoryClient\Entity;

use Paysera\Component\RestClientCommon\Entity\Result;

class CategoryResult extends Result
{
    protected function createItem(array $data)
    {
        return new Category($data);
    }
}




namespace Vendor\Test\CategoryApiBundle;

final class CategoryPermissions
{
    const GET_CATEGORIES = 'get_categories';
    const CREATE_CATEGORY = 'create_category';
    const UPDATE_CATEGORY = 'update_category';
    const DELETE_CATEGORY = 'delete_category';
    const ENABLE_CATEGORY = 'enable_category';
    const DISABLE_CATEGORY = 'disable_category';
    
}




namespace Vendor\Test\CategoryApiBundle;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class VendorCategoryApiBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
    }
}




namespace Vendor\Test\CategoryApiBundle\Controller;

use Vendor\Test\CategoryApiBundle\Entity as Entities;
use Paysera\Component\Serializer\Entity\Result;
use Vendor\Test\CategoryApiBundle\Service\CategoryManager;
use Vendor\Test\CategoryApiBundle\CategoryPermissions;
use Paysera\Bundle\SecurityBundle\Service\AuthorizationChecker;
use Doctrine\ORM\EntityManager;

class CategoryApiController
{
    private $authorizationChecker;
    private $entityManager;
    private $categoryManager;
    
    public function __construct(
        CategoryManager $categoryManager,
        AuthorizationChecker $authorizationChecker,
        EntityManager $entityManager
    ) {
        $this->categoryManager = $categoryManager;
        $this->authorizationChecker = $authorizationChecker;
        $this->entityManager = $entityManager;
    }

    /**
     * Enable category
     * PUT /categories/{id}/enable
     *
     * @param Entities\Category $category
     * @return Entities\Category
     */
    public function enableCategory(Entities\Category $category)
    {
        $this->authorizationChecker->check(CategoryPermissions::ENABLE_CATEGORY);
        $result = $this->categoryManager->enableCategory($category);
        $this->entityManager->flush();
        return $result;
    }
    /**
     * Disable category
     * PUT /categories/{id}/disable
     *
     * @param Entities\Category $category
     * @return Entities\Category
     */
    public function disableCategory(Entities\Category $category)
    {
        $this->authorizationChecker->check(CategoryPermissions::DISABLE_CATEGORY);
        $result = $this->categoryManager->disableCategory($category);
        $this->entityManager->flush();
        return $result;
    }
    /**
     * Update category
     * PUT /categories/{id}
     *
     * @param Entities\Category $originalCategory
     * @param Entities\Category $updatedCategory
     * @return Entities\Category
     */
    public function updateCategory(Entities\Category $originalCategory, Entities\Category $updatedCategory)
    {
        $this->authorizationChecker->check(CategoryPermissions::UPDATE_CATEGORY);
        $result = $this->categoryManager->updateCategory($originalCategory, $updatedCategory);
        $this->entityManager->flush();
        return $result;
    }
    /**
     * Delete category
     * DELETE /categories/{id}
     *
     * @param Entities\Category $category
     * @return null
     */
    public function deleteCategory(Entities\Category $category)
    {
        $this->authorizationChecker->check(CategoryPermissions::DELETE_CATEGORY);
        $this->categoryManager->deleteCategory($category);
        $this->entityManager->flush();
        return null;
    }
    /**
     * Standard SQL-style Result filtering
     * GET /categories
     *
     * @param Entities\CategoryFilter $categoryFilter
     * @return null
     */
    public function getCategories(Entities\CategoryFilter $categoryFilter)
    {
        $this->authorizationChecker->check(CategoryPermissions::GET_CATEGORIES);
        return $this->categoryManager->getCategories($categoryFilter);
    }
    /**
     * Create category
     * POST /categories
     *
     * @param Entities\Category $category
     * @return Result|Entities\Category[]
     */
    public function createCategory(Entities\Category $category)
    {
        $this->authorizationChecker->check(CategoryPermissions::CREATE_CATEGORY);
        $result = $this->categoryManager->createCategory($category);
        $this->entityManager->flush();
        return $result;
    }
}




namespace Vendor\Test\CategoryApiBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('vendor_category_api');

        return $treeBuilder;
    }
}




namespace Vendor\Test\CategoryApiBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

class VendorCategoryApiExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.xml');
    }
}




namespace Vendor\Test\CategoryApiBundle\Entity;

class Category
{
    const STATUS_ACTIVE = 'active';
    const STATUS_INACTIVE = 'inactive';

    private $id;
    private $parentId;
    private $name;
    private $status;

    public function __construct()
    {
                    
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * @return string|null
     */
    public function getParentId()
    {
        return $this->parentId;
    }
    /**
     * @param string $parentId
     * @return $this
     */
    public function setParentId($parentId)
    {
        $this->parentId = $parentId;
        return $this;
    }
    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
    /**
     * @param string $name
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }
    /**
     * @return string|null
     */
    public function getStatus()
    {
        return $this->status;
    }
    /**
     * @param string $status
     * @return $this
     */
    public function setStatus($status)
    {
        $this->status = $status;
        return $this;
    }

}




namespace Vendor\Test\CategoryApiBundle\Entity;

use Paysera\Component\Serializer\Entity\Filter;

class CategoryFilter extends Filter
{
    private $parentId;

    /**
     * @return string|null
     */
    public function getParentId()
    {
        return $this->parentId;
    }
    /**
     * @param string $parentId
     * @return $this
     */
    public function setParentId($parentId)
    {
        $this->parentId = $parentId;
        return $this;
    }
}





namespace Vendor\Test\CategoryApiBundle\Normalizer;

use Paysera\Component\Serializer\Normalizer\DenormalizerInterface;
use Paysera\Component\Serializer\Normalizer\NormalizerInterface;
use Vendor\Test\CategoryApiBundle\Entity\Category;

class CategoryNormalizer implements NormalizerInterface, DenormalizerInterface
{
    
    /**
     * @param array $data
     *
     * @return Category
     */
    public function mapToEntity($data)
    {
        $entity = new Category();

        if (isset($data['parent_id'])) {
            $entity->setParentId($data['parent_id']);
        }
        if (isset($data['name'])) {
            $entity->setName($data['name']);
        }
        if (isset($data['status'])) {
            $entity->setStatus($data['status']);
        }
        
        return $entity;
    }

    /**
     * @param Category $entity
     *
     * @return array
     */
    public function mapFromEntity($entity)
    {
        return [
            'id' => $entity->getId(),
            'parent_id' => $entity->getParentId(),
            'name' => $entity->getName(),
            'status' => $entity->getStatus(),
            
        ];
    }
}




namespace Vendor\Test\CategoryApiBundle\Normalizer;

use Paysera\Component\Serializer\Normalizer\FilterNormalizer;
use Vendor\Test\CategoryApiBundle\Entity\CategoryFilter;

class CategoryFilterNormalizer extends FilterNormalizer
{
    /**
     * @param array $data
     *
     * @return CategoryFilter
     */
    public function mapToEntity($data)
    {
        $entity = new CategoryFilter();
        $this->mapBaseKeys($data, $entity);

        if (isset($data['parent_id'])) {
            $entity->setParentId($data['parent_id']);
        }
        
        return $entity;
    }

    /**
     * @param CategoryFilter $entity
     *
     * @return array
     */
    public function mapFromEntity($entity)
    {
        $data = parent::mapFromEntity($entity);
        return array_merge(
            $data,
            [
                'parent_id' => $entity->getParentId(),
                
            ]
        );
        
    }
}




namespace Vendor\Test\CategoryApiBundle\Repository;

use Doctrine\ORM\EntityRepository;

class CategoryRepository extends EntityRepository
{
}




namespace Vendor\Test\CategoryApiBundle\Service;

use Paysera\Component\Serializer\Entity\Result;
use Vendor\Test\CategoryApiBundle\Entity as Entities;
use Vendor\Test\CategoryApiBundle\Repository\CategoryRepository;
use Doctrine\ORM\EntityManager;

class CategoryManager
{
    private $categoryRepository;
    private $entityManager;

    public function __construct(
        CategoryRepository $categoryRepository,
        EntityManager $entityManager
    ) {
        $this->categoryRepository = $categoryRepository;
        $this->entityManager = $entityManager;
    }

    /**
     * @param Entities\Category $category
     * @return Entities\Category
     */
    public function enableCategory(Entities\Category $category)
    {
        //TODO: generated_code
    }
    /**
     * @param Entities\Category $category
     * @return Entities\Category
     */
    public function disableCategory(Entities\Category $category)
    {
        //TODO: generated_code
    }
    /**
     * @param Entities\Category $category
     * @param Entities\Category $category
     * @return Entities\Category
     */
    public function updateCategory(Entities\Category $originalCategory, Entities\Category $updatedCategory)
    {
        //TODO: generated_code
    }
    /**
     * @param Entities\Category $category
     * @return null
     */
    public function deleteCategory(Entities\Category $category)
    {
        //TODO: generated_code
    }
    /**
     * @param Entities\CategoryFilter $categoryFilter
     * @return null
     */
    public function getCategories(Entities\CategoryFilter $categoryFilter)
    {
        //TODO: generated_code
    }
    /**
     * @param Entities\Category $category
     * @return Result|Entities\Category[]
     */
    public function createCategory(Entities\Category $category)
    {
        //TODO: generated_code
    }
}




namespace Vendor\Test\CategoryApiBundle\Voter;

use Vendor\Test\CategoryApiBundle\CategoryPermissions;
use Paysera\Bundle\SecurityBundle\Security\ContextAwareScopeVoter;
use Paysera\Bundle\SecurityBundle\Entity\AccessedBy;

class CategoryScopeVoter extends ContextAwareScopeVoter
{
    public function getPermissionScopeMap()
    {
        return [
            CategoryPermissions::GET_CATEGORIES => [
                // TODO: generated_code
            ],
            CategoryPermissions::CREATE_CATEGORY => [
                // TODO: generated_code
            ],
            CategoryPermissions::UPDATE_CATEGORY => [
                // TODO: generated_code
            ],
            CategoryPermissions::DELETE_CATEGORY => [
                // TODO: generated_code
            ],
            CategoryPermissions::ENABLE_CATEGORY => [
                // TODO: generated_code
            ],
            CategoryPermissions::DISABLE_CATEGORY => [
                // TODO: generated_code
            ],
        ];
    }

    public function checkAccessRights(AccessedBy $accessedBy, $permission, $subject)
    {
        // TODO: generated_code
    }
}


|   composer.json
|   README.md
|
└───src
    |   CategoryClient.php
    |   ClientFactory.php
    |
    ├───Entity
    |       Category.php
    |       CategoryFilter.php
    |       CategoryResult.php


|   CategoryPermissions.php
|   VendorCategoryApiBundle.php
|
└───Controller
    |   CategoryApiController.php
    |
    DependencyInjection
    |   Configuration.php
    |   VendorCategoryApiExtension.php
    |
    Entity
    |   Category.php
    |   CategoryFilter.php
    |
    Normalizer
    |   CategoryNormalizer.php
    |   CategoryFilterNormalizer.php
    |
    Repository
    |   CategoryRepository.php
    |
    Service
    |   CategoryManager.php
    |
    Voter
    |   CategoryScopeVoter.php
    |
    Resources
    |
    ├───config
    |       routing.xml
    |       services.xml
    |       
    ├───────services
    |           api.xml
    |           controllers.xml
    |           normalizers.xml
    |           repositories.xml
    |           services.xml
    |
    ├───────routing
    |           rest_v1.xml
    |
    ├───────doctrine
    |           Category.orm.xml