Skip to content
Draft
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
27 changes: 27 additions & 0 deletions src/Api/ContextAwareFilterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Api;

/**
* Context aware filter.
*
* @author Baptiste Meyer <baptiste.meyer@gmail.com>
*/
interface ContextAwareFilterInterface extends FilterInterface
{
/**
* {@inheritdoc}
*/
public function getDescription(string $resourceClass, array $context = []): array;
}
2 changes: 1 addition & 1 deletion src/Api/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ interface FilterInterface
*
* @see \ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer::getFiltersParameters
*/
public function getDescription(string $resourceClass): array;
public function getDescription(string $resourceClass/*, array $context = []*/): array;
}
15 changes: 13 additions & 2 deletions src/Bridge/Doctrine/Common/Filter/BooleanFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* For each property passed, if the resource does not have such property or if
* the value is not one of ( "true" | "false" | "1" | "0" ) the property is ignored.
*
* @internal
*
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
* @author Teoh Han Hui <teohhanhui@gmail.com>
* @author Alan Poulain <contact@alanpoulain.eu>
Expand All @@ -37,8 +39,13 @@ trait BooleanFilterTrait
/**
* {@inheritdoc}
*/
public function getDescription(string $resourceClass): array
public function getDescription(string $resourceClass/*, array $context = []*/): array
{
if (\func_num_args() < 2 && __CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName()) {
@trigger_error(sprintf('Method %s() will have a second `$context` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.4.', __FUNCTION__), E_USER_DEPRECATED);
}

$context = 1 < \func_num_args() ? (array) func_get_arg(1) : [];
$description = [];

$properties = $this->getProperties();
Expand All @@ -47,7 +54,11 @@ public function getDescription(string $resourceClass): array
}

