Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladislav Tomsa committed Mar 16, 2017
0 parents commit 99a428b
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#

## Installation

via composer:

```sh
composer require adt/bulk-fetcher
```

## Usage

foreach (new \ADT\BulkFetcher($resultSet, 100) as $key => $value) {
// code
}
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "adt/bulk-fetcher",
"description": "",
"type": "library",
"license": [
"MIT",
"BSD-3-Clause",
"GPL-2.0",
"GPL-3.0"
],
"require-dev": {
"kdyby/doctrine": "^3.1",
"nette/utils": "^2.4",
"nette/di": "^2.4"
},
"autoload": {
"psr-4": {
"ADT\\BulkFetcher\\": "src/"
}
}
}
71 changes: 71 additions & 0 deletions src/BulkFetcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace ADT\BulkFetcher;

class BulkFetcher extends \Nette\Object implements \Iterator {

/** @var \Kdyby\Doctrine\ResultSet */
protected $resultSet;

/**
* Data from database
* @var array
*/
protected $bulkData;

/**
* Index of current fetched row
* @var integer
*/
protected $bulkDataIndex;

protected $limit;

protected $offset;

public function __construct(\Kdyby\Doctrine\ResultSet $resultSet, $bulkCount = 100) {
$this->resultSet = $resultSet;
$this->limit = $bulkCount;
}

public function rewind() {
$this->offset = 0;
$this->loadNewData();
}

public function current() {
return current($this->bulkData);
}

public function key() {
return key($this->bulkData);
}

public function next() {
$this->bulkDataIndex++;

if (next($this->bulkData) === FALSE) {
// fetch next bulk

if ($this->bulkDataIndex === $this->limit) {
// maybe we have more data

$this->offset += $this->limit; // next bulk
$this->loadNewData();
}
}
}

public function valid() {
return current($this->bulkData) !== FALSE;
}

protected function loadNewData() {
$this->bulkDataIndex = 0;
$this->bulkData = $this->resultSet
->applyPaging($this->offset, $this->limit)
->toArray();
}

}

0 comments on commit 99a428b

Please sign in to comment.