Skip to content

Commit e93864e

Browse files
committed
wip
1 parent 68c363b commit e93864e

22 files changed

+1258
-42
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ProtoneMedia\LaravelCrossEloquentSearch\Contracts;
4+
5+
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
6+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
7+
8+
interface SearchResultContract
9+
{
10+
/**
11+
* Get the collection of search results.
12+
*/
13+
public function items(): EloquentCollection;
14+
15+
/**
16+
* Check if the results are paginated.
17+
*/
18+
public function isPaginated(): bool;
19+
20+
/**
21+
* Get the paginator instance if results are paginated.
22+
*/
23+
public function paginator(): ?LengthAwarePaginator;
24+
25+
/**
26+
* Get the total number of results.
27+
*/
28+
public function total(): int;
29+
30+
/**
31+
* Check if there are any results.
32+
*/
33+
public function isEmpty(): bool;
34+
35+
/**
36+
* Check if there are results.
37+
*/
38+
public function isNotEmpty(): bool;
39+
40+
/**
41+
* Get the number of items in the current page/collection.
42+
*/
43+
public function count(): int;
44+
}

src/Contracts/SearcherContract.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ProtoneMedia\LaravelCrossEloquentSearch\Contracts;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Support\Collection;
7+
8+
interface SearcherContract
9+
{
10+
/**
11+
* Add a model to search through.
12+
*
13+
* @param Builder|class-string $query
14+
* @param string|array|Collection|null $columns
15+
* @param string|null $orderByColumn
16+
*/
17+
public function add($query, $columns = null, string $orderByColumn = null): self;
18+
19+
/**
20+
* Add a full-text searchable model.
21+
*
22+
* @param Builder|class-string $query
23+
* @param string|array|Collection|null $columns
24+
* @param array $options
25+
* @param string|null $orderByColumn
26+
*/
27+
public function addFullText($query, $columns = null, array $options = [], string $orderByColumn = null): self;
28+
29+
/**
30+
* Add multiple models at once.
31+
*
32+
* @param array $queries
33+
*/
34+
public function addMany(array $queries): self;
35+
36+
/**
37+
* Set the order by column for the most recently added model.
38+
*
39+
* @param string $orderByColumn
40+
*/
41+
public function orderBy(string $orderByColumn): self;
42+
43+
/**
44+
* Order results in ascending order.
45+
*/
46+
public function orderByAsc(): self;
47+
48+
/**
49+
* Order results in descending order.
50+
*/
51+
public function orderByDesc(): self;
52+
53+
/**
54+
* Order results by relevance.
55+
*/
56+
public function orderByRelevance(): self;
57+
58+
/**
59+
* Order results by model type.
60+
*
61+
* @param array|string $modelClasses
62+
*/
63+
public function orderByModel($modelClasses): self;
64+
65+
/**
66+
* Configure wildcard behavior.
67+
*/
68+
public function beginWithWildcard(bool $state = true): self;
69+
70+
/**
71+
* Configure wildcard behavior.
72+
*/
73+
public function endWithWildcard(bool $state = true): self;
74+
75+
/**
76+
* Enable case-insensitive searching.
77+
*/
78+
public function ignoreCase(bool $state = true): self;
79+
80+
/**
81+
* Enable sounds like searching.
82+
*/
83+
public function soundsLike(bool $state = true): self;
84+
85+
/**
86+
* Configure term parsing.
87+
*/
88+
public function parseTerm(bool $state = true): self;
89+
90+
/**
91+
* Configure pagination.
92+
*/
93+
public function paginate(int $perPage = 15, string $pageName = 'page', int $page = null): self;
94+
95+
/**
96+
* Configure simple pagination.
97+
*/
98+
public function simplePaginate(int $perPage = 15, string $pageName = 'page', int $page = null): self;
99+
100+
/**
101+
* Include model type in results.
102+
*/
103+
public function includeModelType(string $key = 'type'): self;
104+
105+
/**
106+
* Perform the search.
107+
*
108+
* @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator
109+
*/
110+
public function search(string $terms = null);
111+
112+
/**
113+
* Count the search results.
114+
*/
115+
public function count(string $terms = null): int;
116+
117+
/**
118+
* Parse search terms.
119+
*/
120+
public static function parseTerms(string $terms, callable $callback = null): Collection;
121+
}

src/DatabaseGrammarFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ProtoneMedia\LaravelCrossEloquentSearch;
44

55
use Illuminate\Database\Connection;
6+
use ProtoneMedia\LaravelCrossEloquentSearch\Exceptions\InvalidGrammarException;
67
use ProtoneMedia\LaravelCrossEloquentSearch\Grammars\MySqlSearchGrammar;
78
use ProtoneMedia\LaravelCrossEloquentSearch\Grammars\SQLiteSearchGrammar;
89
use ProtoneMedia\LaravelCrossEloquentSearch\Grammars\SearchGrammarInterface;
@@ -21,7 +22,7 @@ class DatabaseGrammarFactory
2122
*
2223
* @param \Illuminate\Database\Connection $connection
2324
* @return \ProtoneMedia\LaravelCrossEloquentSearch\Grammars\SearchGrammarInterface
24-
* @throws \InvalidArgumentException
25+
* @throws InvalidGrammarException
2526
*/
2627
public static function make(Connection $connection): SearchGrammarInterface
2728
{
@@ -34,7 +35,7 @@ public static function make(Connection $connection): SearchGrammarInterface
3435
case 'sqlite':
3536
return new SQLiteSearchGrammar($connection);
3637
default:
37-
throw new \InvalidArgumentException("Database driver '{$driver}' is not supported for cross-eloquent search.");
38+
throw InvalidGrammarException::driverNotSupported($driver);
3839
}
3940
}
4041
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ProtoneMedia\LaravelCrossEloquentSearch\Exceptions;
4+
5+
use Exception;
6+
7+
abstract class CrossEloquentSearchException extends Exception
8+
{
9+
//
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ProtoneMedia\LaravelCrossEloquentSearch\Exceptions;
4+
5+
class InvalidGrammarException extends CrossEloquentSearchException
6+
{
7+
public static function driverNotSupported(string $driver): self
8+
{
9+
return new self("Database driver '{$driver}' is not supported for cross-eloquent search.");
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ProtoneMedia\LaravelCrossEloquentSearch\Exceptions;
4+
5+
class NoModelsAddedException extends CrossEloquentSearchException
6+
{
7+
public static function make(): self
8+
{
9+
return new self('No models have been added to search through.');
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ProtoneMedia\LaravelCrossEloquentSearch\Exceptions;
4+
5+
class OrderByRelevanceException extends CrossEloquentSearchException
6+
{
7+
public static function relationColumnsNotSupported(): self
8+
{
9+
return new self('Cannot order by relevance when searching through relationship columns.');
10+
}
11+
}

src/OrderByRelevanceException.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)