Skip to content

Commit

Permalink
Merge pull request #73 from designmynight/searchable-updates
Browse files Browse the repository at this point in the history
Updates to searchable trait
  • Loading branch information
Will Taylor-Jackson authored Dec 12, 2018
2 parents ec3c675 + 9b6738e commit 5045e8f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 42 deletions.
19 changes: 0 additions & 19 deletions src/Contracts/FilterInterface.php

This file was deleted.

3 changes: 1 addition & 2 deletions src/EloquentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace DesignMyNight\Elasticsearch;

use DesignMyNight\Elasticsearch\Contracts\FilterInterface;
use Generator;
use Illuminate\Database\Eloquent\Builder as BaseBuilder;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -12,7 +11,7 @@

/**
* Class EloquentBuilder
* @method QueryBuilder filter(FilterInterface $filters) Support for Searchable::scopeFilter()
* @method QueryBuilder filter($filters) Support for Searchable::scopeFilter()
* @package DesignMyNight\Elasticsearch
*/
class EloquentBuilder extends BaseBuilder
Expand Down
44 changes: 23 additions & 21 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

namespace DesignMyNight\Elasticsearch;

use DesignMyNight\Elasticsearch\Contracts\FilterInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

trait Searchable
{
public static function getElasticsearchConnectionName(): string
Expand Down Expand Up @@ -118,6 +114,22 @@ public function toSearchableArray()
{
$array = $this->toArray();

foreach ($this->getArrayableRelations() as $key => $relation) {
$attributeName = snake_case($key);

if (isset($array[$attributeName]) && method_exists($relation, 'toSearchableArray')) {
$array[$attributeName] = $relation->toSearchableArray($array[$attributeName]);
} elseif (isset($array[$attributeName]) && $relation instanceof \Illuminate\Support\Collection) {
$array[$attributeName] = $relation->map(function ($item, $i) use ($array, $attributeName) {
if (method_exists($item, 'toSearchableArray')) {
return $item->toSearchableArray($array[$attributeName][$i]);
}

return $item;
})->all();
}
}

$array['id'] = $this->id;

unset($array['_id']);
Expand Down Expand Up @@ -149,14 +161,14 @@ public function datesToSearchable(array $array): array
}
}

foreach ($this->getArrayableRelations() as $key => $value) {
foreach ($this->getArrayableRelations() as $key => $relation) {
$attributeName = snake_case($key);

if (isset($array[$attributeName]) && $value instanceof Model) {
$array[$attributeName] = $value->datesToSearchable($array[$attributeName]);
} else if (isset($array[$attributeName]) && $value instanceof \Illuminate\Support\Collection) {
$array[$attributeName] = $value->map(function ($item, $i) use ($array, $attributeName) {
if ($item instanceof Model) {
if (isset($array[$attributeName]) && method_exists($relation, 'toSearchableArray')) {
$array[$attributeName] = $relation->datesToSearchable($array[$attributeName]);
} elseif (isset($array[$attributeName]) && $relation instanceof \Illuminate\Support\Collection) {
$array[$attributeName] = $relation->map(function ($item, $i) use ($array, $attributeName) {
if (method_exists($item, 'toSearchableArray')) {
return $item->datesToSearchable($array[$attributeName][$i]);
}

Expand Down Expand Up @@ -192,7 +204,7 @@ public function getSearchableDateFormat(): string
/**
* Build index details for a sub document
*
* @param Model $document
* @param \Illuminate\Database\Eloquent\Model $document
* @return array
*/
public function getSubDocumentIndexData($document)
Expand All @@ -215,16 +227,6 @@ public function newCollection(array $models = [])
return new Collection($models);
}

/**
* @param Builder $query
* @param FilterInterface $filters
* @return Builder
*/
public function scopeFilter(Builder $query, FilterInterface $filters): Builder
{
return $filters->apply($query);
}

public static function newElasticsearchQuery(): EloquentBuilder
{
$model = new static();
Expand Down

0 comments on commit 5045e8f

Please sign in to comment.