Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ The package uses a model observer to update the index when models change. If you
Add the ``Indexable`` trait to the model you want to have indexed and define the columns you'd like to index as title and content.

#### Example

```php
class Country extends Model
class Country extends Model implements \Swis\Laravel\Fulltext\Contracts\Indexable
{

use \Swis\Laravel\Fulltext\Indexable;
use \Swis\Laravel\Fulltext\Concerns\HasIndexation;

protected $indexContentColumns = ['biographies.name', 'political_situation', 'elections'];
protected $indexTitleColumns = ['name', 'governmental_type'];
Expand Down Expand Up @@ -81,6 +82,10 @@ Choose the database connection to use, defaults to the default database connecti

Results on ``title`` or ``content`` are weighted in the results. Search result score is multiplied by the weight in this config.

### limit_results

Limit the amount of results returned after searching. Use `0` for no limit.

### enable_wildcards

Enable wildcard after words. So when searching for example ``car`` it will also match ``carbon``.
Expand Down
17 changes: 14 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@
"illuminate/support": "^10.0|^11.0"
},
"require-dev": {
"larastan/larastan": "^2.9",
"laravel/pint": "^1.15",
"mockery/mockery": "^1.5",
"phpunit/phpunit": "^9.5"
"orchestra/testbench": "^9.0.0",
"pestphp/pest": "^3.5",
"pestphp/pest-plugin-laravel": "^3.0",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan-deprecation-rules": "^1.2",
"phpstan/phpstan-phpunit": "^1.4"
},
"autoload": {
"psr-4": {
Expand All @@ -41,7 +47,8 @@
"scripts": {
"test": "phpunit",
"check-style": "pint --test",
"fix-style": "pint"
"fix-style": "pint",
"analyse": "phpstan analyse"
},
"extra": {
"branch-alias": {
Expand All @@ -54,7 +61,11 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"phpstan/extension-installer": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
7 changes: 7 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

parameters:
paths:
- src/

# Level 10 is the highest level
level: max
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</filter>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
14 changes: 11 additions & 3 deletions src/Commands/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Swis\Laravel\Fulltext\Commands;

use Illuminate\Console\Command;
use InvalidArgumentException;
use Swis\Laravel\Fulltext\Indexer;

class Index extends Command
Expand All @@ -19,9 +20,16 @@ class Index extends Command
/**
* Execute the console command.
*/
public function handle()
public function handle(): int
{
$indexer = new Indexer();
$indexer->indexAllByClass($this->argument('model_class'));
$modelClass = $this->argument('model_class');
if (! is_string($modelClass)) {
throw new InvalidArgumentException('Model class must be a string');
}

$indexer = new Indexer;
$indexer->indexAllByClass($modelClass);

return self::SUCCESS;
}
}
19 changes: 16 additions & 3 deletions src/Commands/IndexOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Swis\Laravel\Fulltext\Commands;

use Illuminate\Console\Command;
use InvalidArgumentException;
use Swis\Laravel\Fulltext\Indexer;

class IndexOne extends Command
Expand All @@ -19,9 +20,21 @@ class IndexOne extends Command
/**
* Execute the console command.
*/
public function handle()
public function handle(): int
{
$indexer = new Indexer();
$indexer->indexOneByClass($this->argument('model_class'), $this->argument('id'));
$modelClass = $this->argument('model_class');
if (! is_string($modelClass)) {
throw new InvalidArgumentException('Model class must be a string');
}

$id = $this->argument('id');
if (! is_string($id)) {
throw new InvalidArgumentException('ID must be a string or an integer');
}

$indexer = new Indexer;
$indexer->indexOneByClass($modelClass, $id);

return self::SUCCESS;
}
}
19 changes: 16 additions & 3 deletions src/Commands/UnindexOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Swis\Laravel\Fulltext\Commands;

use Illuminate\Console\Command;
use InvalidArgumentException;
use Swis\Laravel\Fulltext\Indexer;

class UnindexOne extends Command
Expand All @@ -19,9 +20,21 @@ class UnindexOne extends Command
/**
* Execute the console command.
*/
public function handle()
public function handle(): int
{
$indexer = new Indexer();
$indexer->unIndexOneByClass($this->argument('model_class'), $this->argument('id'));
$modelClass = $this->argument('model_class');
if (! is_string($modelClass)) {
throw new InvalidArgumentException('Model class must be a string');
}

$id = $this->argument('id');
if (! is_string($id)) {
throw new InvalidArgumentException('ID must be a string or an integer');
}

$indexer = new Indexer;
$indexer->unIndexOneByClass($modelClass, $id);

return self::SUCCESS;
}
}
33 changes: 20 additions & 13 deletions src/Indexable.php → src/Concerns/HasIndexation.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,59 @@
<?php

namespace Swis\Laravel\Fulltext;
namespace Swis\Laravel\Fulltext\Concerns;

use Illuminate\Database\Eloquent\Relations\MorphOne;
use Swis\Laravel\Fulltext\IndexedRecord;
use Swis\Laravel\Fulltext\ModelObserver;

/**
* @property IndexedRecord|null $indexedRecord
*/
trait Indexable
trait HasIndexation
{
/**
* Boot the trait.
*/
public static function bootIndexable()
public static function bootIndexable(): void
{
static::observe(new ModelObserver());
static::observe(new ModelObserver);
}

public function getIndexContent()
public function getIndexContent(): string
{
return $this->getIndexDataFromColumns($this->indexContentColumns);
return $this->getIndexDataFromColumns($this->indexContentColumns ?? []);
}

public function getIndexTitle()
public function getIndexTitle(): string
{
return $this->getIndexDataFromColumns($this->indexTitleColumns);
return $this->getIndexDataFromColumns($this->indexTitleColumns ?? []);
}

public function indexedRecord()
/**
* @return MorphOne<IndexedRecord, \Swis\Laravel\Fulltext\Contracts\Indexable>
*/
public function indexedRecord(): MorphOne
{
return $this->morphOne(IndexedRecord::class, 'indexable');
}

public function indexRecord()
public function indexRecord(): void
{
if ($this->indexedRecord === null) {
$this->indexedRecord = new IndexedRecord();
$this->indexedRecord = new IndexedRecord;
$this->indexedRecord->indexable()->associate($this);
}
$this->indexedRecord->updateIndex();
}

public function unIndexRecord()
public function unIndexRecord(): void
{
if ($this->indexedRecord !== null) {
$this->indexedRecord->delete();
}
}

protected function getIndexDataFromColumns($columns)
protected function getIndexDataFromColumns($columns): string
{
$indexData = [];
foreach ($columns as $column) {
Expand Down
23 changes: 23 additions & 0 deletions src/Contracts/Indexable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Swis\Laravel\Fulltext\Contracts;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Swis\Laravel\Fulltext\IndexedRecord;

interface Indexable
{
public function getIndexContent(): string;

public function getIndexTitle(): string;

public function indexRecord(): void;

public function unIndexRecord(): void;

/**
* @return MorphOne<IndexedRecord, Model>
*/
public function indexedRecord(): MorphOne;
}
11 changes: 7 additions & 4 deletions src/FulltextServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

class FulltextServiceProvider extends ServiceProvider
{
protected $commands = [
/**
* @var array<string>
*/
protected array $commands = [
Index::class,
IndexOne::class,
UnindexOne::class,
Expand All @@ -18,7 +21,7 @@ class FulltextServiceProvider extends ServiceProvider
/**
* Register the service provider.
*/
public function register()
public function register(): void
{
$this->mergeConfigFrom(
__DIR__.'/../config/laravel-fulltext.php',
Expand All @@ -28,14 +31,14 @@ public function register()
if ($this->app->runningInConsole()) {
$this->publishes(
[
__DIR__.'/../config/laravel-fulltext.php' => config_path('laravel-fulltext.php'),
__DIR__.'/../config/laravel-fulltext.php' => $this->app->configPath('laravel-fulltext.php'),
],
'laravel-fulltext'
);

$this->publishes(
[
__DIR__.'/../database/migrations' => database_path('migrations'),
__DIR__.'/../database/migrations' => $this->app->databasePath('migrations'),
],
'laravel-fulltext'
);
Expand Down
24 changes: 21 additions & 3 deletions src/IndexedRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,42 @@
namespace Swis\Laravel\Fulltext;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\Config;
use Swis\Laravel\Fulltext\Contracts\Indexable;

/**
* @property Indexable $indexable
*/
class IndexedRecord extends Model
{
protected $table = 'laravel_fulltext';

/**
* @param array<array-key, mixed> $attributes
*/
public function __construct(array $attributes = [])
{
$this->connection = config('laravel-fulltext.db_connection');
$connection = Config::get('laravel-fulltext.db_connection');

if (! is_string($connection)) {
$connection = null;
}

$this->connection = $connection;

parent::__construct($attributes);
}

public function indexable()
/**
* @return MorphTo<Model, $this>
*/
public function indexable(): MorphTo
{
return $this->morphTo();
}

public function updateIndex()
public function updateIndex(): void
{
$this->setAttribute('indexed_title', $this->indexable->getIndexTitle());
$this->setAttribute('indexed_content', $this->indexable->getIndexContent());
Expand Down
Loading