Skip to content

Commit

Permalink
Remove use of compact function
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Dec 27, 2024
1 parent 7bf4f2d commit 74d3005
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
8 changes: 1 addition & 7 deletions src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace MongoDB\Laravel\Eloquent;

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Support\Collection;
use MongoDB\BSON\Document;
use MongoDB\Driver\CursorInterface;
use MongoDB\Driver\Exception\WriteException;
Expand All @@ -17,7 +16,6 @@
use function array_key_exists;
use function array_merge;
use function collect;
use function compact;
use function is_array;
use function is_object;
use function iterator_to_array;
Expand Down Expand Up @@ -51,6 +49,7 @@ class Builder extends EloquentBuilder
'insertusing',
'max',
'min',
'autocomplete',
'pluck',
'pull',
'push',
Expand Down Expand Up @@ -78,11 +77,6 @@ public function search(...$args)
return $this->model->hydrate($results->all());
}

public function autocomplete(string $path, string $query, bool|array $fuzzy = false, string $tokenOrder = 'any'): Collection
{
return $this->toBase()->autocomplete(...compact('path', 'query', 'fuzzy', 'tokenOrder'));
}

/** @inheritdoc */
public function update(array $values, array $options = [])
{
Expand Down
9 changes: 4 additions & 5 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
use function blank;
use function call_user_func;
use function call_user_func_array;
use function compact;
use function count;
use function ctype_xdigit;
use function date_default_timezone_get;
Expand Down Expand Up @@ -1531,11 +1530,11 @@ public function search(
/** @return Collection<string> */
public function autocomplete(string $path, string $query, bool|array $fuzzy = false, string $tokenOrder = 'any'): Collection
{
$args = compact('path', 'query', 'fuzzy', 'tokenOrder');
if ($args['fuzzy'] === true) {
$args = ['path' => $path, 'query' => $query, 'tokenOrder' => $tokenOrder];
if ($fuzzy === true) {
$args['fuzzy'] = ['maxEdits' => 2];
} elseif ($args['fuzzy'] === false) {
unset($args['fuzzy']);
} elseif ($fuzzy !== false) {
$args['fuzzy'] = $fuzzy;
}

return $this->aggregate()->search(
Expand Down
14 changes: 8 additions & 6 deletions tests/AtlasSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace MongoDB\Laravel\Tests;

use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Collection as LaravelCollection;
use MongoDB\Builder\Search;
use MongoDB\Collection;
use MongoDB\Collection as MongoDBCollection;
use MongoDB\Driver\Exception\ServerException;
use MongoDB\Laravel\Tests\Models\Book;

Expand Down Expand Up @@ -41,7 +43,7 @@ public function setUp(): void
]);

$collection = $this->getConnection('mongodb')->getCollection('books');
assert($collection instanceof Collection);
assert($collection instanceof MongoDBCollection);
try {
$collection->createSearchIndex([
'mappings' => [
Expand Down Expand Up @@ -85,7 +87,7 @@ public function testEloquentBuilderSearch()
{
$results = Book::search(Search::text('title', 'systems'));

self::assertInstanceOf(\Illuminate\Database\Eloquent\Collection::class, $results);
self::assertInstanceOf(EloquentCollection::class, $results);
self::assertCount(3, $results);
self::assertInstanceOf(Book::class, $results->first());
self::assertSame([
Expand All @@ -100,7 +102,7 @@ public function testDatabaseBuilderSearch()
$results = $this->getConnection('mongodb')->table('books')
->search(Search::text('title', 'systems'));

self::assertInstanceOf(\Illuminate\Support\Collection::class, $results);
self::assertInstanceOf(LaravelCollection::class, $results);
self::assertCount(3, $results);
self::assertIsArray($results->first());
self::assertSame([
Expand All @@ -114,7 +116,7 @@ public function testEloquentBuilderAutocomplete()
{
$results = Book::autocomplete('title', 'system');

self::assertInstanceOf(\Illuminate\Support\Collection::class, $results);
self::assertInstanceOf(LaravelCollection::class, $results);
self::assertCount(3, $results);
self::assertSame([
'Operating System Concepts',
Expand All @@ -128,7 +130,7 @@ public function testDatabaseBuilderAutocomplete()
$results = $this->getConnection('mongodb')->table('books')
->autocomplete('title', 'system');

self::assertInstanceOf(\Illuminate\Support\Collection::class, $results);
self::assertInstanceOf(LaravelCollection::class, $results);
self::assertCount(3, $results);
self::assertSame([
'Operating System Concepts',
Expand Down

0 comments on commit 74d3005

Please sign in to comment.