-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ONGR Team
committed
Oct 30, 2014
0 parents
commit 3cb594c
Showing
26 changed files
with
1,437 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.idea | ||
.DS_Store | ||
vendor | ||
composer.lock | ||
Tests/app/cache | ||
Tests/app/logs | ||
Tests/app/build |
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,10 @@ | ||
language: php | ||
php: | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
before_script: | ||
- composer install | ||
script: | ||
- vendor/bin/phpunit | ||
- vendor/bin/phpcs -p --standard=PSR2 --ignore=vendor/,Tests/app/ ./ |
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,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\PagerBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\ConfigurationInterface; | ||
|
||
/** | ||
* This is the class that validates and merges configuration from app/config files. | ||
*/ | ||
class Configuration implements ConfigurationInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getConfigTreeBuilder() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$treeBuilder->root('ongr_pager'); | ||
|
||
return $treeBuilder; | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\PagerBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Loader; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
|
||
/** | ||
* This is the class that loads and manages your bundle configuration. | ||
*/ | ||
class ONGRPagerExtension extends Extension | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); | ||
$loader->load('services.yml'); | ||
} | ||
} |
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,19 @@ | ||
Copyright (c) 2014 NFQ Technologies UAB | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\PagerBundle; | ||
|
||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
/** | ||
* Main class for bundle init. | ||
*/ | ||
class ONGRPagerBundle extends Bundle | ||
{ | ||
} |
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,253 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ONGR package. | ||
* | ||
* (c) NFQ Technologies UAB <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ONGR\PagerBundle\Pager; | ||
|
||
use ONGR\PagerBundle\Paging\PagerAdapterInterface; | ||
|
||
/** | ||
* Returns all the required data to paginate. | ||
*/ | ||
class PagerService | ||
This comment has been minimized.
Sorry, something went wrong. |
||
{ | ||
/** | ||
* Current page. | ||
* | ||
* @var int | ||
*/ | ||
private $page = 1; | ||
|
||
/** | ||
* Number of items per page. | ||
* | ||
* @var int | ||
*/ | ||
private $limit = 20; | ||
|
||
/** | ||
* Maximum number of pages. | ||
* | ||
* @var int | ||
*/ | ||
private $maxPages = 10; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param PagerAdapterInterface $adapter The pager adapter. | ||
* @param array $options Additional options. | ||
*/ | ||
public function __construct(PagerAdapterInterface $adapter, array $options = []) | ||
{ | ||
$this->adapter = $adapter; | ||
|
||
if (isset($options['limit'])) { | ||
$this->setLimit($options['limit']); | ||
} | ||
|
||
if (isset($options['page'])) { | ||
$this->setPage($options['page']); | ||
} | ||
|
||
if (isset($options['max_pages'])) { | ||
$this->setMaxPages($options['max_pages']); | ||
} | ||
} | ||
|
||
/** | ||
* Sets the current page number. | ||
* | ||
* @param int $page The current page number. | ||
*/ | ||
public function setPage($page) | ||
{ | ||
$this->page = min($page > 0 ? $page : $this->getFirstPage(), $this->getLastPage()); | ||
} | ||
|
||
/** | ||
* Returns the current page number. | ||
* | ||
* @return int | ||
*/ | ||
public function getPage() | ||
{ | ||
return $this->page; | ||
} | ||
|
||
/** | ||
* Sets the results limit for one page. | ||
* | ||
* @param int $limit | ||
*/ | ||
public function setLimit($limit) | ||
{ | ||
$this->limit = $limit > 0 ? $limit : 1; | ||
|
||
$this->setPage($this->page); | ||
} | ||
|
||
/** | ||
* Returns the current results limit for one page. | ||
* | ||
* @return int | ||
*/ | ||
public function getLimit() | ||
{ | ||
return $this->limit; | ||
} | ||
|
||
/** | ||
* Sets the number of pages shown. | ||
* | ||
* @param int $maxPages | ||
*/ | ||
public function setMaxPages($maxPages) | ||
{ | ||
$this->maxPages = $maxPages; | ||
} | ||
|
||
/** | ||
* Returns the number of pages shown. | ||
* | ||
* @return int | ||
*/ | ||
public function getMaxPages() | ||
{ | ||
return $this->maxPages; | ||
} | ||
|
||
/** | ||
* Returns the next page number. | ||
* | ||
* @return int | ||
*/ | ||
public function getNextPage() | ||
{ | ||
$lastPage = $this->getLastPage(); | ||
|
||
return $this->page < $lastPage ? $this->page + 1 : $lastPage; | ||
} | ||
|
||
/** | ||
* Returns the previous page number. | ||
* | ||
* @return int | ||
*/ | ||
public function getPreviousPage() | ||
{ | ||
return $this->page > $this->getFirstPage() ? $this->page - 1 : $this->getFirstPage(); | ||
} | ||
|
||
/** | ||
* Returns true if the current page is first. | ||
* | ||
* @return bool | ||
*/ | ||
public function isFirstPage() | ||
{ | ||
return $this->page == 1; | ||
} | ||
|
||
/** | ||
* Returns the first page number. | ||
* | ||
* @return int | ||
*/ | ||
public function getFirstPage() | ||
{ | ||
return 1; | ||
} | ||
|
||
/** | ||
* Returns true if the current page is last. | ||
* | ||
* @return bool | ||
*/ | ||
public function isLastPage() | ||
{ | ||
return $this->page == $this->getLastPage(); | ||
} | ||
|
||
/** | ||
* Returns the last page number. | ||
* | ||
* @return int | ||
*/ | ||
public function getLastPage() | ||
{ | ||
return $this->hasResults() ? ceil($this->adapter->getTotalResults() / $this->limit) : $this->getFirstPage(); | ||
} | ||
|
||
/** | ||
* Returns true if the current result set requires pagination. | ||
* | ||
* @return bool | ||
*/ | ||
public function isPaginable() | ||
{ | ||
return $this->adapter->getTotalResults() > $this->limit; | ||
} | ||
|
||
/** | ||
* Generates a page list. | ||
* | ||
* @return array The page list | ||
*/ | ||
public function getPages() | ||
{ | ||
$pages = $this->getMaxPages(); | ||
$tmp = $this->page - floor($pages / 2); | ||
$begin = $tmp > $this->getFirstPage() ? $tmp : $this->getFirstPage(); | ||
$end = min($begin + $pages - 1, $this->getLastPage()); | ||
|
||
return range($begin, $end, 1); | ||
} | ||
|
||
/** | ||
* Returns true if the current result set is not empty. | ||
* | ||
* @return bool | ||
*/ | ||
public function hasResults() | ||
{ | ||
return $this->adapter->getTotalResults() > 0; | ||
} | ||
|
||
/** | ||
* Returns results list for the current page and limit. | ||
* | ||
* @return array | ||
*/ | ||
public function getResults() | ||
{ | ||
return $this->hasResults() ? $this->adapter->getResults($this->getOffset(), $this->limit) : []; | ||
} | ||
|
||
/** | ||
* Returns offset. | ||
* | ||
* @return int | ||
*/ | ||
public function getOffset() | ||
{ | ||
return ($this->page - 1) * $this->limit; | ||
} | ||
|
||
/** | ||
* Returns the current adapter instance. | ||
* | ||
* @return PagerAdapterInterface | ||
*/ | ||
public function getAdapter() | ||
{ | ||
return $this->adapter; | ||
} | ||
} |
Oops, something went wrong.
https://github.com/makerlabs/PagerBundle/blob/master/Pager.php
credit for the author? :)