jikan-rest/app/JikanSearchable.php
pushrbx 461ccf3db6 wip
- refactored query builders
- added function for transforming nested fields on models to typesense compatible format.
- readded the AppServiceProvider class to the service container
- todo: create a base class for all models so they would share the common typesense specific implementations
2022-06-05 18:53:53 +01:00

57 lines
1.5 KiB
PHP

<?php
namespace App;
use Laravel\Scout\Builder;
use Laravel\Scout\Searchable;
trait JikanSearchable
{
use Searchable;
private function flattenArrayWithKeys($array): array {
$result = array();
foreach($array as $key=>$value) {
if(is_array($value)) {
$result = $result + $this->flattenArrayWithKeys($value, $key . '.');
}
else {
$result[$key] = $value;
}
}
return $result;
}
protected function toTypeSenseCompatibleNestedField(string $fieldName): array {
$field = $this->{$fieldName};
if (!is_array($field) && !is_object($field)) {
return $field;
}
return $this->flattenArrayWithKeys($field);
}
protected function getMalIdsOfField(mixed $field): array {
return array_map(function($elem) {
return $elem->mal_id;
}, $field);
}
public function queryScoutModelsByIds(Builder $builder, array $ids): Builder
{
$query = static::usesSoftDelete()
? $this->withTrashed() : $this->newQuery();
if ($builder->queryCallback) {
call_user_func($builder->queryCallback, $query);
}
$whereIn = in_array($this->getKeyType(), ['int', 'integer']) ?
'whereIntegerInRaw' :
'whereIn';
return $query->{$whereIn}(
$this->getScoutKeyName(), array_map(function($v) { return (int)$v; }, $ids)
);
}
}