1. Go to this page and download the library: Download kafoso/questful 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/ */
kafoso / questful example snippets
use Kafoso\Questful\Exception\BadRequestException;
use Kafoso\Questful\Factory\Model\QueryParser\QueryParserFactory;
use Kafoso\Questful\Model\Bridge\PdoMySql\PdoMySql5_5;
use Kafoso\Questful\Model\Mapping;
use Kafoso\Questful\Model\Mapping\Allowable;
$queryParserFactory = new QueryParserFactory;
try {
$queryParser = $queryParserFactory->createFromUri($_SERVER['REQUEST_URI']);
$queryParser->parse(); // Captures malformed expressions; throws exceptions
$mapping = new Mapping($queryParser);
$mapping
->relate('name', 'u.name') // Allow this relation
->allow(new Allowable\Filter\AllowedLikeFilter('name')) // Allow a LIKE filter match
->allow(new Allowable\AllowedSort('name')) // Allow this sorting match
->validate(); // Validates input values (queryParser) vs allowed; throws exceptions
$pdoMySql = new PdoMySql5_5($mapping);
$pdoMySql->generate(); // Generates SQL and parameters; throws exceptions
$pdo = \PDO::getInstance(); // Some fully configured PDO instance
$stmt = $pdo->prepare(
"SELECT u.*
FROM User u
WHERE {$pdoMySql->getWhere()}
ORDER BY {$pdoMySql->getOrderBy()};"
);
$stmt->execute($pdoMySql->getParameters());
$json = json_encode($stmt->fetchAll(\PDO::FETCH_ASSOC));
header("HTTP/1.0 200 OK");
echo $json;
} catch (BadRequestException $e) {
header("HTTP/1.0 400 Bad Request");
throw $e;
} catch (\Exception $e) {
header("HTTP/1.0 500 Internal Server Error");
throw $e;
}
use Kafoso\Questful\Factory\Model\QueryParser\QueryParserFactory;
use Kafoso\Questful\Model\Mapping;
use Kafoso\Questful\Model\Mapping\Allowable;
$queryParserFactory = new QueryParserFactory;
$queryParser = $queryParserFactory->createFromUri('?filter[]=foo="bar"');
$queryParser->parse();
$mapping = new Mapping($queryParser);
$mapping
->relate('foo', 't.foo')
->allow(new Allowable\Filter\AllowedStringFilter('foo'))
->validate();
// All is good - "foo" is allowed
use Kafoso\Questful\Model\Mapping\Allowable;
$mapping->allow(new Allowable\Filter\AllowedStringFilter("id", ["="]));
use Kafoso\Questful\Model\Mapping\Allowable;
use Symfony\Component\Validator\Constraints as Assert;
$mapping->allow(new Allowable\Filter\AllowedStringFilter("id", null, [
new Assert\GreaterThan(1)
]));
use Kafoso\Questful\Factory\Model\QueryParser\QueryParserFactory;
use Kafoso\Questful\Model\Bridge\PdoMySql\PdoMySql5_5;
use Kafoso\Questful\Model\Mapping;
use Kafoso\Questful\Model\Mapping\Allowable;
$queryParserFactory = new QueryParserFactory;
$queryParser = $queryParserFactory->createFromUri('?filter[42]=foo="bar"');
$queryParser->parse();
$mapping = new Mapping($queryParser);
$mapping
->relate('foo', 't.foo')
->allow(new Allowable\Filter\AllowedStringFilter("foo"));
$pdoMySql = new PdoMySql5_5($mapping);
$pdoMySql->generate();
var_dump($pdoMySql->toArray());
/**
* Will output:
*
* array(3) {
* ["orderBy"]=>
* NULL
* ["parameters"]=>
* array(1) {
* ["filter_42"]=>
* string(3) "bar"
* }
* ["where"]=>
* string(27) "(t.foo = BINARY :filter_42)"
* }
*/
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.