Skip to content

Commit

Permalink
Replace deprecated constants with the new ones
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu authored and jordisala1991 committed Jul 16, 2020
1 parent a118f31 commit 6a0979b
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 62 deletions.
35 changes: 18 additions & 17 deletions src/Filter/AbstractDateFilter.php
Expand Up @@ -18,6 +18,7 @@
use Sonata\AdminBundle\Form\Type\Filter\DateTimeRangeType;
use Sonata\AdminBundle\Form\Type\Filter\DateTimeType;
use Sonata\AdminBundle\Form\Type\Filter\DateType;
use Sonata\AdminBundle\Form\Type\Operator\DateOperatorType;

abstract class AbstractDateFilter extends Filter
{
Expand Down Expand Up @@ -46,20 +47,20 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
}

//default type for simple filter
$data['type'] = !isset($data['type']) || !is_numeric($data['type']) ? DateType::TYPE_EQUAL : $data['type'];
$data['type'] = !isset($data['type']) || !is_numeric($data['type']) ? DateOperatorType::TYPE_EQUAL : $data['type'];

// Some types do not require a value to be set (NULL, NOT NULL).
if (!$this->typeRequiresValue($data['type']) && !$data['value']) {
return;
}

switch ($data['type']) {
case DateType::TYPE_EQUAL:
case DateOperatorType::TYPE_EQUAL:
$this->applyTypeIsEqual($queryBuilder, $field, $data);

return;

case DateType::TYPE_GREATER_THAN:
case DateOperatorType::TYPE_GREATER_THAN:
if (!\array_key_exists('value', $data) || !$data['value']) {
return;
}
Expand All @@ -68,7 +69,7 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)

return;

case DateType::TYPE_LESS_EQUAL:
case DateOperatorType::TYPE_LESS_EQUAL:
if (!\array_key_exists('value', $data) || !$data['value']) {
return;
}
Expand All @@ -77,14 +78,14 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)

return;

case DateType::TYPE_NULL:
case DateType::TYPE_NOT_NULL:
case DateOperatorType::TYPE_NULL:
case DateOperatorType::TYPE_NOT_NULL:
$this->applyType($queryBuilder, $this->getOperator($data['type']), $field, null);

return;

