MatthewPattell /
yii2-extended-api
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Created by PhpStorm. |
||||
| 4 | * Date: 2017-12-07 |
||||
| 5 | * Time: 03:12 |
||||
| 6 | */ |
||||
| 7 | |||||
| 8 | namespace MP\ExtendedApi; |
||||
| 9 | |||||
| 10 | use Yii; |
||||
| 11 | use yii\base\Event; |
||||
| 12 | use yii\data\ActiveDataProvider; |
||||
| 13 | use yii\rest\IndexAction; |
||||
| 14 | |||||
| 15 | /** |
||||
| 16 | * Class EIndexAction |
||||
| 17 | * @package MP\ExtendedApi |
||||
| 18 | * @author Yarmaliuk Mikhail |
||||
| 19 | * @version 1.0 |
||||
| 20 | */ |
||||
| 21 | class EIndexAction extends IndexAction |
||||
| 22 | { |
||||
| 23 | const EVENT_AFTER_PREPARE_DATAP_ROVIDER = 'afterPrepareDataProvider'; |
||||
| 24 | |||||
| 25 | /** |
||||
| 26 | * @var string |
||||
| 27 | */ |
||||
| 28 | public string $filterAttribute = 'filter'; |
||||
| 29 | |||||
| 30 | /** |
||||
| 31 | * @var string |
||||
| 32 | */ |
||||
| 33 | public string $extraFilter = 'extraFilter'; |
||||
| 34 | |||||
| 35 | /** |
||||
| 36 | * Add custom query condition |
||||
| 37 | * @see \Closure params |
||||
| 38 | * |
||||
| 39 | * @var null|array |
||||
| 40 | */ |
||||
| 41 | public ?array $addQuery = null; |
||||
| 42 | |||||
| 43 | /** |
||||
| 44 | * Column name |
||||
| 45 | * @see \Closure |
||||
| 46 | * |
||||
| 47 | * @var null|array |
||||
| 48 | */ |
||||
| 49 | public ?array $filterUser = null; |
||||
| 50 | |||||
| 51 | /** |
||||
| 52 | * Get filter params |
||||
| 53 | * |
||||
| 54 | * @return array |
||||
| 55 | */ |
||||
| 56 | public function getFilterParams(): array |
||||
| 57 | { |
||||
| 58 | $filterParams = Yii::$app->request->getQueryParams()[$this->filterAttribute] ?? []; |
||||
| 59 | |||||
| 60 | if (is_array($filterParams)) { |
||||
| 61 | return $filterParams; |
||||
| 62 | } else if (!empty($filterParams) && is_string($filterParams)) { |
||||
| 63 | return json_decode($filterParams, true); |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | return []; |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | /** |
||||
| 70 | * @inheritdoc |
||||
| 71 | */ |
||||
| 72 | protected function prepareDataProvider() |
||||
| 73 | { |
||||
| 74 | $filter = Yii::$app->request->get($this->filterAttribute); |
||||
| 75 | $extraFilter = Yii::$app->request->get($this->extraFilter); |
||||
| 76 | |||||
| 77 | if (!empty($filter) && is_string($filter)) { |
||||
| 78 | $this->setFilterParams(json_decode($filter, true)); |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | if (!empty($extraFilter) && is_string($extraFilter)) { |
||||
| 82 | $extraFilter = json_decode($extraFilter, true); |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | $this->prepareDataProvider = function (EIndexAction $action, $filter) use ($extraFilter) { |
||||
| 86 | /** @var ActiveDataProvider $dataProvider */ |
||||
| 87 | $dataProvider = call_user_func([$action->dataFilter->searchModel, 'getDataProvider']); |
||||
| 88 | $dataProvider->query->andWhere($filter); |
||||
| 89 | |||||
| 90 | if ($this->addQuery) { |
||||
| 91 | call_user_func($this->addQuery, $dataProvider->query, $extraFilter, $action->dataFilter, $dataProvider); |
||||
| 92 | |||||
| 93 | if ($action->dataFilter->hasErrors()) { |
||||
|
0 ignored issues
–
show
|
|||||
| 94 | return $action->dataFilter; |
||||
| 95 | } |
||||
| 96 | } |
||||
| 97 | |||||
| 98 | if ($this->filterUser) { |
||||
| 99 | $filterUserColumn = call_user_func($this->filterUser); |
||||
| 100 | |||||
| 101 | if ($filterUserColumn !== null) { |
||||
| 102 | $dataProvider->query->andWhere([$filterUserColumn => Yii::$app->user->getId()]); |
||||
|
0 ignored issues
–
show
The method
getId() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 103 | } |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | return $dataProvider; |
||||
| 107 | }; |
||||
| 108 | |||||
| 109 | $this->trigger(self::EVENT_AFTER_PREPARE_DATAP_ROVIDER, new Event()); |
||||
| 110 | |||||
| 111 | return parent::prepareDataProvider(); |
||||
| 112 | } |
||||
| 113 | |||||
| 114 | /** |
||||
| 115 | * Set filter params |
||||
| 116 | * |
||||
| 117 | * @param array $filterParams |
||||
| 118 | * |
||||
| 119 | * @return void |
||||
| 120 | */ |
||||
| 121 | public function setFilterParams(array $filterParams): void |
||||
| 122 | { |
||||
| 123 | $queryParams = Yii::$app->request->getQueryParams(); |
||||
| 124 | |||||
| 125 | $queryParams[$this->filterAttribute] = $filterParams; |
||||
| 126 | |||||
| 127 | Yii::$app->request->setQueryParams($queryParams); |
||||
| 128 | } |
||||
| 129 | } |
||||
| 130 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.