PHP code example of develme / restful-lists

1. Go to this page and download the library: Download develme/restful-lists 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/ */

    

develme / restful-lists example snippets


use DevelMe\RestfulList\Model\Service;
use Symfony\Component\HttpFoundation\Response;
use Tests\Models\Example;

public function index(Example $example, Service $service): Response
{
    return $service->model($example)->json();
}


$myData = []; //

$data = new \Illuminate\Support\Collection($myData);

$orchestrator = new \DevelMe\RestfulList\Collection\Orchestration\Orchestrator();
$orchestrator->register();

$engine = new \DevelMe\RestfulList\Engines\Collection($data, $orchestrator);

// Filters
$engine->filters([
    'status' => 'Open', // status equals 'Open' - Simple filters default to equals
    'name' => ['field' => 'name', 'type' => 'contains', 'value' => 'John'], // name contains 'John' | Results in '%John%' for SQL
]);

// Orders
$engine->orders([
    'state', // order by state ascending - Simple orders default to ascending
    'created_at' => 'desc', // order by created_at descending - key => value equates to field => direction
    'title' => ['field' => 'title', 'direction' => 'desc'], // order by title descending - Explicitly defining field and direction
]);

// Pagination - simple
$engine->pagination([100, 200]); // start at 100 end at 200 - Index 0 is start, index 1 is end | offset 100 limit 100 for SQL

// Pagination - explicit
$engine->pagination(['start' => 100, 'end' => 200]);

// Results
$results = $engine->go();

$parameters = [
    'filters' => [
        'status' => 'Open', // status equals 'Open' - Simple filters default to equals
        'name' => ['field' => 'name', 'type' => 'contains', 'value' => 'John'], // name contains 'John' | Results in '%John%' for SQL
    ],
    'orders' => [
        'state', // order by state ascending - Simple orders default to ascending
        'created_at' => 'desc', // order by created_at descending - key => value equates to field => direction
        'title' => ['field' => 'title', 'direction' => 'desc'], // order by title descending - Explicitly defining field and direction
    ],
    'pagination' => [
        'page' => 2,
        'size' => 30,
    ]
]

/**
 * Engine that supports the custom data
 */
class CustomDataEngine extends \DevelMe\RestfulList\Engines\Base 
{
    protected $data;

    public function __construct($customData, \DevelMe\RestfulList\Contracts\Orchestration $orchestrator) {
        parent::__construct($orchestrator);
        $this->data = $customData;
    }
}


/**
 * Handles calls for filtering, sorting, paginating and fetching results. Naturally the Orchestrator wants to use the 
 * Strategy behavioral pattern to break out the necessary functionality into smaller class implementations.
 * 
 * Below, we are returning CustomDataHandler which can implement the interfaces specified in the method tags, keeping
 * all the code together, if desired
 * 
 * @method \DevelMe\RestfulList\Contracts\Counter counter()
 * @method \DevelMe\RestfulList\Contracts\Engine\Filtration filter()
 * @method \DevelMe\RestfulList\Contracts\Engine\Arrangement order()
 * @method \DevelMe\RestfulList\Contracts\Engine\Paginator pagination()
 * @method \DevelMe\RestfulList\Contracts\Engine\Result result()
 */
class CustomOrchestrator implements \DevelMe\RestfulList\Contracts\Orchestration
{
    /**
     * We're letting CustomDataHandler handle everything, but one should leverage the dynamic method call implemented
     * here to return single responsibility implementations of what is being requested in the $ask variable.
     */
    public function orchestrate(string $ask):  mixed
    {
        return new CustomDataHandler;
    }
    
    public function __call(string $name,array $arguments)
    {
        $this->orchestrate($name);
    }
}


/** 
 * This handler can handle everything. It has way too many responsibilities, and breaking the behaviors into different
 * class implementations would be the desired result.
 */
class CustomDataHandler implements 
    \DevelMe\RestfulList\Contracts\Counter,
    \DevelMe\RestfulList\Contracts\Engine\Filtration,
    \DevelMe\RestfulList\Contracts\Engine\Arrangement,
    \DevelMe\RestfulList\Contracts\Engine\Paginator, 
    \DevelMe\RestfulList\Contracts\Engine\Result
{
    public function arrange(\DevelMe\RestfulList\Contracts\Engine\Data $data,array $orders)
    {
        // TODO: Implement arrange() method.
    }
    
    public function count(\DevelMe\RestfulList\Contracts\Engine\Data $data) : int
    {
        // TODO: Implement count() method.
    }
    
    public function filter(\DevelMe\RestfulList\Contracts\Engine\Data $data,array $filters)
    {
        // TODO: Implement filter() method.
    }
    
    public function paginate(\DevelMe\RestfulList\Contracts\Engine\Data $data,array $pagination)
    {
        // TODO: Implement paginate() method.
    }
    
    public function get(\DevelMe\RestfulList\Contracts\Engine\Data $data)
    {
     // TODO: Implement get() method.
    }
}

$data = new My\Custom\XML\Loader('path-to-xml-file.xml');

$orchestrator = new CustomOrchestrator();

$engine = new CustomDataEngine($data, $orchestrator);

$results = $engine->filters([])->orders([])->pagination([])->go();