PHP code example of corey-mac / api-extension

1. Go to this page and download the library: Download corey-mac/api-extension 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/ */

    

corey-mac / api-extension example snippets




namespace App\Api\Task;

use Cs\ApiExtensionBundle\Api\Entity\ApiEntity;
use Cs\ApiExtensionBundle\Api\Entity\Traits\IdAwareApiEntity;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Class TaskEntity
 * @package App\Api\Entity
 */
class TaskEntity extends ApiEntity
{
    use IdAwareApiEntity;

    /**
     * @Assert\NotBlank()
     * @var string
     */
    protected string $name;

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName(string $name): void
    {
        $this->name = $name;
    }
}

use Cs\ApiExtensionBundle\Api\Entity\Annotations\ReferenceMany;
use Cs\ApiExtensionBundle\Api\Entity\Annotations\ReferenceOne;

class ... extends ApiEntity
{
    /**
     * @ReferenceOne(type=SomeEntity::class)
     * @var SomeEntity
     */
    protected SomeEntity $someEntity;

use Cs\ApiExtensionBundle\Api\Entity\Traits\IdAwareApiEntity;

class ... extends ApiEntity
{
    use IdAwareApiEntity;

namespace App\Api\Connection;

use Cs\ApiExtensionBundle\Api\Entity\ApiEntityCollection;

/**
 * Class ConnectionEntityCollection
 * @package App\Api\Entity
 */
class ConnectionEntityCollection extends ApiEntityCollection
{
    /**
     * @inheritDoc
     */
    public function getEntityClassName(): string
    {
        return ConnectionEntity::class;
    }
}




namespace App\Api\Task;

use App\Api\Connection\ConnectionConverter;
use Cs\ApiExtensionBundle\Api\Controller\ApiControllerInterface;
use Cs\ApiExtensionBundle\Api\Controller\Annotations;
use Cs\ApiExtensionBundle\Api\Request\ApiEntityDeleteRequest;
use Cs\ApiExtensionBundle\Api\Request\ApiEntityGetRequest;
use Cs\ApiExtensionBundle\Api\Response\ApiCollectionPostResponse;
use Cs\ApiExtensionBundle\Api\Response\ApiCollectionResponse;
use Cs\ApiExtensionBundle\Api\Response\ApiEntityDeleteResponse;
use Cs\ApiExtensionBundle\Api\Response\ApiEntityGetResponse;
use Cs\ApiExtensionBundle\Api\Response\ApiEntityUpdateResponse;
use Cs\ApiExtensionBundle\Exception\ApiEntityNotFoundException;
use Cs\ApiExtensionBundle\Traits\ExceptionAwareApiController;

/**
 * Class TaskController
 * @package App\Api\Task
 * @Annotations\ApiController(entity=TaskEntity::class)
 */
class TaskController implements ApiControllerInterface
{
    use ExceptionAwareApiController;

    /**
     * @Annotations\Operation(type="collection-create")
     */
    public function create(TaskEntity $taskEntity): ApiCollectionPostResponse
    {
        return new ApiCollectionPostResponse($taskEntity);
    }

    /**
     * @Annotations\Operation(type="collection-get")
     * @return ApiCollectionResponse
     */
    public function getAll(): ApiCollectionResponse
    {
        $taskEntities = []; // some service that loads all tasks
        $totalEntityCount = 0; // total count of all entities
        return new ApiCollectionResponse(new TaskEntityCollection($taskEntities, $totalEntityCount));
    }

    /**
     * @Annotations\Operation(type="item-get")
     * @param ApiEntityGetRequest $request
     * @return ApiEntityGetResponse
     */
    public function get(ApiEntityGetRequest $request): ApiEntityGetResponse
    {
        $taskEntity = $this->someService->get($request->getId());
        $this->throwApiResponseEntityNotFoundExceptionIfNull($request->getId(), $task);
        return new ApiEntityGetResponse($this->convertTaskToApiEntity($task));
    }

    /**
     * @Annotations\Operation(type="item-delete")
     * @param ApiEntityDeleteRequest $request
     * @return ApiEntityDeleteResponse
     */
    public function delete(ApiEntityDeleteRequest $request): ApiEntityDeleteResponse
    {
        $success = $this->someService->delete($request->getId());
        
        if(!$success)
        {
            $this->throwApiResponseEntityNotFoundException($request->getId());
        }

        return new ApiEntityDeleteResponse();
    }

    /**
     * @Annotations\Operation(type="item-patch")
     * @param TaskEntity $taskEntity
     * @return ApiEntityUpdateResponse
     */
    public function update(TaskEntity $taskEntity): ApiEntityUpdateResponse
    {
        ...
        return new ApiEntityUpdateResponse();
    }
}