PHP code example of cura / api-platform-extras

1. Go to this page and download the library: Download cura/api-platform-extras 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/ */

    

cura / api-platform-extras example snippets




declare(strict_types=1);

namespace App\Processor;

use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;
use DominikPeters\ApiPlatformExtras\AbstractProcessor;

final class CustomProcessor extends AbstractProcessor
{
    protected function supportsResource(mixed $resource, array $uriVariables, array $context): bool
    {
        // In the read world this would check whether $resource is an instance
        // of your API platform resource
        return $resource !== null;
    }
    
    protected function handlePostOperation($resource, Post $operation, array $uriVariables, array $context): object
    {
        // Custom persistence logic goes here
        return $resouce;
    }
    
    protected function handlePutOperation($resource, Put $operation, array $uriVariables, array $context): object
    {
        // Custom replacement logic goes here
        return $resouce;
    }
    
    protected function handlePatchOperation($resource, Patch $operation, array $uriVariables, array $context): object
    {
        // Custom update logic goes here
        return $resouce;
    }
    
    protected function handleDeleteOperation($resource, Delete $operation, array $uriVariables, array $context): object
    {
        // Custom deletion logic goes here
    }
}



declare(strict_types=1);

namespace App\Provider;

use ApiPlatform\Metadata\GetCollection;use ApiPlatform\Metadata\Operation;use DominikPeters\ApiPlatformExtras\AbstractProvider;

final class CustomProvider extends AbstractProvider
{
    protected function canProvideCollection(Operation $operation, array $uriVariables, array $context) : bool
    {
        if ($operation instanceof GetCollection) {
            return true;
        }
        
        // We can/should provide a collection if no uuid is given
        return !array_key_exists('uuid', $uriVariables);
    }
    
    protected function provideCollection(array $uriVariables,array $context) : array{
        // Custom collection fetch logic here
        return [];
    }
    
    protected function provideItem(array $uriVariables,array $context) : ?object{
        // Custom fetch logic here, return null if not found
        return null;
    }
}