Created
May 18, 2014 19:26
-
-
Save sasajib/d8e4456be6f3c9423b8a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php namespace Sasajib\Pavilion\Api\NewsHeap; | |
use Sasajib\Pavilion\Api\Contracts\RequestValidatorInterface; | |
class PaginationFactory | |
{ | |
protected $types = [ | |
'normal' => '\Sasajib\Pavilion\Api\NewsHeap\PaginationType\NormalPaginator', | |
'fromto' => '\Sasajib\Pavilion\Api\NewsHeap\PaginationType\FromToPaginator' | |
]; | |
/** | |
* @var | |
*/ | |
private $validator; | |
public function __construct(RequestValidatorInterface $validator) | |
{ | |
$this->validator = $validator; | |
} | |
public function getType($input) | |
{ | |
if (preg_match('/-/', $input)) { | |
$data = $this->ValidateAndReturnInput($input); | |
return new $this->types['fromto']($data[0], $data[1]); | |
} else { | |
$limit = $this->getLimit($input); | |
return new $this->types['normal']($limit); | |
} | |
} | |
/** | |
* @param $input | |
* @return array | |
*/ | |
private function ValidateAndReturnInput($input) | |
{ | |
$limit = explode('-', $input); | |
$from = (int)$limit[0]; | |
$to = (int)$limit[1]; | |
$this->validator->validate($from, null); | |
$this->validator->validate($to, null); | |
return [$from, $to]; | |
} | |
private function getLimit($input) | |
{ | |
$ci = (int) $input; | |
$this->validator->validate($ci, null); | |
return $ci; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment