ArrayDataProvider.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. class ArrayDataProvider extends \CDataProvider
  3. {
  4. /**
  5. * @var string the name of the key field. This is a field that uniquely identifies a
  6. * data record. In database this would be the primary key.
  7. * Defaults to 'id'. If it's set to false, keys of {@link rawData} array are used.
  8. */
  9. public $keyField='id';
  10. /**
  11. * @var array the data that is not paginated or sorted. When pagination is enabled,
  12. * this property usually contains more elements than {@link data}.
  13. * The array elements must use zero-based integer keys.
  14. */
  15. public $rawData=array();
  16. /**
  17. * Constructor.
  18. * @param array $rawData the data that is not paginated or sorted. The array elements must use zero-based integer keys.
  19. * @param array $config configuration (name=>value) to be applied as the initial property values of this class.
  20. */
  21. public function __construct($rawData,$config=array())
  22. {
  23. $this->rawData=$rawData;
  24. foreach($config as $key=>$value)
  25. $this->$key=$value;
  26. }
  27. protected function fetchData()
  28. {
  29. return $this->rawData;
  30. }
  31. protected function fetchKeys()
  32. {
  33. if($this->keyField===false)
  34. return array_keys($this->rawData);
  35. $keys=array();
  36. foreach($this->getData() as $i=>$data)
  37. $keys[$i]=is_object($data) ? $data->{$this->keyField} : $data[$this->keyField];
  38. return $keys;
  39. }
  40. protected function calculateTotalItemCount()
  41. {
  42. return count($this->rawData);
  43. }
  44. }