2016-01-16 05:02:28 +00:00
|
|
|
<?php namespace Backend\Widgets\Table;
|
2014-12-11 06:46:17 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Base class for the Table widget data sources.
|
|
|
|
|
*/
|
2016-01-16 05:02:28 +00:00
|
|
|
abstract class DataSourceBase
|
2014-12-11 06:46:17 +00:00
|
|
|
{
|
|
|
|
|
/**
|
2017-04-24 11:38:19 +00:00
|
|
|
* @var string Specifies a name of record's key column
|
2014-12-11 06:46:17 +00:00
|
|
|
*/
|
|
|
|
|
protected $keyColumn;
|
|
|
|
|
|
2014-12-12 06:12:43 +00:00
|
|
|
/**
|
|
|
|
|
* @var integer Internal record offset
|
|
|
|
|
*/
|
|
|
|
|
protected $offset = 0;
|
|
|
|
|
|
2014-12-11 06:46:17 +00:00
|
|
|
/**
|
|
|
|
|
* Class constructor.
|
|
|
|
|
* @param string $keyColumn Specifies a name of the key column.
|
|
|
|
|
*/
|
2015-01-09 23:59:58 +00:00
|
|
|
public function construct($keyColumn = 'id')
|
2014-12-11 06:46:17 +00:00
|
|
|
{
|
|
|
|
|
$this->keyColumn = $keyColumn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initializes records in the data source.
|
|
|
|
|
* The method doesn't replace existing records and
|
|
|
|
|
* could be called multiple times in order to fill
|
|
|
|
|
* the data source.
|
|
|
|
|
* @param array $records Records to initialize in the data source.
|
|
|
|
|
*/
|
|
|
|
|
abstract public function initRecords($records);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a total number of records in the data source.
|
|
|
|
|
* @return integer
|
|
|
|
|
*/
|
|
|
|
|
abstract public function getCount();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes all records from the data source.
|
|
|
|
|
*/
|
|
|
|
|
abstract public function purge();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return records from the data source.
|
|
|
|
|
* @param integer $offset Specifies the offset of the first record to return, zero-based.
|
|
|
|
|
* @param integer $count Specifies the number of records to return.
|
2016-01-16 06:38:05 +00:00
|
|
|
* @return array Returns the records.
|
2014-12-12 06:12:43 +00:00
|
|
|
* If there are no more records, returns an empty array.
|
2014-12-11 06:46:17 +00:00
|
|
|
*/
|
|
|
|
|
abstract public function getRecords($offset, $count);
|
2014-12-12 06:12:43 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Rewinds the the data source to the first record.
|
|
|
|
|
* Use this method with the readRecords() method.
|
|
|
|
|
*/
|
|
|
|
|
public function reset()
|
|
|
|
|
{
|
|
|
|
|
$this->offset = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a set of records from the data source.
|
|
|
|
|
* @param integer $count Specifies the number of records to return.
|
2015-01-03 00:57:50 +00:00
|
|
|
* @return array Returns the records.
|
2014-12-12 06:12:43 +00:00
|
|
|
* If there are no more records, returns an empty array.
|
|
|
|
|
*/
|
|
|
|
|
public function readRecords($count = 10)
|
|
|
|
|
{
|
|
|
|
|
$result = $this->getRecords($this->offset, $count);
|
|
|
|
|
$this->offset += count($result);
|
2015-01-03 00:57:50 +00:00
|
|
|
|
2014-12-12 06:12:43 +00:00
|
|
|
return $result;
|
|
|
|
|
}
|
2017-04-24 11:38:19 +00:00
|
|
|
}
|