foreach ($properties as $property => $unused) {
if (!$this->isPropertyMapped($property, $resourceClass) || !$this->isBooleanField($property, $resourceClass)) {
if (
!$this->isPropertyEnabled($property, $resourceClass, $context) ||
!$this->isPropertyMapped($property, $resourceClass) ||
!$this->isBooleanField($property, $resourceClass)
) {
continue;
}

Expand Down
15 changes: 13 additions & 2 deletions src/Bridge/Doctrine/Common/Filter/DateFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
/**
* Trait for filtering the collection by date intervals.
*
* @internal
*
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Théo FIDRY <theo.fidry@gmail.com>
* @author Alan Poulain <contact@alanpoulain.eu>
Expand All @@ -29,8 +31,13 @@ trait DateFilterTrait
/**
* {@inheritdoc}
*/
public function getDescription(string $resourceClass): array
public function getDescription(string $resourceClass/*, array $context = []*/): array
{
if (\func_num_args() < 2 && __CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName()) {
@trigger_error(sprintf('Method %s() will have a second `$context` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.4.', __FUNCTION__), E_USER_DEPRECATED);
}

$context = 1 < \func_num_args() ? (array) func_get_arg(1) : [];
$description = [];

$properties = $this->getProperties();
Expand All @@ -39,7 +46,11 @@ public function getDescription(string $resourceClass): array
}

foreach ($properties as $property => $nullManagement) {
if (!$this->isPropertyMapped($property, $resourceClass) || !$this->isDateField($property, $resourceClass)) {
if (
!$this->isPropertyEnabled($property, $resourceClass, $context) ||
!$this->isPropertyMapped($property, $resourceClass) ||
!$this->isDateField($property, $resourceClass)
) {
continue;
}

Expand Down
15 changes: 13 additions & 2 deletions src/Bridge/Doctrine/Common/Filter/ExistsFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
/**
* Trait for filtering the collection by whether a property value exists or not.
*
* @internal
*
* @author Teoh Han Hui <teohhanhui@gmail.com>
* @author Alan Poulain <contact@alanpoulain.eu>
*/
Expand All @@ -30,8 +32,13 @@ trait ExistsFilterTrait
/**
* {@inheritdoc}
*/
public function getDescription(string $resourceClass): array
public function getDescription(string $resourceClass/*, array $context = []*/): array
{
if (\func_num_args() < 2 && __CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName()) {
@trigger_error(sprintf('Method %s() will have a second `$context` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.4.', __FUNCTION__), E_USER_DEPRECATED);
}

$context = 1 < \func_num_args() ? (array) func_get_arg(1) : [];
$description = [];

$properties = $this->getProperties();
Expand All @@ -40,7 +47,11 @@ public function getDescription(string $resourceClass): array
}

foreach ($properties as $property => $unused) {
if (!$this->isPropertyMapped($property, $resourceClass, true) || !$this->isNullableField($property, $resourceClass)) {
if (
!$this->isPropertyEnabled($property, $resourceClass, $context) ||
!$this->isPropertyMapped($property, $resourceClass, true) ||
!$this->isNullableField($property, $resourceClass)
) {
continue;
}

Expand Down
15 changes: 13 additions & 2 deletions src/Bridge/Doctrine/Common/Filter/NumericFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
/**
* Trait for filtering the collection by numeric values.
*
* @internal
*
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
* @author Teoh Han Hui <teohhanhui@gmail.com>
* @author Alan Poulain <contact@alanpoulain.eu>
Expand All @@ -31,8 +33,13 @@ trait NumericFilterTrait
/**
* {@inheritdoc}
*/
public function getDescription(string $resourceClass): array
public function getDescription(string $resourceClass/*, array $context = []*/): array
{
if (\func_num_args() < 2 && __CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName()) {
@trigger_error(sprintf('Method %s() will have a second `$context` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.4.', __FUNCTION__), E_USER_DEPRECATED);
}

$context = 1 < \func_num_args() ? (array) func_get_arg(1) : [];
$description = [];

$properties = $this->getProperties();
Expand All @@ -41,7 +48,11 @@ public function getDescription(string $resourceClass): array
}

foreach ($properties as $property => $unused) {
if (!$this->isPropertyMapped($property, $resourceClass) || !$this->isNumericField($property, $resourceClass)) {
if (
!$this->isPropertyEnabled($property, $resourceClass, $context) ||
!$this->isPropertyMapped($property, $resourceClass) ||
!$this->isNumericField($property, $resourceClass)
) {
continue;
}

Expand Down
14 changes: 12 additions & 2 deletions src/Bridge/Doctrine/Common/Filter/OrderFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
/**
* Trait for ordering the collection by given properties.
*
* @internal
*
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Théo FIDRY <theo.fidry@gmail.com>
* @author Alan Poulain <contact@alanpoulain.eu>
Expand All @@ -34,8 +36,13 @@ trait OrderFilterTrait
/**
* {@inheritdoc}
*/
public function getDescription(string $resourceClass): array
public function getDescription(string $resourceClass/*, array $context = []*/): array
{
if (\func_num_args() < 2 && __CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName()) {
@trigger_error(sprintf('Method %s() will have a second `$context` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.4.', __FUNCTION__), E_USER_DEPRECATED);
}

$context = 1 < \func_num_args() ? (array) func_get_arg(1) : [];
$description = [];

$properties = $this->getProperties();
Expand All @@ -44,7 +51,10 @@ public function getDescription(string $resourceClass): array
}

foreach ($properties as $property => $propertyOptions) {
if (!$this->isPropertyMapped($property, $resourceClass)) {
if (
!$this->isPropertyEnabled($property, $resourceClass, $context) ||
!$this->isPropertyMapped($property, $resourceClass)
) {
continue;
}

Expand Down
14 changes: 12 additions & 2 deletions src/Bridge/Doctrine/Common/Filter/RangeFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
/**
* Trait for filtering the collection by range.
*
* @internal
*
* @author Lee Siong Chan <ahlee2326@me.com>
* @author Alan Poulain <contact@alanpoulain.eu>
*/
Expand All @@ -30,8 +32,13 @@ trait RangeFilterTrait
/**
* {@inheritdoc}
*/
public function getDescription(string $resourceClass): array
public function getDescription(string $resourceClass/*, array $context = []*/): array
{
if (\func_num_args() < 2 && __CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName()) {
@trigger_error(sprintf('Method %s() will have a second `$context` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.4.', __FUNCTION__), E_USER_DEPRECATED);
}

$context = 1 < \func_num_args() ? (array) func_get_arg(1) : [];
$description = [];

$properties = $this->getProperties();
Expand All @@ -40,7 +47,10 @@ public function getDescription(string $resourceClass): array
}

foreach ($properties as $property => $unused) {
if (!$this->isPropertyMapped($property, $resourceClass)) {
if (
!$this->isPropertyEnabled($property, $resourceClass, $context) ||
!$this->isPropertyMapped($property, $resourceClass)
) {
continue;
}

Expand Down
14 changes: 12 additions & 2 deletions src/Bridge/Doctrine/Common/Filter/SearchFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
/**
* Trait for filtering the collection by given properties.
*
* @internal
*
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Alan Poulain <contact@alanpoulain.eu>
*/
Expand All @@ -35,8 +37,13 @@ trait SearchFilterTrait
/**
* {@inheritdoc}
*/
public function getDescription(string $resourceClass): array
public function getDescription(string $resourceClass/*, array $context = []*/): array
{
if (\func_num_args() < 2 && __CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName()) {
@trigger_error(sprintf('Method %s() will have a second `$context` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.4.', __FUNCTION__), E_USER_DEPRECATED);
}

$context = 1 < \func_num_args() ? (array) func_get_arg(1) : [];
$description = [];

$properties = $this->getProperties();
Expand All @@ -45,7 +52,10 @@ public function getDescription(string $resourceClass): array
}

foreach ($properties as $property => $strategy) {
if (!$this->isPropertyMapped($property, $resourceClass, true)) {
if (
!$this->isPropertyEnabled($property, $resourceClass, $context) ||
!$this->isPropertyMapped($property, $resourceClass, true)
) {
continue;
}

Expand Down
49 changes: 49 additions & 0 deletions src/Bridge/Doctrine/Common/PropertyHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

namespace ApiPlatform\Core\Bridge\Doctrine\Common;

use ApiPlatform\Core\Exception\PropertyNotFoundException;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\PropertyMetadataFactoryOptionsTrait;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\DBAL\Types\Type;
Expand All @@ -26,8 +29,54 @@
*/
trait PropertyHelperTrait
{
use PropertyMetadataFactoryOptionsTrait;

/** @var PropertyMetadataFactoryInterface|null */
protected $propertyMetadataFactory;

abstract protected function getManagerRegistry(): ManagerRegistry;

/**
* Determines whether the given property is enabled.
*/
protected function isPropertyEnabled(string $property/*, string $resourceClass, array $context = []*/): bool
{
if (\func_num_args() < 3 && __CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName()) {
if (\func_num_args() < 2) {
@trigger_error(sprintf('Method %s() will have a second `$resourceClass` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.1.', __FUNCTION__), E_USER_DEPRECATED);
}

@trigger_error(sprintf('Method %s() will have a third "$context" argument in API Platform 3.0. Not defining it is deprecated since API Platform 2.4.', __FUNCTION__), E_USER_DEPRECATED);
}

$resourceClass = 1 < \func_num_args() ? (string) func_get_arg(1) : null;
$context = 2 < \func_num_args() ? (array) func_get_arg(2) : [];

if (null !== $this->properties) {
return \array_key_exists($property, $this->properties);
}

if (null !== $resourceClass && null !== $this->propertyMetadataFactory) {
try {
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property, $this->getPropertyMetadataFactoryOptions($context));
} catch (PropertyNotFoundException $e) {
return false;
}

// to ensure sanity, unreadable properties must still be explicitly enabled
if (!$propertyMetadata->isReadable()) {
return false;
}
}

// to ensure sanity, nested properties must still be explicitly enabled
if (null === $resourceClass) {
return !$this->isPropertyNested($property);
}

return !$this->isPropertyNested($property, $resourceClass);
}

/**
* Determines whether the given property is mapped.
*/
Expand Down
Loading