Skip to content

Commit

Permalink
Simplify ExistsFilter declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
antograssiot committed Apr 19, 2019
1 parent bc0806b commit 24f574d
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/Bridge/Doctrine/MongoDbOdm/Filter/ExistsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class ExistsFilter extends AbstractFilter implements ExistsFilterInterface
{
use ExistsFilterTrait;

public function __construct(ManagerRegistry $managerRegistry, LoggerInterface $logger = null, string $existsParameterName = self::QUERY_PARAMETER_KEY, array $properties = null)
public function __construct(ManagerRegistry $managerRegistry, LoggerInterface $logger = null, array $properties = null, string $existsParameterName = self::QUERY_PARAMETER_KEY)
{
parent::__construct($managerRegistry, $logger, $properties);

Expand Down
30 changes: 1 addition & 29 deletions src/Bridge/Doctrine/Orm/Filter/ExistsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\ExistsFilterTrait;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryBuilderHelper;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Query\Expr\Join;
Expand All @@ -42,35 +41,8 @@ class ExistsFilter extends AbstractContextAwareFilter implements ExistsFilterInt
{
use ExistsFilterTrait;

/**
* @param RequestStack|null $requestStack No prefix to prevent autowiring of this deprecated property
* @param mixed|null $properties
*/
public function __construct(ManagerRegistry $managerRegistry, $requestStack = null, LoggerInterface $logger = null, /* string $existsParameterName = self::QUERY_PARAMETER_KEY, array*/ $properties = null)
public function __construct(ManagerRegistry $managerRegistry, ?RequestStack $requestStack = null, LoggerInterface $logger = null, array $properties = null, string $existsParameterName = self::QUERY_PARAMETER_KEY)
{
$existsParameterName = self::QUERY_PARAMETER_KEY;

if (($funcNumArgs = \func_num_args()) > 3) {
$fourthArgument = func_get_arg(3);
if (4 <= $funcNumArgs && (null === $fourthArgument || \is_array($fourthArgument))) {
@trigger_error(sprintf('Passing the "$properties" argument as 4th argument of "%s" is deprecated since API Platform 2.5 and will not be possible anymore in API Platform 3. Pass the new "$existsParameterName" argument as 4th argument and the old "$properties" argument as 5th argument instead.', __CLASS__), E_USER_DEPRECATED);
$properties = $fourthArgument;
} elseif (\is_string($fourthArgument)) {
$existsParameterName = $fourthArgument;
} else {
throw new InvalidArgumentException(sprintf('The "$existsParameterName" argument of "%s" is expected to be a string.', __CLASS__));
}
}

if ($funcNumArgs > 4) {
$fifthArgument = func_get_arg(4);
if (null === $fifthArgument || \is_array($fifthArgument)) {
$properties = $fifthArgument;
} else {
throw new InvalidArgumentException(sprintf('The "$properties" argument of "%s" is expected to be an array or null.', __CLASS__));
}
}

parent::__construct($managerRegistry, $requestStack, $logger, $properties);

$this->existsParameterName = $existsParameterName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<service id="api_platform.doctrine_mongodb.odm.exists_filter" class="ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter\ExistsFilter" public="false" abstract="true">
<argument type="service" id="doctrine_mongodb" />
<argument type="service" id="logger" on-invalid="ignore" />
<argument>%api_platform.collection.exists_parameter_name%</argument>
<argument key="$existsParameterName">%api_platform.collection.exists_parameter_name%</argument>
</service>
<service id="ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter\ExistsFilter" alias="api_platform.doctrine_mongodb.odm.exists_filter" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<argument type="service" id="doctrine" />
<argument>null</argument>
<argument type="service" id="logger" on-invalid="ignore" />
<argument>%api_platform.collection.exists_parameter_name%</argument>
<argument key="$existsParameterName">%api_platform.collection.exists_parameter_name%</argument>
</service>
<service id="ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter" alias="api_platform.doctrine.orm.exists_filter" />

Expand Down
8 changes: 4 additions & 4 deletions tests/Bridge/Doctrine/MongoDbOdm/Filter/ExistsFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ public function testGetDescriptionDefaultFields()
public function provideApplyTestData(): array
{
$existsFilterFactory = function (ManagerRegistry $managerRegistry, array $properties = null): ExistsFilter {
return new ExistsFilter($managerRegistry, null, 'exists', $properties);
return new ExistsFilter($managerRegistry, null, $properties, 'exists');
};
$customExistsFilterFactory = function (ManagerRegistry $managerRegistry, array $properties = null): ExistsFilter {
return new ExistsFilter($managerRegistry, null, 'customExists', $properties);
return new ExistsFilter($managerRegistry, null, $properties, 'customExists');
};

return array_merge_recursive(
Expand Down Expand Up @@ -451,7 +451,7 @@ public function testLegacyExistsAfterSyntax()
],
],
function (ManagerRegistry $managerRegistry, array $properties = null): ExistsFilter {
return new ExistsFilter($managerRegistry, null, 'exists', $properties);
return new ExistsFilter($managerRegistry, null, $properties, 'exists');
},
];

Expand All @@ -460,6 +460,6 @@ function (ManagerRegistry $managerRegistry, array $properties = null): ExistsFil

protected function buildFilter(?array $properties = null)
{
return new $this->filterClass($this->managerRegistry, null, 'exists', $properties);
return new $this->filterClass($this->managerRegistry, null, $properties, 'exists');
}
}
10 changes: 5 additions & 5 deletions tests/Bridge/Doctrine/Orm/Filter/ExistsFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ public function testGetDescriptionDefaultFields()
public function provideApplyTestData(): array
{
$existsFilterFactory = function (ManagerRegistry $managerRegistry, array $properties = null, RequestStack $requestStack = null): ExistsFilter {
return new ExistsFilter($managerRegistry, $requestStack, null, 'exists', $properties);
return new ExistsFilter($managerRegistry, $requestStack, null, $properties, 'exists');
};
$customExistsFilterFactory = function (ManagerRegistry $managerRegistry, array $properties = null, RequestStack $requestStack = null): ExistsFilter {
return new ExistsFilter($managerRegistry, $requestStack, null, 'customExists', $properties);
return new ExistsFilter($managerRegistry, $requestStack, null, $properties, 'customExists');
};

return array_merge_recursive(
Expand Down Expand Up @@ -245,7 +245,7 @@ public function testLegacyExistsAfterSyntax()
sprintf('SELECT o FROM %s o WHERE o.description IS NOT NULL', Dummy::class),
null,
function (ManagerRegistry $managerRegistry, array $properties = null, RequestStack $requestStack = null): ExistsFilter {
return new ExistsFilter($managerRegistry, $requestStack, null, 'exists', $properties);
return new ExistsFilter($managerRegistry, $requestStack, null, $properties, 'exists');
},
];

Expand Down Expand Up @@ -273,7 +273,7 @@ public function testLegacyRequest()
sprintf('SELECT o FROM %s o WHERE o.description IS NOT NULL', Dummy::class),
null,
function (ManagerRegistry $managerRegistry, array $properties = null, RequestStack $requestStack = null): ExistsFilter {
return new ExistsFilter($managerRegistry, $requestStack, null, 'exists', $properties);
return new ExistsFilter($managerRegistry, $requestStack, null, $properties, 'exists');
},
];

Expand All @@ -282,6 +282,6 @@ function (ManagerRegistry $managerRegistry, array $properties = null, RequestSta

protected function buildFilter(?array $properties = null)
{
return new $this->filterClass($this->managerRegistry, null, null, 'exists', $properties);
return new $this->filterClass($this->managerRegistry, null, null, $properties, 'exists');
}
}

0 comments on commit 24f574d

Please sign in to comment.