From 71efb477b2945a1c365280970dad0474228f0e6d Mon Sep 17 00:00:00 2001 From: Dmytro Karpovych Date: Mon, 15 May 2017 14:22:32 +0300 Subject: [PATCH] refactoring --- src/DataTableAction.php | 43 +++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/DataTableAction.php b/src/DataTableAction.php index 90ce666..3029055 100644 --- a/src/DataTableAction.php +++ b/src/DataTableAction.php @@ -13,6 +13,7 @@ use yii\base\InvalidConfigException; use yii\data\ActiveDataProvider; use yii\db\ActiveQuery; +use yii\db\ActiveRecord; use yii\web\Response; /** @@ -23,11 +24,16 @@ class DataTableAction extends Action { /** - * GET or POST - * + * Types of request method + */ + const REQUEST_METHOD_GET = 'GET'; + const REQUEST_METHOD_POST = 'POST'; + + /** + * @see \nullref\datatable\DataTableAction::getParam * @var string */ - public $requestMethod = "GET"; + public $requestMethod = self::REQUEST_METHOD_GET; /** * @var ActiveQuery @@ -66,6 +72,10 @@ class DataTableAction extends Action */ public $formatResponse; + /** + * Check if query is configured + * @throws InvalidConfigException + */ public function init() { if ($this->query === null) { @@ -73,13 +83,9 @@ public function init() } } - protected function getParam($name, $defaultValue = null) - { - return $this->requestMethod == 'GET' ? - Yii::$app->request->getQueryParam($name, $defaultValue) : - Yii::$app->request->getBodyParam($name, $defaultValue); - } - + /** + * @return array|ActiveRecord[] + */ public function run() { /** @var ActiveQuery $originalQuery */ @@ -114,6 +120,19 @@ public function run() return $this->formatResponse($response); } + /** + * Extract param from request + * @param $name + * @param null $defaultValue + * @return mixed + */ + protected function getParam($name, $defaultValue = null) + { + return $this->requestMethod == self::REQUEST_METHOD_GET ? + Yii::$app->request->getQueryParam($name, $defaultValue) : + Yii::$app->request->getBodyParam($name, $defaultValue); + } + /** * @param ActiveQuery $query * @param array $columns @@ -164,7 +183,7 @@ public function applyOrder(ActiveQuery $query, $columns, $order) /** * @param ActiveQuery $query * @param array $columns - * @return ActiveQuery + * @return array|ActiveRecord[] */ public function formatData(ActiveQuery $query, $columns) { @@ -177,7 +196,7 @@ public function formatData(ActiveQuery $query, $columns) /** * @param array $response - * @return ActiveQuery + * @return array|ActiveRecord[] */ public function formatResponse($response) {