Skip to content

Commit

Permalink
Fix tests and some minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
teohhanhui authored and abluchet committed Aug 31, 2018
1 parent 0f650eb commit 76bd7e4
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 190 deletions.
2 changes: 1 addition & 1 deletion features/hydra/collection.feature
Expand Up @@ -434,4 +434,4 @@ Feature: Collections support

When I send a "GET" request to "/dummies?itemsPerPage=0&page=2"
Then the response status code should be 400
And the JSON node "hydra:description" should be equal to "Page should not be greater than 1 if itemsPerPage is equal to 0"
And the JSON node "hydra:description" should be equal to "Page should not be greater than 1 if limit is equal to 0"
61 changes: 31 additions & 30 deletions src/Bridge/Doctrine/Orm/Extension/PaginationExtension.php
Expand Up @@ -21,11 +21,9 @@
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Util\Inflector;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrineOrmPaginator;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;

/**
* Applies pagination on the Doctrine query for resource collection when enabled.
Expand All @@ -52,41 +50,44 @@ public function __construct(ManagerRegistry $managerRegistry, /* ResourceMetadat
$requestStack = $resourceMetadataFactory;
$resourceMetadataFactory = $pagination;

if (3 < \count($args = func_get_args())) {
@trigger_error(sprintf('Passing "$enabled", "$clientEnabled", "$clientItemsPerPage", "$itemsPerPage", "$pageParameterName", "$enabledParameterName", "$itemsPerPageParameterName", "$maximumItemPerPage", "$partial", "$clientPartial" and "$partialParameterName" arguments is deprecated since API Platform 2.3 and will not be possible anymore in API Platform 3. Pass an instance of "%s" as third argument instead.', Paginator::class), E_USER_DEPRECATED);
}

$options = [];
$legacyArgs = [
['name' => 'enabled', 'type' => 'bool', 'default' => true],
['name' => 'client_enabled', 'type' => 'bool', 'default' => false],
['name' => 'client_items_per_page', 'type' => 'bool', 'default' => false],
['name' => 'items_per_page', 'type' => 'bool', 'default' => false],
['name' => 'page_parameter_name', 'type' => 'string', 'default' => 'page'],
['name' => 'enabled_parameter_name', 'type' => 'string', 'default' => 'pagination'],
['name' => 'items_per_page_parameter_name', 'type' => 'string', 'default' => 'itemsPerPage'],
['name' => 'maximum_items_per_page', 'type' => 'int', 'default' => null],
['name' => 'partial', 'type' => 'bool', 'default' => false],
['name' => 'client_partial', 'type' => 'bool', 'default' => false],
['name' => 'partial_parameter_name', 'type' => 'string', 'default' => 'partial'],
$legacyPaginationArgs = [
3 => ['arg_name' => 'enabled', 'option_name' => 'enabled', 'type' => 'bool', 'default' => true],
4 => ['arg_name' => 'clientEnabled', 'option_name' => 'client_enabled', 'type' => 'bool', 'default' => false],
5 => ['arg_name' => 'clientItemsPerPage', 'option_name' => 'client_items_per_page', 'type' => 'bool', 'default' => false],
6 => ['arg_name' => 'itemsPerPage', 'option_name' => 'items_per_page', 'type' => 'int', 'default' => 30],
7 => ['arg_name' => 'pageParameterName', 'option_name' => 'page_parameter_name', 'type' => 'string', 'default' => 'page'],
8 => ['arg_name' => 'enabledParameterName', 'option_name' => 'enabled_parameter_name', 'type' => 'string', 'default' => 'pagination'],
9 => ['arg_name' => 'itemsPerPageParameterName', 'option_name' => 'items_per_page_parameter_name', 'type' => 'string', 'default' => 'itemsPerPage'],
10 => ['arg_name' => 'maximumItemPerPage', 'option_name' => 'maximum_items_per_page', 'type' => 'int', 'default' => null],
11 => ['arg_name' => 'partial', 'option_name' => 'partial', 'type' => 'bool', 'default' => false],
12 => ['arg_name' => 'clientPartial', 'option_name' => 'client_partial', 'type' => 'bool', 'default' => false],
13 => ['arg_name' => 'partialParameterName', 'option_name' => 'partial_parameter_name', 'type' => 'string', 'default' => 'partial'],
];

foreach ($legacyArgs as $i => $arg) {
$option = null;
if (array_key_exists($i + 3, $args)) {
if (!\call_user_func('is_'.$arg['type'], $args[$i + 3]) && !(null === $arg['default'] && null === $args[$i + 3])) {
throw new InvalidArgumentException(sprintf('The "$%s" argument is expected to be a %s%s.', Inflector::camelize($arg['name']), $arg['type'], null === $arg['default'] ? ' or null' : ''));
$paginationOptions = array_column($legacyPaginationArgs, 'default', 'option_name');

if (0 < \count($legacyArgs = \array_slice(\func_get_args(), 3, null, true))) {
@trigger_error(sprintf('Passing "$%s" arguments is deprecated since API Platform 2.3 and will not be possible anymore in API Platform 3. Pass an instance of "%s" as third argument instead.', implode('", "$', array_column($legacyPaginationArgs, 'arg_name')), Paginator::class), E_USER_DEPRECATED);

foreach ($legacyArgs as $pos => $arg) {
[
'arg_name' => $argName,
'option_name' => $optionName,
'type' => $type,
'default' => $default,
] = $legacyPaginationArgs[$pos];

if (!(\call_user_func("is_{$type}", $arg) || null === $default && null === $arg)) {
throw new InvalidArgumentException(sprintf('The "$%s" argument is expected to be a %s%s.', $argName, $type, null === $default ? ' or null' : ''));
}

$option = $args[$i + 3];
$paginationOptions[$optionName] = $arg;
}

$options[$arg['name']] = $option ?? $arg['default'];
}

$pagination = new Pagination($requestStack, $resourceMetadataFactory, $options);
$pagination = new Pagination($requestStack, $resourceMetadataFactory, $paginationOptions);
} elseif (!$resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) {
throw new InvalidArgumentException(sprintf('The "$resourceMetadataFactory" argument is expected to be an implementation of the "%s" interface.', MetadataFactoryInterface::class));
throw new InvalidArgumentException(sprintf('The "$resourceMetadataFactory" argument is expected to be an implementation of the "%s" interface.', ResourceMetadataFactoryInterface::class));
} elseif (!$pagination instanceof Pagination) {
throw new InvalidArgumentException(sprintf('The "$pagination" argument is expected to be an instance of the "%s" class.', Pagination::class));
}
Expand Down Expand Up @@ -167,7 +168,7 @@ private function useFetchJoinCollection(QueryBuilder $queryBuilder, string $reso

$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);

return $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_fetch_join_collection', true, true);
return $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_fetch_join_collection', true, true);
}

/**
Expand Down
36 changes: 23 additions & 13 deletions src/DataProvider/Pagination.php
Expand Up @@ -85,24 +85,31 @@ public function getLimit(string $resourceClass = null, string $operationName = n
$clientLimit = $this->options['client_items_per_page'];
$request = $this->requestStack->getCurrentRequest();

if (null !== $resourceClass && $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass)) {
if (null !== $resourceClass) {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
$limit = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_items_per_page', $limit, true);

if ($request) {
$clientLimit = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $clientLimit, true);
}
}

if (null !== $resourceClass && $request && $request->attributes->get('_graphql')) {
$collectionArgs = $request->attributes->get('_graphql_collections_args', []);
$limit = $collectionArgs[$resourceClass]['first'] ?? $limit;
if ($request->attributes->get('_graphql')) {
$collectionArgs = $request->attributes->get('_graphql_collections_args', []);
$limit = $collectionArgs[$resourceClass]['first'] ?? $limit;
}
}
}

if ($clientLimit && $request) {
$limit = (int) $this->getParameterFromRequest($request, $this->options['items_per_page_parameter_name'], $limit);
$maxItemsPerPage = $this->options['maximum_items_per_page'];

if (null !== $resourceClass) {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
$maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', $maxItemsPerPage, true);
}

if (null !== $this->options['maximum_items_per_page'] && $limit > $this->options['maximum_items_per_page']) {
$limit = $this->options['maximum_items_per_page'];
if (null !== $maxItemsPerPage && $limit > $maxItemsPerPage) {
$limit = $maxItemsPerPage;
}
}

Expand Down Expand Up @@ -134,15 +141,18 @@ private function getEnabled(string $resourceClass = null, string $operationName
$clientEnabled = $this->options[$partial ? 'client_partial' : 'client_enabled'];
$request = $this->requestStack->getCurrentRequest();

if (null !== $resourceClass && $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass)) {
if (null === $request) {
return false;
}

if (null !== $resourceClass) {
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
$enabled = $resourceMetadata->getCollectionOperationAttribute($operationName, $partial ? 'pagination_partial' : 'pagination_enabled', $enabled, true);

if ($request) {
$clientEnabled = $resourceMetadata->getCollectionOperationAttribute($operationName, $partial ? 'pagination_client_partial' : 'pagination_client_enabled', $clientEnabled, true);
}
$clientEnabled = $resourceMetadata->getCollectionOperationAttribute($operationName, $partial ? 'pagination_client_partial' : 'pagination_client_enabled', $clientEnabled, true);
}

if ($clientEnabled && $request) {
if ($clientEnabled) {
return filter_var($this->getParameterFromRequest($request, $this->options[$partial ? 'partial_parameter_name' : 'enabled_parameter_name'], $enabled), FILTER_VALIDATE_BOOLEAN);
}

Expand Down

0 comments on commit 76bd7e4

Please sign in to comment.