Skip to content

Commit

Permalink
Change type of PaginatorComponent::$_paginator to Paginator
Browse files Browse the repository at this point in the history
  • Loading branch information
chinpei215 committed Jul 27, 2017
1 parent 0a5bf84 commit 68fb13a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Controller/Component/PaginatorComponent.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
use Cake\Controller\ComponentRegistry; use Cake\Controller\ComponentRegistry;
use Cake\Datasource\Exception\PageOutOfBoundsException; use Cake\Datasource\Exception\PageOutOfBoundsException;
use Cake\Datasource\Paginator; use Cake\Datasource\Paginator;
use Cake\Datasource\PaginatorInterface;
use Cake\Datasource\QueryInterface; use Cake\Datasource\QueryInterface;
use Cake\Network\Exception\NotFoundException; use Cake\Network\Exception\NotFoundException;
use InvalidArgumentException;


/** /**
* This component is used to handle automatic model data pagination. The primary way to use this * This component is used to handle automatic model data pagination. The primary way to use this
Expand Down Expand Up @@ -60,7 +60,7 @@ class PaginatorComponent extends Component
/** /**
* Datasource paginator instance. * Datasource paginator instance.
* *
* @var \Cake\Datasource\PaginatorInterface * @var \Cake\Datasource\Paginator
*/ */
protected $_paginator; protected $_paginator;


Expand All @@ -70,6 +70,9 @@ class PaginatorComponent extends Component
public function __construct(ComponentRegistry $registry, array $config = []) public function __construct(ComponentRegistry $registry, array $config = [])
{ {
if (isset($config['paginator'])) { if (isset($config['paginator'])) {
if (!$config['paginator'] instanceof Paginator) {
throw new InvalidArgumentException('Paginator must be an instance of ' . Paginator::class);
}
$this->_paginator = $config['paginator']; $this->_paginator = $config['paginator'];
unset($config['paginator']); unset($config['paginator']);
} else { } else {
Expand Down Expand Up @@ -237,10 +240,10 @@ public function mergeOptions($alias, $settings)
/** /**
* Set paginator instance. * Set paginator instance.
* *
* @param \Cake\Datasource\PaginatorInterface $paginator Paginator instance. * @param \Cake\Datasource\Paginator $paginator Paginator instance.
* @return self * @return self
*/ */
public function setPaginator(PaginatorInterface $paginator) public function setPaginator(Paginator $paginator)
{ {
$this->_paginator = $paginator; $this->_paginator = $paginator;


Expand All @@ -250,7 +253,7 @@ public function setPaginator(PaginatorInterface $paginator)
/** /**
* Get paginator instance. * Get paginator instance.
* *
* @return \Cake\Datasource\PaginatorInterface * @return \Cake\Datasource\Paginator
*/ */
public function getPaginator() public function getPaginator()
{ {
Expand All @@ -275,6 +278,7 @@ protected function _setPagingParams()
/** /**
* Proxy getting/setting config options to Paginator. * Proxy getting/setting config options to Paginator.
* *
* @deprecated 3.5.0 use setConfig()/getConfig() instead.
* @param string|array|null $key The key to get/set, or a complete array of configs. * @param string|array|null $key The key to get/set, or a complete array of configs.
* @param mixed|null $value The value to set. * @param mixed|null $value The value to set.
* @param bool $merge Whether to recursively merge or overwrite existing config, defaults to true. * @param bool $merge Whether to recursively merge or overwrite existing config, defaults to true.
Expand All @@ -283,7 +287,7 @@ protected function _setPagingParams()
public function config($key = null, $value = null, $merge = true) public function config($key = null, $value = null, $merge = true)
{ {
$return = $this->_paginator->config($key, $value, $merge); $return = $this->_paginator->config($key, $value, $merge);
if ($return instanceof PaginatorInterface) { if ($return instanceof Paginator) {
$return = $this; $return = $this;
} }


Expand Down
15 changes: 15 additions & 0 deletions tests/TestCase/Controller/Component/PaginatorComponentTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Cake\ORM\Entity; use Cake\ORM\Entity;
use Cake\ORM\TableRegistry; use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase; use Cake\TestSuite\TestCase;
use stdClass;


/** /**
* PaginatorTestController class * PaginatorTestController class
Expand Down Expand Up @@ -121,6 +122,20 @@ public function testPaginatorSetting()
$this->assertSame($paginator, $component->getPaginator()); $this->assertSame($paginator, $component->getPaginator());
} }


/**
* Test that an exception is thrown when paginator option is invalid.
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Paginator must be an instance of Cake\Datasource\Paginator
* @return void
*/
public function testInvalidPaginatorOption()
{
new PaginatorComponent($this->registry, [
'paginator' => new stdClass()
]);
}

/** /**
* Test that non-numeric values are rejected for page, and limit * Test that non-numeric values are rejected for page, and limit
* *
Expand Down

0 comments on commit 68fb13a

Please sign in to comment.