1. Go to this page and download the library: Download yiisoft/data 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/ */
yiisoft / data example snippets
$reader = new MyDataReader(...);
$result = $reader->read();
// using is foreach
foreach ($result as $item) {
// ...
}
// preparing array
$dataArray = $result instanceof \Traversable ? iterator_to_array($result, true) : (array)$result;
$reader = (new MyDataReader(...))->withLimit(10);
foreach ($reader->read() as $item) {
// ...
}
$reader = new MyDataReader(...);
$total = count($reader);
$filter = new All(
new GreaterThan('id', 3),
new Like('name', 'agent')
);
$reader = (new MyDataReader(...))
->withFilter($filter);
$data = $reader->read();
// own filter for filtering
class OwnNotTwoFilter implenents FilterInterface
{
private $field;
public function __construct($field)
{
$this->field = $field;
}
public static function getOperator(): string
{
return 'my!2';
}
public function toArray(): array
{
return [static::getOperator(), $this->field];
}
}
// own iterable filter handler for matching
class OwnIterableNotTwoFilterHandler implements IterableFilterHandlerInterface
{
public function getOperator(): string
{
return OwnNotTwoFilter::getOperator();
}
public function match(array $item, array $arguments, array $filterHandlers): bool
{
[$field] = $arguments;
return $item[$field] != 2;
}
}
// and using it on a data reader
$filter = new All(
new LessThan('id', 8),
new OwnNotTwoFilter('id'),
);
$reader = (new MyDataReader(...))
->withFilter($filter)
->withFilterHandlers(
new OwnIterableNotTwoFilter()
new OwnSqlNotTwoFilter() // for SQL
// and for any supported readers...
);
$data = $reader->read();