PHP code example of xepozz / remap

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

    

xepozz / remap example snippets


final class Todo
{
    public int $id;
    public string $title;
    public int $status;
    public int $created_at;
}

$sql = 'SELECT id, title, status, created_at FROM todo WHERE id = :id LIMIT 1';
$params = ['id' => 1];

$remap->map(Todo::class, $sql, $params); // returns \Generator of Todo objects

use Yiisoft\Hydrator\Attribute\Parameter\Data;
use Yiisoft\Hydrator\Attribute\SkipHydration;

final class TodoModelHydrator
{
    #[Data('id')]
    public string $identifier;
    #[Data('title')]
    public string $text;
    public string $status;
    #[Data('created_at')]
    public string $date_created;
}

final class Todo
{
    public int $id;
    public string $title;
    public int $status;
    public int $created_at;

    public static function selectOne(int $id): array
    {
        return [
            'SELECT id, title, status, created_at FROM todo WHERE id = :id LIMIT 1',
            ['id' => $id],
        ];
    }

    public static function selectAll(): string
    {
        return 'SELECT id, title, status, created_at FROM todo';
    }
}

$remap->map(Todo::class, ...Todo::selectOne(1)); // selectOne may return ['SELECT ...', ['id' => 1]]
$remap->map(Todo::class, Todo::selectOne(1)); // or shorter, without unpacking
$remap->map(Todo::class, Todo::selectAll());
$remap->map(...Todo::selectAll()); // selectAll may return [Todo::class, "SELECT ..."]

$remap->map(...)

$result = iterator_to_array(
    $remap->map(Todo::class, Todo::selectOne(1))
);

$result = [...$remap->map(Todo::class, Todo::selectOne(1))];