Skip to content

Commit

Permalink
deprecate PagerInterface::getNbResults() in favour of countResults()
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Jan 3, 2021
1 parent c68ea81 commit e6090d0
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 20 deletions.
11 changes: 10 additions & 1 deletion src/Action/SearchAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\BreadcrumbsBuilderInterface;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Datagrid\PagerInterface;
use Sonata\AdminBundle\Search\SearchHandler;
use Sonata\AdminBundle\Templating\TemplateRegistryInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
Expand Down Expand Up @@ -114,7 +115,15 @@ public function __invoke(Request $request): Response
];
}
$page = (int) $pager->getPage();
$total = (int) $pager->getNbResults();
if (method_exists($pager, 'countResults')) {
$total = (int) $pager->countResults();
} else {
@trigger_error(sprintf(
'Not implementing "%s::countResults()" is deprecated since sonata-project/admin-bundle 3.86 and will fail in 4.0.',
'Sonata\AdminBundle\Datagrid\PagerInterface'
), E_USER_DEPRECATED);
$total = (int) $pager->getNbResults();
}
}

$response = new JsonResponse([
Expand Down
25 changes: 22 additions & 3 deletions src/Datagrid/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public function getLinks($nbLinks = null)
*/
public function haveToPaginate()
{
return $this->getMaxPerPage() && $this->getNbResults() > $this->getMaxPerPage();
return $this->getMaxPerPage() && $this->countResults() > $this->getMaxPerPage();
}

/**
Expand Down Expand Up @@ -441,9 +441,21 @@ public function getLastIndice()
}

/**
* @deprecated since sonata-project/admin-bundle 3.86, use countResults instead
*
* @return int
*/
public function getNbResults()
{
@trigger_error(sprintf(
'The method "%s()" is deprecated since sonata-project/admin-bundle 3.86 and will be removed in 4.0. Use countResults() instead.',
__METHOD__
), E_USER_DEPRECATED);

return $this->countResults();
}

public function countResults(): int
{
return $this->nbResults;
}
Expand Down Expand Up @@ -739,7 +751,7 @@ public function valid()
/**
* NEXT_MAJOR: Remove this method.
*
* @deprecated since sonata-project/admin-bundle 3.84, use getNbResults instead
* @deprecated since sonata-project/admin-bundle 3.84, use countResults instead
*/
public function count()
{
Expand All @@ -748,7 +760,7 @@ public function count()
__METHOD__
), E_USER_DEPRECATED);

return $this->getNbResults();
return $this->countResults();
}

/**
Expand Down Expand Up @@ -838,10 +850,17 @@ public function getQuery()
/**
* @param int $nb
*
* @deprecated since sonata-project/admin-bundle 3.86
*
* @return void
*/
protected function setNbResults($nb)
{
@trigger_error(sprintf(
'The method "%s()" is deprecated since sonata-project/admin-bundle 3.86 and will be removed in 4.0.',
__METHOD__
), E_USER_DEPRECATED);

$this->nbResults = $nb;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Datagrid/PagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @method int getPreviousPage()
* @method bool isFirstPage()
* @method bool isLastPage()
* @method int getNbResults()
* @method int countResults()
* @method array getLinks(?int $nbLinks = null)
* @method bool haveToPaginate()
* @method ProxyQueryInterface|null getQuery()
Expand Down Expand Up @@ -99,7 +99,7 @@ public function setQuery($query);
public function getResults();

// NEXT_MAJOR: uncomment this method in 4.0
// public function getNbResults(): int;
// public function countResults(): int;

// NEXT_MAJOR: uncomment this method 4.0
// /**
Expand Down
10 changes: 10 additions & 0 deletions src/Datagrid/SimplePager.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ public function __construct($maxPerPage = 10, $threshold = 1)
}

public function getNbResults()
{
@trigger_error(sprintf(
'The method "%s()" is deprecated since sonata-project/admin-bundle 3.86 and will be removed in 4.0. Use countResults() instead.',
__METHOD__
), E_USER_DEPRECATED);

return $this->countResults();
}

