PHP code example of gnugat / query-bus

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

    

gnugat / query-bus example snippets




nugat\QueryBus\QueryBus;

class GetArticle
{
    public $id;

    public function __construct($id)
    {
        if (null === $id) {
            throw new \InvalidArgumentException('Required parameter ID is missing');
        }
        $this->id = (int) $id;
    }
}

// ...

use Gnugat\QueryBus\QueryMatcher;

class GetArticleMatcher implements QueryMatcher
{
    private $connection;

    public function __construct($connection)
    {
        $this->connection = $connection;
        pg_prepare($this->connection, 'get_article', 'SELECT id, title, content FROM articles WHERE id = $1');
    }

    public function supports($query)
    {
        return $query instanceof GetArticle;
    }

    public function match($query)
    {
        $result = pg_execute($this->connection, 'get_article', array($query->id));

        return pg_fetch_array($result, NULL, PGSQL_ASSOC);
    }
}

// ...

use Gnugat\QueryBus\QueryBus;

$connection = pg_pconnect('dbname=blog');
$queryBus = new QueryBus();
$queryBus->add(new GetArticleMatcher($connection));

// ...

$articles = $queryBus->match(new GetArticle(42));