Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ZAYEC77 committed May 15, 2017
1 parent 214e633 commit 71efb47
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions src/DataTableAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use yii\base\InvalidConfigException;
use yii\data\ActiveDataProvider;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\web\Response;

/**
Expand All @@ -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
Expand Down Expand Up @@ -66,20 +72,20 @@ class DataTableAction extends Action
*/
public $formatResponse;

/**
* Check if query is configured
* @throws InvalidConfigException
*/
public function init()
{
if ($this->query === null) {
throw new InvalidConfigException(get_class($this) . '::$query must be set.');
}
}

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 */
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand All @@ -177,7 +196,7 @@ public function formatData(ActiveQuery $query, $columns)

/**
* @param array $response
* @return ActiveQuery
* @return array|ActiveRecord[]
*/
public function formatResponse($response)
{
Expand Down

0 comments on commit 71efb47

Please sign in to comment.