diff --git a/CHANGELOG-4.0.md b/CHANGELOG-4.0.md index 7aac700e834..60a9ebfaf8c 100644 --- a/CHANGELOG-4.0.md +++ b/CHANGELOG-4.0.md @@ -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 ` to use the `create_instance` or `create_instance_params` for better performance. [#14419](https://github.com/phalcon/cphalcon/pull/14419) diff --git a/phalcon/Paginator/PaginatorFactory.zep b/phalcon/Paginator/PaginatorFactory.zep index 0225a982e56..ac45158e6f0 100644 --- a/phalcon/Paginator/PaginatorFactory.zep +++ b/phalcon/Paginator/PaginatorFactory.zep @@ -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; @@ -47,7 +47,7 @@ class PaginatorFactory extends AbstractFactory * $paginator = (new PaginatorFactory())->load($options); *``` */ - public function load(var config) -> var + public function load(var config) -> { var name, options; @@ -61,7 +61,7 @@ class PaginatorFactory extends AbstractFactory /** * Create a new instance of the adapter */ - public function newInstance(string! name, array! options = []) -> + public function newInstance(string! name, array! options = []) -> { var definition; diff --git a/phalcon/Paginator/Repository.zep b/phalcon/Paginator/Repository.zep index 03cc2ca8ada..76fe745b6ca 100644 --- a/phalcon/Paginator/Repository.zep +++ b/phalcon/Paginator/Repository.zep @@ -10,6 +10,7 @@ namespace Phalcon\Paginator; +use JsonSerializable; use Phalcon\Helper\Arr; /** @@ -17,7 +18,7 @@ use Phalcon\Helper\Arr; * * Repository of current state Phalcon\Paginator\AdapterInterface::paginate() */ -class Repository implements RepositoryInterface +class Repository implements RepositoryInterface, JsonSerializable { /** * @var array @@ -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} */ diff --git a/tests/integration/Paginator/Repository/JsonSerializeCest.php b/tests/integration/Paginator/Repository/JsonSerializeCest.php new file mode 100644 index 00000000000..9976bb96b6e --- /dev/null +++ b/tests/integration/Paginator/Repository/JsonSerializeCest.php @@ -0,0 +1,95 @@ + + * + * 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() + ); + } +}