Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #114 from spiral/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
wolfy-j committed Mar 13, 2016
2 parents 1fa1107 + 68b1929 commit 697d04e
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"defuse/php-encryption": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
"phpunit/phpunit": "~4.0",
"mockery/mockery": "^0.9.4"
},
"autoload": {
"files": [
Expand Down
2 changes: 2 additions & 0 deletions source/Spiral/ORM/Entities/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -711,10 +711,12 @@ public function __clone()
{
foreach ($this->loaders as $name => $loader) {
$this->loaders[$name] = clone $loader;
$this->loaders[$name]->parent = $this;
}

foreach ($this->joiners as $name => $loader) {
$this->joiners[$name] = clone $loader;
$this->joiners[$name]->parent = $this;
}
}

Expand Down
180 changes: 180 additions & 0 deletions source/Spiral/Pagination/CountingPaginator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php
/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/
namespace Spiral\Pagination;

class CountingPaginator implements PredictableInterface, \Countable
{
/**
* @var int
*/
private $pageNumber = 1;

/**
* @var int
*/
private $countPages = 1;

/**
* @var int
*/
private $limit = 25;

/**
* @var int
*/
private $count = 0;

/**
* @param int $limit
*/
public function __construct($limit = 25)
{
$this->setLimit($limit);
}

/**
* {@inheritdoc}
*/
public function setPage($number)
{
$this->pageNumber = abs(intval($number));

//Real page number
return $this->getPage();
}

/**
* {@inheritdoc}
*/
public function getPage()
{
if ($this->pageNumber < 1) {
return 1;
}

if ($this->pageNumber > $this->countPages) {
return $this->countPages;
}

return $this->pageNumber;
}

/**
* {@inheritdoc}
*/
public function setLimit($limit)
{
$this->limit = $limit;
}

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

/**
* Get pagination offset.
*
* @return int
*/
public function getOffset()
{
return ($this->getPage() - 1) * $this->limit;
}

/**
* {@inheritdoc}
*/
public function paginate(PaginableInterface $paginable)
{
$this->setCount($paginable->count());

$paginable->offset($this->getOffset());
$paginable->limit($this->getLimit());

return $paginable;
}

/**
* {@inheritdoc}
*/
public function setCount($count)
{
$this->count = abs(intval($count));
if ($this->count > 0) {
$this->countPages = ceil($this->count / $this->limit);
} else {
$this->countPages = 1;
}

return $this;
}

/**
* @return int
*/
public function count()
{
return $this->count;
}

/**
* {@inheritdoc}
*/
public function countPages()
{
return $this->countPages;
}

/**
* {@inheritdoc}
*/
public function countDisplayed()
{
if ($this->getPage() == $this->countPages) {
return $this->count - $this->getOffset();
}

return $this->limit;
}

/**
* {@inheritdoc}
*/
public function isRequired()
{
return ($this->countPages > 1);
}

/**
* {@inheritdoc}
*/
public function nextPage()
{
if ($this->getPage() != $this->countPages) {
return $this->getPage() + 1;
}

return false;
}

/**
* {@inheritdoc}
*/
public function previousPage()
{
if ($this->getPage() > 1) {
return $this->getPage() - 1;
}

return false;
}
}
19 changes: 17 additions & 2 deletions source/Spiral/Pagination/PaginatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,28 @@ public function setPage($number);
*/
public function getPage();

/**
* Set pagination limit.
*
* @param int $limit
* @return $this
*/
public function setLimit($limit);

/**
* Get pagination limit (items per page).
*
* @return int
*/
public function getLimit();

/**
* Set initial paginator uri
*
* @internal to be moved to UriPaginator
* @param UriInterface $uri
*/
public function setUri(UriInterface $uri);
//public function setUri(UriInterface $uri);

/**
* Create page URL using specific page number. No domain or schema information included by
Expand All @@ -49,7 +64,7 @@ public function setUri(UriInterface $uri);
* @param int $pageNumber
* @return UriInterface
*/
public function uri($pageNumber);
//public function uri($pageNumber);

/**
* Apply paginator to paginable object.
Expand Down
Empty file added tests/Files/fixtures/.empty
Empty file.

0 comments on commit 697d04e

Please sign in to comment.