Skip to content

Commit

Permalink
Removed EQUAL REGEX. Uses native implementation now
Browse files Browse the repository at this point in the history
  • Loading branch information
Nil Portugues Caldero committed Apr 27, 2016
1 parent 36926ad commit 088eda9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
16 changes: 10 additions & 6 deletions src/Infrastructure/Model/Repository/MongoDB/MongoDBFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ class MongoDBFilter
const CONTAINS_PATTERN = '/%s/i.test(this.%s)';
const STARTS_WITH_PATTERN = '/^%s/i.test(this.%s)';
const ENDS_WITH_PATTERN = '/%s$/i.test(this.%s)';
const EQUALS_PATTERN = '/^%s$/i.test(this.%s)';

const NOT_CONTAINS_PATTERN = '/^((?!%s.))/i.test(this.%s)';
const NOT_STARTS_WITH_PATTERN = '/^(?!%s).+/i.test(this.%s)';
const NOT_ENDS_WITH_PATTERN = '!(/%s$/i.test(this.%s))';
const NOT_EQUALS_PATTERN = '!(/^%s$/i.test(this.%s))';
const NOT_RANGES_REGEX = '%s < this.%s && %s > this.%s ';

/**
Expand Down Expand Up @@ -207,12 +205,18 @@ protected static function apply(array &$filterArray, array $filters, $conditiona
);
break;
case BaseFilter::EQUALS:
$regex = ('$not' !== $conditional) ? self::EQUALS_PATTERN : self::NOT_EQUALS_PATTERN;
$where[] = sprintf($regex, $value, $key);
if ('$not' !== $conditional) {
$filterArray[$key] = $value;
} else {
$filterArray[$key] = ['$ne' => $value];
}
break;
case BaseFilter::NOT_EQUAL:
$regex = ('$not' !== $conditional) ? self::NOT_EQUALS_PATTERN : self::EQUALS_PATTERN;
$where[] = sprintf($regex, $value, $key);
if ('$not' !== $conditional) {
$filterArray[$key] = ['$ne' => $value];
} else {
$filterArray[$key] = $value;
}
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public function addAll(array $values)
foreach ($values as $value) {
Assert::isInstanceOf($value, Identity::class);
$value = MongoDBTransformer::create()->serialize($value);

if (self::MONGODB_OBJECT_ID === $this->primaryKey) {
$id = new ObjectID(
(!empty($value[self::MONGODB_OBJECT_ID])) ? $value[self::MONGODB_OBJECT_ID] : null
Expand All @@ -293,7 +293,7 @@ public function addAll(array $values)
$documents[][BulkWrite::UPDATE_ONE] = [[$this->primaryKey => $id], ['$set' => $value]];
}
}

if (empty($documents)) {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ public function testFindByWithMustNotEqualTest()
$filter->must()->notEqual('name', 'Ken Sugimori');

$fields = new Fields(['name']);

$results = $this->repository->findBy($filter, null, $fields);

$this->assertEquals(3, count($results));
Expand Down

0 comments on commit 088eda9

Please sign in to comment.