forked from Insolita/yii2-fractal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJsonApiActiveDataProvider.php
158 lines (143 loc) · 5.42 KB
/
JsonApiActiveDataProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* @copyright Copyright (c) 2020 Insolita <[email protected]> and contributors
* @license https://github.com/insolita/yii2-fractal/blob/master/LICENSE
*/
namespace insolita\fractal\providers;
use insolita\fractal\actions\HasResourceTransformer;
use insolita\fractal\pagination\JsonApiPaginator;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\ResourceInterface;
use Yii;
use yii\base\InvalidArgumentException;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\db\ActiveQueryInterface;
use yii\web\HeaderCollection;
use const SORT_ASC;
use const SORT_DESC;
/**
* The wrapper around ActiveDataProvider that helps to return valid League\Fractal\Resource\Collection
*/
class JsonApiActiveDataProvider extends ActiveDataProvider implements JsonApiDataProviderInterface
{
use HasResourceTransformer;
private $_pagination;
/**@var \insolita\fractal\providers\JsonApiSort $_sort */
private $_sort;
public function init():void
{
parent::init();
$this->initResourceTransformer();
}
/**
* @return JsonApiPaginator|false
* @throws \yii\base\InvalidConfigException
*/
public function getPagination()
{
if ($this->_pagination === null) {
$this->setPagination(['class' => JsonApiPaginator::class]);
}
return $this->_pagination;
}
/**
* @param array|bool|JsonApiPaginator $value
* @throws \yii\base\InvalidConfigException
*/
public function setPagination($value):void
{
if (is_array($value)) {
$config = ['class' => JsonApiPaginator::class];
$this->_pagination = Yii::createObject(array_merge($config, $value));
if (!$this->_pagination instanceof JsonApiPaginator) {
throw new InvalidArgumentException('Only JsonApiPaginator instance or false allowed');
}
$this->_pagination->totalCount = $this->getTotalCount();
} elseif ($value instanceof JsonApiPaginator) {
$this->_pagination = $value;
$this->_pagination->totalCount = $this->getTotalCount();
} elseif ($value === false) {
$this->_pagination = false;
} else {
throw new InvalidArgumentException('Only JsonApiPaginator instance, configuration array or false is allowed.');
}
}
/**
* @inheritdoc
* @return JsonApiSort|bool the sorting object. If this is false, it means the sorting is disabled.
* @throws \yii\base\InvalidConfigException
*/
public function getSort()
{
if ($this->_sort === null) {
$this->setSort([]);
}
return $this->_sort;
}
/**
* @inheritdoc
* @param array|JsonApiSort|bool $value the sort definition to be used by this data provider.
* @throws \yii\base\InvalidConfigException
*/
public function setSort($value)
{
if (is_array($value)) {
$config = ['class' => JsonApiSort::class];
if ($this->id !== null) {
$config['sortParam'] = $this->id . '-sort';
}
$this->_sort = Yii::createObject(array_merge($config, $value));
} elseif ($value instanceof JsonApiSort || $value === false) {
$this->_sort = $value;
} else {
throw new InvalidArgumentException('Only JsonApiSort instance, configuration array or false is allowed.');
}
if ($this->_sort !== false && $this->query instanceof ActiveQueryInterface) {
/* @var $modelClass Model */
$modelClass = $this->query->modelClass;
$this->_sort->tableName = $modelClass::tableName();
$model = $modelClass::instance();
if (empty($this->_sort->attributes)) {
foreach ($model->attributes() as $attribute) {
$this->_sort->attributes[$attribute] = [
'asc' => [$attribute => SORT_ASC],
'desc' => [$attribute => SORT_DESC],
'label' => $model->getAttributeLabel($attribute),
];
}
} else {
foreach ($this->_sort->attributes as $attribute => $config) {
if (!isset($config['label'])) {
$this->_sort->attributes[$attribute]['label'] = $model->getAttributeLabel($attribute);
}
}
}
}
}
/**
* @return \League\Fractal\Resource\ResourceInterface
* @throws \yii\base\InvalidConfigException
*/
public function toCollection():ResourceInterface
{
$resource = new Collection($this->getModels(), $this->transformer, $this->resourceKey);
$paginator = $this->getPagination();
if ($paginator !== false) {
$paginator->setItemsCount($this->getCount());
$resource->setPaginator($this->getPagination());
}
return $resource;
}
public function fillHeaders(HeaderCollection $headers):void
{
$paginator = $this->getPagination();
if ($paginator !== false) {
$paginator->setItemsCount($this->getCount());
}
$headers->set('X-Pagination-Total-Count', $this->getTotalCount())
->set('X-Pagination-Page-Count', $paginator->getPageCount())
->set('X-Pagination-Current-Page', $paginator->getPage())
->set('X-Pagination-Per-Page', $paginator->getPageSize());
}
}