DataProvider.php 4.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\data;

use Yii;
use yii\base\Component;
use yii\base\InvalidParamException;

/**
Qiang Xue committed
15 16
 * DataProvider is the base class of data provider classes.
 *
17
 * It implements the [[getPagination()]] and [[getSort()]] methods as specified by the [[DataProviderInterface]].
18
 *
19
 * @property integer $count The number of data models in the current page. This property is read-only.
20 21 22
 * @property Pagination|boolean $pagination The pagination object. If this is false, it means the pagination
 * is disabled. Note that the type of this property differs in getter and setter. See [[getPagination()]] and
 * [[setPagination()]] for details.
23
 * @property Sort|boolean $sort The sorting object. If this is false, it means the sorting is disabled. Note
24
 * that the type of this property differs in getter and setter. See [[getSort()]] and [[setSort()]] for details.
25
 *
26 27 28
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
29
abstract class DataProvider extends Component implements DataProviderInterface
30
{
Qiang Xue committed
31 32 33 34 35 36 37
	/**
	 * @var string an ID that uniquely identifies the data provider among all data providers.
	 * You should set this property if the same page contains two or more different data providers.
	 * Otherwise, the [[pagination]] and [[sort]] mainly not work properly.
	 */
	public $id;

38 39 40 41
	private $_sort;
	private $_pagination;

	/**
42
	 * @return Pagination|boolean the pagination object. If this is false, it means the pagination is disabled.
43 44 45 46 47
	 */
	public function getPagination()
	{
		if ($this->_pagination === null) {
			$this->_pagination = new Pagination;
Qiang Xue committed
48 49 50
			if ($this->id !== null) {
				$this->_pagination->pageVar = $this->id . '-page';
			}
51
			$this->_pagination->totalCount = $this->getTotalCount();
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
		}
		return $this->_pagination;
	}

	/**
	 * Sets the pagination for this data provider.
	 * @param array|Pagination|boolean $value the pagination to be used by this data provider.
	 * This can be one of the following:
	 *
	 * - a configuration array for creating the pagination object. The "class" element defaults
	 *   to 'yii\data\Pagination'
	 * - an instance of [[Pagination]] or its subclass
	 * - false, if pagination needs to be disabled.
	 *
	 * @throws InvalidParamException
	 */
	public function setPagination($value)
	{
		if (is_array($value)) {
Qiang Xue committed
71 72 73 74 75 76 77
			$config = array(
				'class' => Pagination::className(),
			);
			if ($this->id !== null) {
				$config['pageVar'] = $this->id . '-page';
			}
			$this->_pagination = Yii::createObject(array_merge($config, $value));
78 79 80 81 82 83 84 85
		} elseif ($value instanceof Pagination || $value === false) {
			$this->_pagination = $value;
		} else {
			throw new InvalidParamException('Only Pagination instance, configuration array or false is allowed.');
		}
	}

	/**
86
	 * @return Sort|boolean the sorting object. If this is false, it means the sorting is disabled.
87 88 89 90
	 */
	public function getSort()
	{
		if ($this->_sort === null) {
91
			$this->setSort(array());
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
		}
		return $this->_sort;
	}

	/**
	 * Sets the sort definition for this data provider.
	 * @param array|Sort|boolean $value the sort definition to be used by this data provider.
	 * This can be one of the following:
	 *
	 * - a configuration array for creating the sort definition object. The "class" element defaults
	 *   to 'yii\data\Sort'
	 * - an instance of [[Sort]] or its subclass
	 * - false, if sorting needs to be disabled.
	 *
	 * @throws InvalidParamException
	 */
	public function setSort($value)
	{
		if (is_array($value)) {
Qiang Xue committed
111 112 113 114 115 116 117
			$config = array(
				'class' => Sort::className(),
			);
			if ($this->id !== null) {
				$config['sortVar'] = $this->id . '-sort';
			}
			$this->_sort = Yii::createObject(array_merge($config, $value));
118 119 120 121 122 123 124 125
		} elseif ($value instanceof Sort || $value === false) {
			$this->_sort = $value;
		} else {
			throw new InvalidParamException('Only Sort instance, configuration array or false is allowed.');
		}
	}

	/**
126 127
	 * Returns the number of data models in the current page.
	 * @return integer the number of data models in the current page.
128
	 */
Qiang Xue committed
129
	public function getCount()
130
	{
131
		return count($this->getModels());
132 133
	}
}