Skip to content

Commit

Permalink
Drop support for doctrine/mongodb-odm 1.x
Browse files Browse the repository at this point in the history
It uses the deprecated mongodb php extension and 2.0 was released
one year ago.
  • Loading branch information
franmomu authored and phansys committed Sep 23, 2020
1 parent ee41c22 commit 43da623
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 79 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 4 additions & 13 deletions src/Filter/ModelFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
18 changes: 2 additions & 16 deletions src/Filter/StringFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
}
}
4 changes: 1 addition & 3 deletions src/Guesser/FilterTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions src/Guesser/TypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
17 changes: 3 additions & 14 deletions tests/Filter/ModelFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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', [
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
}
}
18 changes: 2 additions & 16 deletions tests/Filter/StringFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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]);
Expand Down Expand Up @@ -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));
}
}
18 changes: 6 additions & 12 deletions tests/Guesser/TypeGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function testGuessTypeNoAssociation(string $type, string $resultType, int

public function noAssociationData(): array
{
$noAssociationData = [
return [
'collection' => [
Type::COLLECTION,
'array',
Expand All @@ -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',
Expand All @@ -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;
}
}

0 comments on commit 43da623

Please sign in to comment.