|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace pastuhov\ymlcatalog\readers; |
|
4
|
|
|
|
|
5
|
|
|
use pastuhov\ymlcatalog\ReaderInterface; |
|
6
|
|
|
use yii\base\Exception; |
|
7
|
|
|
use yii\base\Model; |
|
8
|
|
|
use yii\data\ActiveDataProvider; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Класс выборки данных с помощью DataProvider. |
|
12
|
|
|
* |
|
13
|
|
|
* @package pastuhov\ymlcatalog\readers |
|
14
|
|
|
*/ |
|
15
|
|
|
class DataProviderReader implements ReaderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ActiveDataProvider|null |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $dataProvider = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \yii\data\Pagination|boolean|null |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $pagination = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Model[]|null |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $models = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @inheritdoc |
|
34
|
|
|
* |
|
35
|
|
|
* @param ActiveDataProvider $dataProvider |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function __construct(ActiveDataProvider $dataProvider = null) |
|
38
|
|
|
{ |
|
39
|
1 |
|
if (is_null($dataProvider)) { |
|
40
|
|
|
throw new Exception('DataProvider can`t be empty.'); |
|
41
|
|
|
} |
|
42
|
1 |
|
$this->dataProvider = $dataProvider; |
|
43
|
1 |
|
$this->pagination = $this->dataProvider->getPagination(); |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritdoc |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function rewind() |
|
50
|
|
|
{ |
|
51
|
1 |
|
if ($this->pagination && $this->pagination->getPage() != 0) { |
|
52
|
|
|
$this->pagination->setPage(0); |
|
53
|
|
|
$this->dataProvider->prepare(true); |
|
54
|
|
|
} |
|
55
|
1 |
|
$this->models = $this->dataProvider->getModels(); |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritdoc |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function next() |
|
62
|
|
|
{ |
|
63
|
1 |
|
if ($this->pagination) { |
|
64
|
1 |
|
$this->pagination->setPage($this->pagination->getPage() + 1); |
|
65
|
1 |
|
$this->dataProvider->prepare(true); |
|
66
|
1 |
|
$this->models = $this->dataProvider->getModels(); |
|
67
|
1 |
|
} else { |
|
68
|
1 |
|
$this->models = null; |
|
69
|
|
|
} |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @inheritdoc |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function valid() |
|
76
|
|
|
{ |
|
77
|
1 |
|
return !empty($this->models); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @inheritdoc |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function current() |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->models; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @inheritdoc |
|
90
|
|
|
*/ |
|
91
|
|
|
public function key() |
|
92
|
|
|
{ |
|
93
|
|
|
$key = 0; |
|
94
|
|
|
if ($this->pagination) { |
|
95
|
|
|
$key = $this->pagination->getPage(); |
|
96
|
|
|
} |
|
97
|
|
|
return $key; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|