PHP code example of wizards / rest-api

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

    

wizards / rest-api example snippets




namespace App\Controller;

use Psr\Http\Message\ServerRequestInterface;
use WizardsRest\ObjectManager\ArrayObjectManager;
use WizardsRest\CollectionManager;
use WizardsRest\Provider;
use WizardsRest\Serializer;
use WizardsRest\ObjectReader\ArrayReader;
use WizardsRest\Paginator\ArrayPagerfantaPaginator;
use Symfony\Component\Routing\RouterInterface;
use WizardsRest\Transformer\ArrayTransformer;

Class BookController {
    private $source = [
        ['name' => 'Book 1', 'author' => 'Author 1', 'editor' => 'Editor 1'],
        ['name' => 'Book 2', 'author' => 'Author 2', 'editor' => 'Editor 2'],
	];
    
	// Books controller. Somehow, this is called
    public function getBooks(RouterInterface $router, ServerRequestInterface $request) {
        // Fetch
        $objectManager = new ArrayObjectManager();
        $paginator = new ArrayPagerfantaPaginator($router);
        $collectionManager = new CollectionManager($paginator, $objectManager);
        $collection = $collectionManager->getPaginatedCollection($this->source, $request);
        
        // Transform
        $fractalManager = new \League\Fractal\Manager();
        $reader = new ArrayReader();
        $provider = new Provider(new ArrayTransformer(), $fractalManager, $reader);
        $resource = $provider->transform($collection, $request, null, 'books');
        
        // Serialize
        $serializer = new Serializer($fractalManager, 'https://mysite.com');
        return $serializer->serialize($resource, Serializer::SPEC_DATA_ARRAY, Serializer::FORMAT_ARRAY);
    }
}