Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
refactor(Pager): Separate Pager as Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Aug 7, 2019
1 parent e786e9e commit 4442bb7
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 118 deletions.
4 changes: 2 additions & 2 deletions apps/models/form/News/SearchForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function callbackRules()
return ['checkPager'];
}

public function getRemoteTotal()
public function getRemoteTotal(): int
{
$search = $this->getData('search');
$query = $this->getData('query');
Expand All @@ -59,7 +59,7 @@ public function getRemoteTotal()
return $count;
}

public function getRemoteData()
public function getRemoteData(): array
{
$search = $this->search;
$query = $this->query;
Expand Down
4 changes: 2 additions & 2 deletions apps/models/form/Torrent/SnatchForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public static function callbackRules()
return ['checkPager', 'isExistTorrent'];
}

protected function getRemoteTotal()
protected function getRemoteTotal(): int
{
$tid = $this->getData('id');
return app()->pdo->createCommand('SELECT COUNT(`id`) FROM `snatched` WHERE `torrent_id` = :tid')->bindParams([
'tid' => $tid
])->queryScalar();
}

protected function getRemoteData()
protected function getRemoteData(): array
{
return app()->pdo->createCommand([
['SELECT * FROM `snatched` WHERE `torrent_id` = :tid ORDER BY finish_at,create_at DESC ', 'params' => ['tid' => $this->id]],
Expand Down
6 changes: 2 additions & 4 deletions apps/models/form/Torrents/SearchForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace apps\models\form\Torrents;


use apps\models\Torrent;
use Rid\Validators\Pager;

class SearchForm extends Pager
Expand All @@ -30,7 +28,7 @@ private function getTagsArray()
return $this->_tags;
}

protected function getRemoteTotal()
protected function getRemoteTotal(): int
{
$tags = $this->getTagsArray();

Expand All @@ -42,7 +40,7 @@ protected function getRemoteTotal()
])->queryScalar();
}

protected function getRemoteData()
protected function getRemoteData(): array
{
$tags = $this->getTagsArray();

Expand Down
4 changes: 2 additions & 2 deletions apps/models/form/Torrents/TagsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class TagsForm extends Pager

static $max_limit = 100;

public function getRemoteTotal()
public function getRemoteTotal(): int
{
return app()->pdo->createCommand([
['SELECT COUNT(tags.id) as `count` FROM tags'],
['WHERE `tags`.`tag` LIKE :tag', 'if' => !empty($search), 'params' => ['tag' => '%' . $this->getData('search') . '%']],
])->queryScalar();
}

public function getRemoteData()
public function getRemoteData(): array
{
$search = $this->search;
return app()->pdo->createCommand([
Expand Down
109 changes: 1 addition & 108 deletions framework/Validators/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,112 +11,5 @@

class Pager extends Validator
{
public $page;
public $limit; // pecPage
public $data;

protected $offset;
protected $total;

static $default_page = 1;
static $default_limit = 50;
static $max_limit = 50;
static $data_source = 'remote';

protected $pager_data_total;
protected $pager_data;

public static function defaultData(){
return [
'page' => static::$default_page,
'limit' => static::$default_limit
];
}

public static function inputRules()
{
return [
'page' => 'Integer', 'limit' => 'Integer'
];
}

public static function callbackRules()
{
return ['checkPager'];
}

/**
* @return mixed
*/
public function getPage()
{
return $this->page;
}

/**
* @return mixed
*/
public function getLimit()
{
return $this->limit;
}

/**
* @return mixed
*/
public function getOffset()
{
return $this->offset;
}

/**
* @return mixed
*/
public function getTotal()
{
return $this->total;
}

protected function checkPager()
{
$limit = $this->getData('limit', static::$default_limit);
if ($limit > static::$max_limit) $limit = static::$max_limit;
$page = $this->getData('page', static::$default_page);

$this->setData(['limit' => $limit, 'page' => $page]);

$this->total = $this->getDataTotal();
$this->offset = ($page - 1) * $limit;

// Quick return empty array when offset is much bigger than total, So we needn't hit remote or local data
if ($this->offset > $this->total) $this->pager_data = [];
}

final public function getDataTotal(): int
{
if (is_null($this->pager_data_total)) {
if (static::$data_source == 'remote') $this->pager_data_total = $this->getRemoteTotal();
else $this->pager_data_total = count($this->getData('data', []));
}
return $this->pager_data_total;
}

protected function getRemoteTotal()
{
throw new \RuntimeException('function "getRemoteTotal()" not implemented.');
}

final public function getPagerData()
{
if (is_null($this->pager_data)) {
if (static::$data_source == 'remote') $this->pager_data = $this->getRemoteData();
else $this->pager_data = array_slice($this->getData('data', []), $this->offset, $this->limit);
}
return $this->pager_data;
}

protected function getRemoteData()
{
throw new \RuntimeException('function "getRemoteData()" not implemented.');
}
use PagerTrait;
}
122 changes: 122 additions & 0 deletions framework/Validators/PagerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 8/7/2019
* Time: 7:37 PM
*/

namespace Rid\Validators;


Trait PagerTrait
{
public $page;
public $limit; // pecPage
public $data;

protected $offset;
protected $total;

static $default_page = 1;
static $default_limit = 50;
static $max_limit = 50;
static $data_source = 'remote';

protected $pager_data_total;
protected $pager_data;

public static function defaultData(){
return [
'page' => static::$default_page,
'limit' => static::$default_limit
];
}

public static function inputRules()
{
return [
'page' => 'Integer', 'limit' => 'Integer'
];
}

public static function callbackRules()
{
return ['checkPager'];
}

/**
* @return mixed
*/
public function getPage()
{
return $this->page;
}

/**
* @return mixed
*/
public function getLimit()
{
return $this->limit;
}

/**
* @return mixed
*/
public function getOffset()
{
return $this->offset;
}

/**
* @return mixed
*/
public function getTotal()
{
return $this->total;
}

protected function checkPager()
{
$limit = $this->getData('limit', static::$default_limit);
if ($limit > static::$max_limit) $limit = static::$max_limit;
$page = $this->getData('page', static::$default_page);

$this->setData(['limit' => $limit, 'page' => $page]);

$this->total = $this->getDataTotal();
$this->offset = ($page - 1) * $limit;

// Quick return empty array when offset is much bigger than total, So we needn't hit remote or local data
if ($this->offset > $this->total) $this->pager_data = [];
}

final public function getDataTotal(): int
{
if (is_null($this->pager_data_total)) {
if (static::$data_source == 'remote') $this->pager_data_total = $this->getRemoteTotal();
else $this->pager_data_total = count($this->getData('data', []));
}
return $this->pager_data_total;
}

protected function getRemoteTotal(): int
{
throw new \RuntimeException('function "getRemoteTotal()" not implemented.');
}

final public function getPagerData(): array
{
if (is_null($this->pager_data)) {
if (static::$data_source == 'remote') $this->pager_data = $this->getRemoteData();
else $this->pager_data = array_slice($this->getData('data', []), $this->offset, $this->limit);
}
return $this->pager_data;
}

protected function getRemoteData(): array
{
throw new \RuntimeException('function "getRemoteData()" not implemented.');
}
}

0 comments on commit 4442bb7

Please sign in to comment.