case DateType::TYPE_GREATER_EQUAL:
case DateType::TYPE_LESS_THAN:
case DateOperatorType::TYPE_GREATER_EQUAL:
case DateOperatorType::TYPE_LESS_THAN:
$this->applyType($queryBuilder, $this->getOperator($data['type']), $field, $data['value']);
}
}
Expand Down Expand Up @@ -140,8 +141,8 @@ protected function applyType(ProxyQueryInterface $queryBuilder, $operation, $fie
protected function typeRequiresValue($type)
{
return \in_array($type, [
DateType::TYPE_NULL,
DateType::TYPE_NOT_NULL,
DateOperatorType::TYPE_NULL,
DateOperatorType::TYPE_NOT_NULL,
], true);
}

Expand All @@ -155,13 +156,13 @@ protected function typeRequiresValue($type)
protected function getOperator($type)
{
$choices = [
DateType::TYPE_NULL => 'equals',
DateType::TYPE_NOT_NULL => 'notEqual',
DateType::TYPE_EQUAL => 'equals',
DateType::TYPE_GREATER_EQUAL => 'gte',
DateType::TYPE_GREATER_THAN => 'gt',
DateType::TYPE_LESS_EQUAL => 'lte',
DateType::TYPE_LESS_THAN => 'lt',
DateOperatorType::TYPE_NULL => 'equals',
DateOperatorType::TYPE_NOT_NULL => 'notEqual',
DateOperatorType::TYPE_EQUAL => 'equals',
DateOperatorType::TYPE_GREATER_EQUAL => 'gte',
DateOperatorType::TYPE_GREATER_THAN => 'gt',
DateOperatorType::TYPE_LESS_EQUAL => 'lte',
DateOperatorType::TYPE_LESS_THAN => 'lt',
];

return $choices[(int) $type];
Expand Down
8 changes: 4 additions & 4 deletions src/Filter/ChoiceFilter.php
Expand Up @@ -16,7 +16,7 @@
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
use Sonata\AdminBundle\Form\Type\Filter\DefaultType;
use Sonata\Form\Type\BooleanType;
use Sonata\AdminBundle\Form\Type\Operator\ContainsOperatorType;

class ChoiceFilter extends Filter
{
Expand All @@ -40,7 +40,7 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
return;
}

if (ChoiceType::TYPE_NOT_CONTAINS === $data['type']) {
if (ContainsOperatorType::TYPE_NOT_CONTAINS === $data['type']) {
$queryBuilder->field($field)->notIn($data['value']);
} else {
$queryBuilder->field($field)->in($data['value']);
Expand All @@ -52,7 +52,7 @@ public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data)
return;
}

if (ChoiceType::TYPE_NOT_CONTAINS === $data['type']) {
if (ContainsOperatorType::TYPE_NOT_CONTAINS === $data['type']) {
$queryBuilder->field($field)->notEqual($data['value']);
} else {
$queryBuilder->field($field)->equals($data['value']);
Expand All @@ -73,7 +73,7 @@ public function getDefaultOptions()
public function getRenderSettings()
{
return [DefaultType::class, [
'operator_type' => BooleanType::class,
'operator_type' => ChoiceType::class,
'field_type' => $this->getFieldType(),
'field_options' => $this->getFieldOptions(),
'label' => $this->getLabel(),
Expand Down
8 changes: 4 additions & 4 deletions src/Filter/ModelFilter.php
Expand Up @@ -20,7 +20,7 @@
use MongoDB\Exception\InvalidArgumentException;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Form\Type\Filter\DefaultType;
use Sonata\Form\Type\EqualType;
use Sonata\AdminBundle\Form\Type\Operator\EqualOperatorType;

class ModelFilter extends Filter
{
Expand Down Expand Up @@ -55,7 +55,7 @@ public function getDefaultOptions()
'field_name' => false,
'field_type' => DocumentType::class,
'field_options' => [],
'operator_type' => EqualType::class,
'operator_type' => EqualOperatorType::class,
'operator_options' => [],
];
}
Expand Down Expand Up @@ -89,7 +89,7 @@ protected function handleMultiple(ProxyQueryInterface $queryBuilder, $alias, $fi
$ids[] = self::fixIdentifier($value->getId());
}

if (isset($data['type']) && EqualType::TYPE_IS_NOT_EQUAL === $data['type']) {
if (isset($data['type']) && EqualOperatorType::TYPE_NOT_EQUAL === $data['type']) {
$queryBuilder->field($field)->notIn($ids);
} else {
$queryBuilder->field($field)->in($ids);
Expand All @@ -113,7 +113,7 @@ protected function handleScalar(ProxyQueryInterface $queryBuilder, $alias, $fiel

$id = self::fixIdentifier($data['value']->getId());

if (isset($data['type']) && EqualType::TYPE_IS_NOT_EQUAL === $data['type']) {
if (isset($data['type']) && EqualOperatorType::TYPE_NOT_EQUAL === $data['type']) {
$queryBuilder->field($field)->notEqual($id);
} else {
$queryBuilder->field($field)->equals($id);
Expand Down
11 changes: 6 additions & 5 deletions src/Filter/NumberFilter.php
Expand Up @@ -15,6 +15,7 @@

use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Form\Type\Filter\NumberType;
use Sonata\AdminBundle\Form\Type\Operator\NumberOperatorType;

class NumberFilter extends Filter
{
Expand Down Expand Up @@ -66,11 +67,11 @@ public function getRenderSettings()
private function getOperator($type)
{
$choices = [
NumberType::TYPE_EQUAL => 'equals',
NumberType::TYPE_GREATER_EQUAL => 'gte',
NumberType::TYPE_GREATER_THAN => 'gt',
NumberType::TYPE_LESS_EQUAL => 'lte',
NumberType::TYPE_LESS_THAN => 'lt',
NumberOperatorType::TYPE_EQUAL => 'equals',
NumberOperatorType::TYPE_GREATER_EQUAL => 'gte',
NumberOperatorType::TYPE_GREATER_THAN => 'gt',
NumberOperatorType::TYPE_LESS_EQUAL => 'lte',
NumberOperatorType::TYPE_LESS_THAN => 'lt',
];

return $choices[$type] ?? false;
Expand Down
9 changes: 5 additions & 4 deletions src/Filter/StringFilter.php
Expand Up @@ -16,6 +16,7 @@
use MongoDB\BSON\Regex;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
use Sonata\AdminBundle\Form\Type\Operator\ContainsOperatorType;

class StringFilter extends Filter
{
Expand All @@ -35,18 +36,18 @@ public function filter(ProxyQueryInterface $queryBuilder, $name, $field, $data)
return;
}

$data['type'] = isset($data['type']) && !empty($data['type']) ? $data['type'] : ChoiceType::TYPE_CONTAINS;
$data['type'] = isset($data['type']) && !empty($data['type']) ? $data['type'] : ContainsOperatorType::TYPE_CONTAINS;

$obj = $queryBuilder;
if (self::CONDITION_OR === $this->condition) {
$obj = $queryBuilder->expr();
}

if (ChoiceType::TYPE_EQUAL === $data['type']) {
if (ContainsOperatorType::TYPE_EQUAL === $data['type']) {
$obj->field($field)->equals($data['value']);
} elseif (ChoiceType::TYPE_CONTAINS === $data['type']) {
} elseif (ContainsOperatorType::TYPE_CONTAINS === $data['type']) {
$obj->field($field)->equals($this->getRegexExpression($data['value']));
} elseif (ChoiceType::TYPE_NOT_CONTAINS === $data['type']) {
} elseif (ContainsOperatorType::TYPE_NOT_CONTAINS === $data['type']) {
$obj->field($field)->not($this->getRegexExpression($data['value']));
}

Expand Down
6 changes: 3 additions & 3 deletions src/Guesser/FilterTypeGuesser.php
Expand Up @@ -16,6 +16,7 @@
use Doctrine\Bundle\MongoDBBundle\Form\Type\DocumentType;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Types\Type;
use Sonata\AdminBundle\Form\Type\Operator\EqualOperatorType;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Sonata\DoctrineMongoDBAdminBundle\Filter\BooleanFilter;
use Sonata\DoctrineMongoDBAdminBundle\Filter\DateFilter;
Expand All @@ -25,7 +26,6 @@
use Sonata\DoctrineMongoDBAdminBundle\Filter\StringFilter;
use Sonata\DoctrineMongoDBAdminBundle\Model\MissingPropertyMetadataException;
use Sonata\Form\Type\BooleanType;
use Sonata\Form\Type\EqualType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
Expand All @@ -50,7 +50,7 @@ public function guessType($class, $property, ModelManagerInterface $modelManager
'options' => [],
];

list($metadata, $propertyName, $parentAssociationMappings) = $ret;
[$metadata, $propertyName, $parentAssociationMappings] = $ret;

$options['parent_association_mappings'] = $parentAssociationMappings;

Expand All @@ -60,7 +60,7 @@ public function guessType($class, $property, ModelManagerInterface $modelManager
switch ($mapping['type']) {
case ClassMetadata::ONE:
case ClassMetadata::MANY:
$options['operator_type'] = EqualType::class;
$options['operator_type'] = EqualOperatorType::class;
$options['operator_options'] = [];

$options['field_type'] = DocumentType::class;
Expand Down
8 changes: 4 additions & 4 deletions tests/Filter/ChoiceFilterTest.php
Expand Up @@ -13,7 +13,7 @@

namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Filter;

use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
use Sonata\AdminBundle\Form\Type\Operator\ContainsOperatorType;
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
use Sonata\DoctrineMongoDBAdminBundle\Filter\ChoiceFilter;

Expand Down Expand Up @@ -51,7 +51,7 @@ public function testFilterArray(): void
->with(['1', '2'])
;

$filter->filter($builder, 'alias', 'field', ['type' => ChoiceType::TYPE_CONTAINS, 'value' => ['1', '2']]);
$filter->filter($builder, 'alias', 'field', ['type' => ContainsOperatorType::TYPE_CONTAINS, 'value' => ['1', '2']]);

$this->assertTrue($filter->isActive());
}
Expand All @@ -69,7 +69,7 @@ public function testFilterScalar(): void
->with('1')
;

$filter->filter($builder, 'alias', 'field', ['type' => ChoiceType::TYPE_CONTAINS, 'value' => '1']);
$filter->filter($builder, 'alias', 'field', ['type' => ContainsOperatorType::TYPE_CONTAINS, 'value' => '1']);

$this->assertTrue($filter->isActive());
}
Expand All @@ -87,7 +87,7 @@ public function testFilterZero(): void
->with('0')
;

$filter->filter($builder, 'alias', 'field', ['type' => ChoiceType::TYPE_CONTAINS, 'value' => 0]);
$filter->filter($builder, 'alias', 'field', ['type' => ContainsOperatorType::TYPE_CONTAINS, 'value' => 0]);

$this->assertTrue($filter->isActive());
}
Expand Down
12 changes: 6 additions & 6 deletions tests/Filter/ModelFilterTest.php
Expand Up @@ -18,9 +18,9 @@
use MongoDB\BSON\ObjectId;
use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
use Sonata\AdminBundle\Form\Type\Operator\EqualOperatorType;
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
use Sonata\DoctrineMongoDBAdminBundle\Filter\ModelFilter;
use Sonata\Form\Type\EqualType;

class DocumentStub
{
Expand Down Expand Up @@ -101,7 +101,7 @@ public function testFilterArray(): void
;

$filter->filter($builder, 'alias', 'field', [
'type' => EqualType::TYPE_IS_EQUAL,
'type' => EqualOperatorType::TYPE_EQUAL,
'value' => [$oneDocument, $otherDocument],
]);

Expand Down Expand Up @@ -130,7 +130,7 @@ public function testFilterScalar(): void
->with($this->getMongoIdentifier($document1->getId()))
;

$filter->filter($builder, 'alias', 'field', ['type' => EqualType::TYPE_IS_EQUAL, 'value' => $document1]);
$filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => $document1]);

$this->assertTrue($filter->isActive());
}
Expand Down Expand Up @@ -179,7 +179,7 @@ public function testAssociationWithValidMapping(): void
->willReturnSelf()
;

$filter->apply($builder, ['type' => EqualType::TYPE_IS_EQUAL, 'value' => new DocumentStub()]);
$filter->apply($builder, ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => new DocumentStub()]);

$this->assertTrue($filter->isActive());
}
Expand Down Expand Up @@ -211,7 +211,7 @@ public function testAssociationWithValidParentAssociationMappings(): void
->willReturnSelf()
;

$filter->apply($builder, ['type' => EqualType::TYPE_IS_EQUAL, 'value' => new DocumentStub()]);
$filter->apply($builder, ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => new DocumentStub()]);

$this->assertTrue($filter->isActive());
}
Expand All @@ -238,7 +238,7 @@ public function testDifferentIdentifiersBasedOnMapping(string $storeAs, string $
->willReturnSelf()
;

$filter->apply($builder, ['type' => EqualType::TYPE_IS_EQUAL, 'value' => new DocumentStub()]);
$filter->apply($builder, ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => new DocumentStub()]);

$this->assertTrue($filter->isActive());
}
Expand Down
12 changes: 6 additions & 6 deletions tests/Filter/NumberFilterTest.php
Expand Up @@ -13,7 +13,7 @@

namespace Sonata\DoctrineMongoDBAdminBundle\Tests\Filter;

use Sonata\AdminBundle\Form\Type\Filter\NumberType;
use Sonata\AdminBundle\Form\Type\Operator\NumberOperatorType;
use Sonata\DoctrineMongoDBAdminBundle\Datagrid\ProxyQuery;
use Sonata\DoctrineMongoDBAdminBundle\Filter\NumberFilter;

Expand Down Expand Up @@ -78,11 +78,11 @@ public function testFilter(array $data, string $method): void
public function getNumberExamples(): array
{
return [
[['type' => NumberType::TYPE_EQUAL, 'value' => 42], 'equals'],
[['type' => NumberType::TYPE_GREATER_EQUAL, 'value' => 42], 'gte'],
[['type' => NumberType::TYPE_GREATER_THAN, 'value' => 42], 'gt'],
[['type' => NumberType::TYPE_LESS_EQUAL, 'value' => 42], 'lte'],
[['type' => NumberType::TYPE_LESS_THAN, 'value' => 42], 'lt'],
[['type' => NumberOperatorType::TYPE_EQUAL, 'value' => 42], 'equals'],
[['type' => NumberOperatorType::TYPE_GREATER_EQUAL, 'value' => 42], 'gte'],
[['type' => NumberOperatorType::TYPE_GREATER_THAN, 'value' => 42], 'gt'],
[['type' => NumberOperatorType::TYPE_LESS_EQUAL, 'value' => 42], 'lte'],
[['type' => NumberOperatorType::TYPE_LESS_THAN, 'value' => 42], 'lt'],
[['value' => 42], 'equals'],
];
}
Expand Down

0 comments on commit 6a0979b

Please sign in to comment.