2022-05-30 18:37:28 +01:00
|
|
|
<?php
|
|
|
|
namespace App;
|
|
|
|
|
2022-06-08 17:05:12 +01:00
|
|
|
use Jikan\Helper\Parser;
|
2022-05-30 18:37:28 +01:00
|
|
|
use Laravel\Scout\Builder;
|
|
|
|
use Laravel\Scout\Searchable;
|
2023-02-02 23:37:37 +00:00
|
|
|
use MongoDB\BSON\UTCDateTime;
|
2022-05-30 18:37:28 +01:00
|
|
|
|
|
|
|
trait JikanSearchable
|
|
|
|
{
|
|
|
|
use Searchable;
|
|
|
|
|
2022-06-09 16:01:37 +01:00
|
|
|
protected function toTypeSenseCompatibleNestedField(string $fieldName): array
|
|
|
|
{
|
2022-06-05 18:53:53 +01:00
|
|
|
$field = $this->{$fieldName};
|
2022-06-08 17:05:12 +01:00
|
|
|
|
2022-06-05 18:53:53 +01:00
|
|
|
if (!is_array($field) && !is_object($field)) {
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
2022-06-08 17:05:12 +01:00
|
|
|
return collect($field)->to2dArrayWithDottedKeys($field, $fieldName.'.');
|
2022-06-05 18:53:53 +01:00
|
|
|
}
|
|
|
|
|
2022-06-09 16:01:37 +01:00
|
|
|
protected function getMalIdsOfField(mixed $field): array
|
|
|
|
{
|
2022-06-17 16:21:20 +01:00
|
|
|
if (is_null($field)) {
|
|
|
|
return [];
|
|
|
|
}
|
2022-06-05 18:53:53 +01:00
|
|
|
return array_map(function($elem) {
|
2022-06-08 17:05:12 +01:00
|
|
|
return $elem["mal_id"];
|
2022-06-05 18:53:53 +01:00
|
|
|
}, $field);
|
|
|
|
}
|
|
|
|
|
2023-02-02 23:37:37 +00:00
|
|
|
protected function convertToTimestamp(mixed $datetime): int
|
2022-06-08 17:05:12 +01:00
|
|
|
{
|
2023-02-02 23:37:37 +00:00
|
|
|
if ($datetime instanceof \DateTimeInterface) {
|
|
|
|
return $datetime->getTimestamp();
|
|
|
|
}
|
|
|
|
if ($datetime instanceof UTCDateTime) {
|
|
|
|
return $datetime->toDateTime()->getTimestamp();
|
|
|
|
}
|
2022-06-08 17:05:12 +01:00
|
|
|
return $datetime ? Parser::parseDate($datetime)->getTimestamp() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function queryScoutModelsByIds(Builder $builder, array $ids): \Laravel\Scout\Builder|\Illuminate\Database\Eloquent\Builder
|
2022-05-30 18:37:28 +01:00
|
|
|
{
|
|
|
|
$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}(
|
2022-06-08 17:05:12 +01:00
|
|
|
$this->getScoutKeyName(), array_map(fn ($v) => (int)$v, $ids)
|
2022-05-30 18:37:28 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|