public function countResults(): int
{
$n = ($this->getLastPage() - 1) * $this->getMaxPerPage();
if ($this->getLastPage() === $this->getPage()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/views/Block/block_search_result.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ file that was distributed with this source code.
</h3>

<div class="box-tools pull-right">
{% if pager and pager.getNbResults() > 0 %}
<span class="badge">{{ pager.getNbResults() }}</span>
{% if pager and (pager.countResults is defined ? pager.countResults() : pager.getNbResults()) > 0 %}
<span class="badge">{{ pager.countResults is defined ? pager.countResults() : pager.getNbResults() }}</span>
{% elseif admin.hasRoute('create') and admin.hasAccess('create') %}
<a href="{{ admin.generateUrl('create') }}" class="btn btn-box-tool">
<i class="fa fa-plus" aria-hidden="true"></i>
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/views/Block/block_stats.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ file that was distributed with this source code.
<!-- small box -->
<div class="small-box {{ settings.color }}">
<div class="inner">
<h3>{{ pager.getNbResults() }}</h3>
<h3>{{ pager.countResults is defined ? pager.countResults() : pager.getNbResults() }}</h3>
<p>
{% if translation_domain %}
{{ settings.text|trans({'%count%': pager.getNbResults()}, translation_domain) }}
{{ settings.text|trans({'%count%': pager.countResults is defined ? pager.countResults() : pager.getNbResults()}, translation_domain) }}
{% else %}
{{ settings.text }}
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion tests/App/Datagrid/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function getResults(): array
return $this->repository->all();
}

public function getNbResults()
public function countResults(): int
{
return \count($this->getResults());
}
Expand Down
27 changes: 19 additions & 8 deletions tests/Datagrid/PagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,14 @@ public function testGetMaxRecordLimit(): void
$this->assertSame(99, $this->pager->getMaxRecordLimit());
}

/**
* @group legacy
*/
public function testGetNbResults(): void
{
$this->assertSame(0, $this->pager->getNbResults());

$this->callMethod($this->pager, 'setNbResults', [100]);
$this->setProperty($this->pager, 'nbResults', 100);

$this->assertSame(100, $this->pager->getNbResults());
}
Expand All @@ -155,7 +158,7 @@ public function testCount(): void
$this->expectDeprecation('The method "Sonata\AdminBundle\Datagrid\Pager::count()" is deprecated since sonata-project/admin-bundle 3.84 and will be removed in 4.0.');
$this->assertSame(0, $this->pager->count());

$this->callMethod($this->pager, 'setNbResults', [100]);
$this->setProperty($this->pager, 'nbResults', 100);

$this->assertSame(100, $this->pager->count());
}
Expand Down Expand Up @@ -293,7 +296,7 @@ public function testHaveToPaginate(): void
$this->pager->setMaxPerPage(10);
$this->assertFalse($this->pager->haveToPaginate());

$this->callMethod($this->pager, 'setNbResults', [100]);
$this->setProperty($this->pager, 'nbResults', 100);
$this->assertTrue($this->pager->haveToPaginate());
}

Expand Down Expand Up @@ -417,7 +420,7 @@ public function testGetCursor(): void
$this->pager->setCursor(300);
$this->assertSame(0, $this->pager->getCursor());

$this->callMethod($this->pager, 'setNbResults', [100]);
$this->setProperty($this->pager, 'nbResults', 100);

$this->pager->setCursor(5);
$this->assertSame(5, $this->pager->getCursor());
Expand All @@ -442,7 +445,7 @@ public function testGetObjectByCursor(): void
$object3 = new \stdClass();
$object3->foo = 'bar3';

$this->callMethod($this->pager, 'setNbResults', [3]);
$this->setProperty($this->pager, 'nbResults', 3);

$query = $this->createMock(ProxyQueryInterface::class);

Expand Down Expand Up @@ -557,7 +560,7 @@ public function testGetLastIndex(): void
$this->pager->setPage(0);
$this->assertSame(0, $this->pager->getLastIndex());

$this->callMethod($this->pager, 'setNbResults', [100]);
$this->setProperty($this->pager, 'nbResults', 100);

$this->assertSame(100, $this->pager->getLastIndex());

Expand Down Expand Up @@ -590,7 +593,7 @@ public function testGetNext(): void
$object3 = new \stdClass();
$object3->foo = 'bar3';

$this->callMethod($this->pager, 'setNbResults', [3]);
$this->setProperty($this->pager, 'nbResults', 3);

$query = $this->createMock(ProxyQueryInterface::class);

Expand Down Expand Up @@ -654,7 +657,7 @@ public function testGetPrevious(): void
$object3 = new \stdClass();
$object3->foo = 'bar3';

$this->callMethod($this->pager, 'setNbResults', [3]);
$this->setProperty($this->pager, 'nbResults', 3);

$query = $this->createMock(ProxyQueryInterface::class);

Expand Down Expand Up @@ -770,4 +773,12 @@ protected function callMethod($obj, string $name, array $args = [])

return $method->invokeArgs($obj, $args);
}

private function setProperty(object $obj, string $name, $value)
{
$class = new \ReflectionClass($obj);
$property = $class->getProperty($name);
$property->setAccessible(true);
$property->setValue($obj, $value);
}
}
2 changes: 1 addition & 1 deletion tests/Datagrid/SimplePagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function testNoPagesForNoResults(): void
$this->pager->setQuery($this->proxyQuery);
$this->pager->init();
$this->assertSame(1, $this->pager->getLastPage());
$this->assertSame(0, $this->pager->getNbResults());
$this->assertSame(0, $this->pager->countResults());
}

public function testInitNoQuery(): void
Expand Down

0 comments on commit e6090d0

Please sign in to comment.