Skip to content

Commit

Permalink
Add QueryIteratorDataProvider::class. (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed May 7, 2023
1 parent 5a9de9c commit e5fae94
Show file tree
Hide file tree
Showing 18 changed files with 874 additions and 441 deletions.
21 changes: 17 additions & 4 deletions README.md
Expand Up @@ -15,7 +15,7 @@ For install this package, you need [composer](https://getcomposer.org/).
## Install

```shell
composer require yii-tools/awesome-component
composer require yii-tools/data-provider
```

## Usage
Expand Down Expand Up @@ -54,18 +54,31 @@ $arrayIteratorDataProvider = new ArrayIteratorDataProvider(
);
```

### SqlIteratorDataProvider
### QueryIteratorDataProvider

```php
<?php

declare(strict_types=1);

use Yii\DataProvider\SqlIteratorDataProvider;
use Yii\DataProvider\QueryIteratorDataProvider;

/** @var ConnectionInterface $db */
$queryIteratorDataProvider = new QueryIteratorDataProvider((new Query($db))->select('*')->from('{{%user}}'));
```

### SQLIteratorDataProvider

```php
<?php

declare(strict_types=1);

use Yii\DataProvider\SQLIteratorDataProvider;
use Yiisoft\Db\Connection\ConnectionInterface;

/** @var ConnectionInterface $db */
$sqlIteratorDataProvider = new SqlIteratorDataProvider($db, 'SELECT * FROM {{%user}}');
$sqlIteratorDataProvider = new SQLIteratorDataProvider($db, 'SELECT * FROM {{%user}}');
```

## Testing
Expand Down
Expand Up @@ -7,7 +7,7 @@
use ArrayIterator;
use Traversable;

abstract class AbstractIteratorProvider implements IteratorProviderInterface
abstract class AbstractIteratorDataDataProvider implements IteratorDataProviderInterface
{
protected int $limit = self::DEFAULT_LIMIT;
protected int $offset = self::DEFAULT_OFFSET;
Expand Down
Expand Up @@ -9,7 +9,7 @@
/**
* Provides a way to iterate over the results of an Active Query with support for pagination.
*/
final class ActiveIteratorProvider extends AbstractIteratorProvider
final class ActiveIteratorDataProvider extends AbstractIteratorDataDataProvider
{
public function __construct(private ActiveQueryInterface $activeQuery)
{
Expand Down
Expand Up @@ -10,7 +10,7 @@
/**
* Provides a way to iterate over an array with support for pagination.
*/
final class ArrayIteratorProvider extends AbstractIteratorProvider
final class ArrayIteratorDataProvider extends AbstractIteratorDataDataProvider
{
public function __construct(private array $data)
{
Expand Down
Expand Up @@ -13,7 +13,7 @@
*
* @extends IteratorAggregate<int, array>
*/
interface IteratorProviderInterface extends Countable, IteratorAggregate
interface IteratorDataProviderInterface extends Countable, IteratorAggregate
{
/**
* The default page size.
Expand Down
2 changes: 1 addition & 1 deletion src/OffsetPaginator.php
Expand Up @@ -22,7 +22,7 @@
*/
final class OffsetPaginator implements Countable, IteratorAggregate
{
public function __construct(private IteratorProviderInterface $iteratorProvider)
public function __construct(private IteratorDataProviderInterface $iteratorProvider)
{
}

Expand Down
29 changes: 29 additions & 0 deletions src/QueryIteratorDataProvider.php
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Yii\DataProvider;

use Yiisoft\Db\Query\QueryInterface;

final class QueryIteratorDataProvider extends AbstractIteratorDataDataProvider
{
public function __construct(private QueryInterface $query)
{
}

public function count(): int
{
return (int) $this->query->count('*');
}

public function read(): array
{
return $this->query->limit($this->limit)->offset($this->offset)->all();
}

public function readOne(): array
{
return $this->query->limit(1)->offset($this->offset)->all();
}
}
Expand Up @@ -11,7 +11,7 @@
/**
* Provides a way to iterate over the results of a SQL query with support for pagination.
*/
final class SqlIteratorDataProvider extends AbstractIteratorProvider
final class SQLIteratorDataDataProvider extends AbstractIteratorDataDataProvider
{
public function __construct(private ConnectionInterface $db, private string $sql, private array $params = [])
{
Expand Down

0 comments on commit e5fae94

Please sign in to comment.