Skip to content

Commit

Permalink
Merge pull request #80 from designmynight/refactor-date-conversion
Browse files Browse the repository at this point in the history
Refactor date conversion
  • Loading branch information
Will Taylor-Jackson authored Jan 27, 2019
2 parents 39f4274 + 1301e74 commit 82f8ed8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 65 deletions.
12 changes: 7 additions & 5 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ public function addToIndex()
}

$instance = $this->first();
$instance->setConnection($instance->getElasticsearchConnectionName());
$query = $this->first()->newQueryWithoutScopes();

$docs = $this->map(function ($model) {
$docs = $this->map(function ($model, $i) {
return $model->toSearchableArray();
});

return $instance->onSearchConnection(function ($instance) use ($docs) {
$query = $instance->newQueryWithoutScopes();
$success = $query->insert($docs->all());

return $query->insert($docs->all());
}, $instance);
unset($docs);

return $success;
}
}
77 changes: 17 additions & 60 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,28 @@ public function removeFromIndex()
*/
public function toSearchableArray()
{
// Run this on the search connection if it's not the current connection
if ($this->getConnectionName() !== static::getElasticsearchConnectionName()) {
return $this->onSearchConnection(function ($model) {
return $model->toSearchableArray();
}, $this);
}

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

return $item;
Expand All @@ -135,69 +146,15 @@ public function toSearchableArray()
$subDocuments = $this->$field ?? [];

foreach ($subDocuments as $subDocument) {
$array['child_documents'][] = $this->getSubDocumentIndexData($subDocument, $field);
$array['child_documents'][] = $this->onSearchConnection(function ($model) {
return $model->getSubDocumentIndexData($model);
}, $subDocument);
}
}

$array = $this->datesToSearchable($array);

return $array;
}

/**
* Convert all dates to searchable format
*
* @param array $array
* @return array
*/
public function datesToSearchable(array $array): array
{
foreach ($this->getDates() as $dateField) {
if (isset($array[$dateField])) {
$array[$dateField] = $this->fromDateTimeSearchable($array[$dateField]);
}
}

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

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]);
}

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

return $array;
}

/**
* Convert a DateTime to a string in ES format.
*
* @param \DateTime|int $value
* @return string
*/
public function fromDateTimeSearchable($value): string
{
return empty($value) ? $value : $this->asDateTime($value)->format($this->getSearchableDateFormat());
}

/**
* Return the format to be used for dates in Elasticsearch
*
* @return string
*/
public function getSearchableDateFormat(): string
{
return 'Y-m-d\TH:i:s';
}

/**
* Build index details for a sub document
*
Expand Down

0 comments on commit 82f8ed8

Please sign in to comment.