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.
52 lines
1.4 KiB
52 lines
1.4 KiB
<?php
|
|
|
|
|
|
namespace App\Tables;
|
|
|
|
use App\Models\Row;
|
|
use MightyPork\Utils\Utils;
|
|
|
|
/**
|
|
* Generates a sequence of string codes for internal naming of columns
|
|
*
|
|
* Produces database-unique lowercase identifiers for created or proposed columns.
|
|
* Column identifiers are used in row objects to uniquely identify data even if the
|
|
* revision header (columns object) is modified, such as reordering, changing name,
|
|
* adding new column in concurrent proposals, etc.
|
|
*
|
|
* Thanks to this uniqueness, it could even be possible to compare or merge forks
|
|
* of the same table.
|
|
*/
|
|
class ColumnNumerator
|
|
{
|
|
const ALPHABET = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
|
|
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
|
|
|
|
/** @var int */
|
|
private $next;
|
|
/** @var int */
|
|
private $last;
|
|
|
|
/**
|
|
* Create numerator for the given number of columns
|
|
*/
|
|
public function __construct($capacity)
|
|
{
|
|
list($this->next, $this->last) = Row::allocateColIDs($capacity);
|
|
}
|
|
|
|
/**
|
|
* Get next column name, incrementing the internal state
|
|
*
|
|
* @return string
|
|
*/
|
|
public function next()
|
|
{
|
|
if ($this->next > $this->last)
|
|
throw new \OutOfBoundsException("Column numerator has run out of allocated GCID slots");
|
|
|
|
$key = Utils::alphabetEncode($this->next, self::ALPHABET);
|
|
$this->next++;
|
|
return $key;
|
|
}
|
|
}
|
|
|