datatable.directory codebase
https://datatable.directory/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.4 KiB
77 lines
1.4 KiB
6 years ago
|
<?php
|
||
|
|
||
|
|
||
|
namespace App\Tables;
|
||
|
|
||
|
|
||
|
abstract class BaseNumerator
|
||
|
{
|
||
|
/** @var int */
|
||
|
protected $next;
|
||
|
|
||
|
/** @var int */
|
||
|
protected $last;
|
||
|
|
||
|
/**
|
||
|
* BaseNumerator constructor.
|
||
|
*
|
||
|
* @param int|int[] $first - first index, or [first, last]
|
||
|
* @param int|null $last - last index, or null
|
||
|
*/
|
||
|
public function __construct($first, $last = null)
|
||
|
{
|
||
|
if (is_array($first) && $last === null) {
|
||
|
list($first, $last) = $first;
|
||
|
}
|
||
|
|
||
|
$this->next = $first;
|
||
|
$this->last = $last;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get next key, incrementing the internal state
|
||
|
*
|
||
|
* @return string
|
||
|
*/
|
||
|
public function next()
|
||
|
{
|
||
|
if (!$this->hasMore())
|
||
|
throw new \OutOfBoundsException("Column numerator has run out of allocated GCID slots");
|
||
|
|
||
|
$key = $this->getKey($this->next);
|
||
|
$this->next++;
|
||
|
return $key;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Convert numeric index to a key
|
||
|
*
|
||
|
* @param int $index
|
||
|
* @return mixed
|
||
|
*/
|
||
|
protected function getKey($index)
|
||
|
{
|
||
|
return $index; // simple default
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return bool - true iof there are more keys available
|
||
|
*/
|
||
|
protected function hasMore()
|
||
|
{
|
||
|
return $this->next <= $this->last;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Generate all keys
|
||
|
*
|
||
|
* @return \Generator
|
||
|
*/
|
||
|
public function generate()
|
||
|
{
|
||
|
while ($this->hasMore()) {
|
||
|
yield $this->next();
|
||
|
}
|
||
|
}
|
||
|
}
|