This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Pager): Separate Pager as Trait
- Loading branch information
Showing
6 changed files
with
131 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} |