From 43da623f7a4cfaf027ca3bf1502215dd7f06ef69 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Tue, 22 Sep 2020 15:11:39 +0200 Subject: [PATCH] Drop support for doctrine/mongodb-odm 1.x It uses the deprecated mongodb php extension and 2.0 was released one year ago. --- composer.json | 5 +++-- src/Filter/ModelFilter.php | 17 ++++------------- src/Filter/StringFilter.php | 18 ++---------------- src/Guesser/FilterTypeGuesser.php | 4 +--- src/Guesser/TypeGuesser.php | 4 +--- tests/Filter/ModelFilterTest.php | 17 +++-------------- tests/Filter/StringFilterTest.php | 18 ++---------------- tests/Guesser/TypeGuesserTest.php | 18 ++++++------------ 8 files changed, 22 insertions(+), 79 deletions(-) diff --git a/composer.json b/composer.json index b2e88b92..e134fcaa 100644 --- a/composer.json +++ b/composer.json @@ -26,9 +26,10 @@ ], "require": { "php": "^7.2", + "ext-mongodb": "*", "doctrine/collections": "^1.6", - "doctrine/mongodb-odm": "^1.3 || ^2.1", - "doctrine/mongodb-odm-bundle": "^3.6 || ^4.0", + "doctrine/mongodb-odm": "^2.1", + "doctrine/mongodb-odm-bundle": "^4.0", "doctrine/persistence": "^1.3.4 || ^2.0", "sonata-project/admin-bundle": "^3.75", "sonata-project/form-extensions": "^0.1 || ^1.4", diff --git a/src/Filter/ModelFilter.php b/src/Filter/ModelFilter.php index eb431fb9..ac69dfd4 100644 --- a/src/Filter/ModelFilter.php +++ b/src/Filter/ModelFilter.php @@ -123,26 +123,17 @@ protected function handleScalar(ProxyQueryInterface $queryBuilder, $alias, $fiel } /** - * Return \MongoId|ObjectId if $id is MongoId|ObjectId in string representation, otherwise custom string. + * Return ObjectId if $id is ObjectId in string representation, otherwise custom string. * * @param mixed $id * - * @return \MongoId|string|ObjectId + * @return string|ObjectId */ protected static function fixIdentifier($id) { - // NEXT_MAJOR: Use only ObjectId when dropping support for doctrine/mongodb-odm 1.x - if (class_exists(ObjectId::class)) { - try { - return new ObjectId($id); - } catch (InvalidArgumentException $ex) { - return $id; - } - } - try { - return new \MongoId($id); - } catch (\MongoException $ex) { + return new ObjectId($id); + } catch (InvalidArgumentException $ex) { return $id; } } diff --git a/src/Filter/StringFilter.php b/src/Filter/StringFilter.php index 995da849..11dc9a92 100644 --- a/src/Filter/StringFilter.php +++ b/src/Filter/StringFilter.php @@ -46,9 +46,9 @@ public function filter(ProxyQueryInterface $queryBuilder, $name, $field, $data) if (ContainsOperatorType::TYPE_EQUAL === $data['type']) { $obj->field($field)->equals($data['value']); } elseif (ContainsOperatorType::TYPE_CONTAINS === $data['type']) { - $obj->field($field)->equals($this->getRegexExpression($data['value'])); + $obj->field($field)->equals(new Regex($data['value'], 'i')); } elseif (ContainsOperatorType::TYPE_NOT_CONTAINS === $data['type']) { - $obj->field($field)->not($this->getRegexExpression($data['value'])); + $obj->field($field)->not(new Regex($data['value'], 'i')); } if (self::CONDITION_OR === $this->condition) { @@ -74,18 +74,4 @@ public function getRenderSettings() 'label' => $this->getLabel(), ]]; } - - /** - * NEXT_MAJOR: Use only Regex when dropping support for doctrine/mongodb-odm 1.x. - * - * @return Regex|\MongoRegex - */ - private function getRegexExpression(string $pattern) - { - if (class_exists(Regex::class)) { - return new Regex($pattern, 'i'); - } - - return new \MongoRegex(sprintf('/%s/i', $pattern)); - } } diff --git a/src/Guesser/FilterTypeGuesser.php b/src/Guesser/FilterTypeGuesser.php index c77975f3..6473208c 100644 --- a/src/Guesser/FilterTypeGuesser.php +++ b/src/Guesser/FilterTypeGuesser.php @@ -100,9 +100,7 @@ public function guessType($class, $property, ModelManagerInterface $modelManager return new TypeGuess(DateTimeFilter::class, $options, Guess::HIGH_CONFIDENCE); case Type::DATE: - // NEXT_MAJOR: Use only the constant when dropping support for doctrine/mongodb-odm 1.3. - // case Type::DATE_IMMUTABLE: - case 'date_immutable': + case Type::DATE_IMMUTABLE: $options['field_type'] = DateType::class; return new TypeGuess(DateFilter::class, $options, Guess::HIGH_CONFIDENCE); diff --git a/src/Guesser/TypeGuesser.php b/src/Guesser/TypeGuesser.php index b16af648..a1a5e91d 100755 --- a/src/Guesser/TypeGuesser.php +++ b/src/Guesser/TypeGuesser.php @@ -79,9 +79,7 @@ public function guessType($class, $property, ModelManagerInterface $modelManager case Type::TIMESTAMP: return new TypeGuess('datetime', [], Guess::HIGH_CONFIDENCE); case Type::DATE: - // NEXT_MAJOR: Use only the constant when dropping support for doctrine/mongodb-odm 1.3. - // case Type::DATE_IMMUTABLE: - case 'date_immutable': + case Type::DATE_IMMUTABLE: return new TypeGuess('date', [], Guess::HIGH_CONFIDENCE); case 'decimal': @trigger_error( diff --git a/tests/Filter/ModelFilterTest.php b/tests/Filter/ModelFilterTest.php index 7a87c0e5..c0a9c0fb 100644 --- a/tests/Filter/ModelFilterTest.php +++ b/tests/Filter/ModelFilterTest.php @@ -28,8 +28,7 @@ class DocumentStub public function __construct() { - // NEXT_MAJOR: Use only ObjectId when dropping support for doctrine/mongodb-odm 1.x - $this->id = class_exists(ObjectId::class) ? new ObjectId() : new MongoId(); + $this->id = new ObjectId(); } public function getId() @@ -97,7 +96,7 @@ public function testFilterArray(): void $builder->getQueryBuilder() ->expects($this->once()) ->method('in') - ->with([$this->getMongoIdentifier($oneDocument->getId()), $this->getMongoIdentifier($otherDocument->getId())]) + ->with([new ObjectId($oneDocument->getId()), new ObjectId($otherDocument->getId())]) ; $filter->filter($builder, 'alias', 'field', [ @@ -127,7 +126,7 @@ public function testFilterScalar(): void $builder->getQueryBuilder() ->expects($this->once()) ->method('equals') - ->with($this->getMongoIdentifier($document1->getId())) + ->with(new ObjectId($document1->getId())) ; $filter->filter($builder, 'alias', 'field', ['type' => EqualOperatorType::TYPE_EQUAL, 'value' => $document1]); @@ -252,14 +251,4 @@ public function getMappings(): array [ClassMetadata::REFERENCE_STORE_AS_DB_REF, '.$id'], ]; } - - /** - * NEXT_MAJOR: Use only ObjectId when dropping support for doctrine/mongodb-odm 1.x. - * - * @return ObjectId|\MongoId - */ - private function getMongoIdentifier(string $id) - { - return class_exists(ObjectId::class) ? new ObjectId($id) : new \MongoId($id); - } } diff --git a/tests/Filter/StringFilterTest.php b/tests/Filter/StringFilterTest.php index 387add2f..f88a951b 100644 --- a/tests/Filter/StringFilterTest.php +++ b/tests/Filter/StringFilterTest.php @@ -50,7 +50,7 @@ public function testContains(): void $builder->getQueryBuilder() ->expects($this->exactly(2)) ->method('equals') - ->with($this->getMongoRegex('asd')) + ->with(new Regex('asd', 'i')) ; $filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_CONTAINS]); @@ -68,7 +68,7 @@ public function testNotContains(): void $builder->getQueryBuilder() ->expects($this->once()) ->method('not') - ->with($this->getMongoRegex('asd')) + ->with(new Regex('asd', 'i')) ; $filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_NOT_CONTAINS]); @@ -149,18 +149,4 @@ public function testOr(): void $filter->filter($builder, 'alias', 'field', ['value' => 'asd', 'type' => ContainsOperatorType::TYPE_CONTAINS]); $this->assertTrue($filter->isActive()); } - - /** - * NEXT_MAJOR: Use only Regex when dropping support for doctrine/mongodb-odm 1.x. - * - * @return Regex|\MongoRegex - */ - private function getMongoRegex(string $pattern) - { - if (class_exists(Regex::class)) { - return new Regex($pattern, 'i'); - } - - return new \MongoRegex(sprintf('/%s/i', $pattern)); - } } diff --git a/tests/Guesser/TypeGuesserTest.php b/tests/Guesser/TypeGuesserTest.php index af59d199..6e500492 100644 --- a/tests/Guesser/TypeGuesserTest.php +++ b/tests/Guesser/TypeGuesserTest.php @@ -141,7 +141,7 @@ public function testGuessTypeNoAssociation(string $type, string $resultType, int public function noAssociationData(): array { - $noAssociationData = [ + return [ 'collection' => [ Type::COLLECTION, 'array', @@ -167,6 +167,11 @@ public function noAssociationData(): array 'date', Guess::HIGH_CONFIDENCE, ], + 'date_immutable' => [ + Type::DATE_IMMUTABLE, + 'date', + Guess::HIGH_CONFIDENCE, + ], 'float' => [ Type::FLOAT, 'number', @@ -188,16 +193,5 @@ public function noAssociationData(): array Guess::LOW_CONFIDENCE, ], ]; - - // Remove the check and add the case to the "$noAssociationData" when dropping support of doctrine/mongodb-odm 1.x - if (\defined(sprintf('%s::DATE_IMMUTABLE', Type::class))) { - $noAssociationData['datetime_immutable'] = [ - Type::DATE_IMMUTABLE, - 'date', - Guess::HIGH_CONFIDENCE, - ]; - } - - return $noAssociationData; } }