diff --git a/src/Engines/ScoutPhpmorphyEngine.php b/src/Engines/ScoutPhpmorphyEngine.php index 98903e6..544cde5 100755 --- a/src/Engines/ScoutPhpmorphyEngine.php +++ b/src/Engines/ScoutPhpmorphyEngine.php @@ -2,31 +2,27 @@ namespace VI\ScoutPhpmorphy\Engines; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Facades\DB; use Laravel\Scout\Builder; use Laravel\Scout\Engines\Engine; -use Illuminate\Database\Eloquent\Collection; use VI\ScoutPhpmorphy\Models\Index; use VI\ScoutPhpmorphy\Models\Word; -use VI\ScoutPhpmorphy\Services\ArrayService; -use VI\ScoutPhpmorphy\Services\StringService; use VI\ScoutPhpmorphy\Services\WordsService; class ScoutPhpmorphyEngine extends Engine { - public function update($models): Collection { - $models->each(function ($model) use (&$params) { $words = WordsService::prepareArray($model->toSearchableArray()); $words = Word::getOrCreateItems($words); foreach ($words as $word) { Index::firstOrCreate([ - 'index' => $model->searchableAs(), - 'key' => $model->getScoutKey(), - 'word_id' => $word->id, + 'index' => $model->searchableAs(), + 'key' => $model->getScoutKey(), + 'word_id' => $word->id, 'count_words' => $word->count_words, ]); } @@ -47,7 +43,6 @@ public function delete($models) public function search(Builder $builder) { - $index = $this->buildIndexQuery($builder) ->selectRaw('`index`, `key`, COUNT(*) AS `count_lines`, SUM(`count_words`) AS `sum_count_words`') ->groupBy('index', 'key') @@ -55,7 +50,6 @@ public function search(Builder $builder) ->get(); return $index; - } public function paginate(Builder $builder, $perPage, $page) @@ -72,12 +66,10 @@ public function paginate(Builder $builder, $perPage, $page) public function mapIds($results) { dd(__METHOD__); - } public function map(Builder $builder, $results, $model) { - dd($builder); if ($results->isEmpty()) { @@ -91,14 +83,11 @@ public function map(Builder $builder, $results, $model) )->get()->keyBy($model->getKeyName()); return $models; - } public function getTotalCount($results) { - return $results->total(); - } public function flush($model) @@ -134,5 +123,4 @@ protected function buildIndexQuery(Builder $builder) return $query; } - } diff --git a/src/Models/Index.php b/src/Models/Index.php index d68dd83..e42f4eb 100644 --- a/src/Models/Index.php +++ b/src/Models/Index.php @@ -2,19 +2,16 @@ namespace VI\ScoutPhpmorphy\Models; -use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; -use Illuminate\Database\Eloquent\Relations\HasMany; class Index extends Model { - public $timestamps = false; public function getTable() { - return config('scout-phpmorphy.table_prefix') . 'index'; + return config('scout-phpmorphy.table_prefix').'index'; } protected $fillable = [ @@ -24,10 +21,8 @@ public function getTable() 'count_words', ]; - public function word(): BelongsTo { return $this->belongsTo(Word::class); } - } diff --git a/src/Models/Word.php b/src/Models/Word.php index 36728f1..a8e88ba 100644 --- a/src/Models/Word.php +++ b/src/Models/Word.php @@ -9,30 +9,28 @@ class Word extends Model { - public $timestamps = false; + public int $count_words = 0; + public bool $is_dictionary = false; public function getTable() { - return config('scout-phpmorphy.table_prefix') . 'words'; + return config('scout-phpmorphy.table_prefix').'words'; } protected $fillable = [ 'word', ]; - public function index(): HasMany { return $this->hasMany(Index::class); } - public static function getOrCreateItems($array): Collection { - $items = new Collection; foreach ($array as $wordItem) { @@ -43,31 +41,27 @@ public static function getOrCreateItems($array): Collection } return $items; - } - public static function getItems($array): Collection { - $items = new Collection; - if (!empty($array)) { + if (! empty($array)) { $items = self::query() - ->when(array_filter($array, fn($word) => $word['is_dictionary']), - fn(Builder $query, $words) => $query->whereIn('word', collect($words)->pluck('word'))) - ->when(array_filter($array, fn($word) => !$word['is_dictionary']), + ->when(array_filter($array, fn ($word) => $word['is_dictionary']), + fn (Builder $query, $words) => $query->whereIn('word', collect($words)->pluck('word'))) + ->when(array_filter($array, fn ($word) => ! $word['is_dictionary']), function (Builder $query, $words) { foreach ($words as $word) { - $query->orWhere('word', 'LIKE', $word['word'] . '%'); + $query->orWhere('word', 'LIKE', $word['word'].'%'); } + return $query; }) ->get(); } return $items; - } - } diff --git a/src/ScoutPhpmorphyServiceProvider.php b/src/ScoutPhpmorphyServiceProvider.php index 6e60911..9631450 100644 --- a/src/ScoutPhpmorphyServiceProvider.php +++ b/src/ScoutPhpmorphyServiceProvider.php @@ -11,7 +11,6 @@ class ScoutPhpmorphyServiceProvider extends PackageServiceProvider { public function configurePackage(Package $package): void { - /* * This class is a Package Service Provider * @@ -21,7 +20,6 @@ public function configurePackage(Package $package): void ->name('scout-phpmorphy') ->hasConfigFile() ->hasMigrations(['create_phpmorphy_words_table', 'create_phpmorphy_index_table']); - } public function boot() diff --git a/src/Services/ArrayService.php b/src/Services/ArrayService.php index c84dd31..771187c 100644 --- a/src/Services/ArrayService.php +++ b/src/Services/ArrayService.php @@ -6,14 +6,13 @@ class ArrayService { public static function flatten($array): array { - $result = []; foreach ($array as $value) { if (is_scalar($value)) { $result[] = $value; } else { - foreach (self::flatten((array)$value) as $subValue) { + foreach (self::flatten((array) $value) as $subValue) { $result[] = $subValue; } } diff --git a/src/Services/StringService.php b/src/Services/StringService.php index 96cb57e..8473ea0 100644 --- a/src/Services/StringService.php +++ b/src/Services/StringService.php @@ -4,10 +4,10 @@ class StringService { - public static function toArray(string $string) { $string = strip_tags($string); + return preg_split('/((^\p{P}+)|(\p{P}*\s+\p{P}*)|(\p{P}+$))/', $string); } } diff --git a/src/Services/WordsService.php b/src/Services/WordsService.php index 6679b6b..98af101 100644 --- a/src/Services/WordsService.php +++ b/src/Services/WordsService.php @@ -2,11 +2,8 @@ namespace VI\ScoutPhpmorphy\Services; -use cijic\phpMorphy\Facade\Morphy; - class WordsService { - public static function prepareString(string $string) { $words = StringService::toArray($string); @@ -28,9 +25,9 @@ protected static function prepare(array $array) }); $words = ArrayService::flatten($words); $words = collect($words) - ->map(fn($word) => str($word)->upper()->trim()->toString()) + ->map(fn ($word) => str($word)->upper()->trim()->toString()) ->filter() - ->filter(fn($word) => str($word)->length() >= 2); + ->filter(fn ($word) => str($word)->length() >= 2); $morphy = new \cijic\phpMorphy\Morphy(); @@ -38,7 +35,7 @@ protected static function prepare(array $array) $array = [ [ 'is_dictionary' => false, - 'word' => $word, + 'word' => $word, ], ]; if ($morphyWord = $morphy->getPseudoRoot($word)) { @@ -46,10 +43,11 @@ protected static function prepare(array $array) foreach ($morphyWord as $morphyWordNew) { $array[] = [ 'is_dictionary' => true, - 'word' => $morphyWordNew, + 'word' => $morphyWordNew, ]; } } + return $array; }); @@ -68,7 +66,7 @@ protected static function prepare(array $array) foreach ($temp as $wordItems) { $wordItems['word'] = trim($wordItems['word']); if ($wordItems['word']) { - if (!isset($words[$wordItems['word']])) { + if (! isset($words[$wordItems['word']])) { $words[$wordItems['word']] = $wordItems; $words[$wordItems['word']]['count'] = 0; }