Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json serializable paginator repository #14475

Merged
merged 3 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# [4.0.0-rc.2](https://github.com/phalcon/cphalcon/releases/tag/v4.0.0-rc.2) (2019-XX-XX)
## Added
- Added `cast` parameter to `Phalcon\Collection::get` and `Phalcon\Helper\Arr::get` allowing developers to cast the result returned to the type they want to. [#14465](https://github.com/phalcon/cphalcon/pull/14465)
- Added `Phalcon\Paginator\Repository::jsonSerialize()` implementing `JsonSerializable` [#14475](https://github.com/phalcon/cphalcon/pull/14475)

## Changed
- Changed all calls to `new <object>` to use the `create_instance` or `create_instance_params` for better performance. [#14419](https://github.com/phalcon/cphalcon/pull/14419)
Expand Down
6 changes: 3 additions & 3 deletions phalcon/Paginator/PaginatorFactory.zep
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Phalcon\Paginator;

use Phalcon\Paginator\Adapter\AbstractAdapter;
use Phalcon\Paginator\Adapter\AdapterInterface;
use Phalcon\Factory\AbstractFactory;
use Phalcon\Helper\Arr;

Expand Down Expand Up @@ -47,7 +47,7 @@ class PaginatorFactory extends AbstractFactory
* $paginator = (new PaginatorFactory())->load($options);
*```
*/
public function load(var config) -> var
public function load(var config) -> <AdapterInterface>
{
var name, options;

Expand All @@ -61,7 +61,7 @@ class PaginatorFactory extends AbstractFactory
/**
* Create a new instance of the adapter
*/
public function newInstance(string! name, array! options = []) -> <AbstractAdapter>
public function newInstance(string! name, array! options = []) -> <AdapterInterface>
{
var definition;

Expand Down
11 changes: 10 additions & 1 deletion phalcon/Paginator/Repository.zep
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@

namespace Phalcon\Paginator;

use JsonSerializable;
use Phalcon\Helper\Arr;

/**
* Phalcon\Paginator\Repository
*
* Repository of current state Phalcon\Paginator\AdapterInterface::paginate()
*/
class Repository implements RepositoryInterface
class Repository implements RepositoryInterface, JsonSerializable
{
/**
* @var array
Expand Down Expand Up @@ -126,6 +127,14 @@ class Repository implements RepositoryInterface
return this->getProperty(self::PROPERTY_TOTAL_ITEMS, 0);
}

/**
* See [jsonSerialize](https://php.net/manual/en/jsonserializable.jsonserialize.php)
*/
public function jsonSerialize() -> array
{
return this->properties;
}

/**
* {@inheritdoc}
*/
Expand Down
95 changes: 95 additions & 0 deletions tests/integration/Paginator/Repository/JsonSerializeCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
declare(strict_types=1);

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Test\Integration\Paginator\Repository;

use IntegrationTester;
use Phalcon\Paginator\Adapter\NativeArray;
use Phalcon\Paginator\Repository;

class JsonSerializeCest
{
/**
* Tests Phalcon\Paginator\Repository :: jsonSerialize()
*
* @param IntegrationTester $I
*
* @since 2019-10-16
*/
public function paginatorRepositoryJsonSerialize(IntegrationTester $I)
{
$I->wantToTest('Paginator\Repository - jsonSerialize()');

$data = [
'key' => 'value',
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
];

$repository = new Repository();
$repository->setProperties($data);

$I->assertEquals(
$data,
$repository->jsonSerialize()
);
}

/**
* Tests Phalcon\Paginator\Repository :: jsonSerialize()
*
* @param IntegrationTester $I
*
* @since 2019-10-16
*/
public function paginatorRepositoryJsonSerializeWithAdapter(IntegrationTester $I)
{
$I->wantToTest('Paginator\Repository - jsonSerialize() - with adapter');

$data = [
'key' => 'value',
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
];

$expectedData = [
'items' => [
'key' => 'value',
'key1' => 'value1',
],
'total_items' => 4,
'limit' => 2,
'first' => 1,
'previous' => 1,
'current' => 1,
'next' => 2,
'last' => 2,
];

$paginator = new NativeArray(
[
'data' => $data,
'limit' => 2,
'page' => 1,
]
);

$repository = $paginator->paginate();

$I->assertEquals(
$expectedData,
$repository->jsonSerialize()
);
}
}