Skip to content

Commit

Permalink
Added NestedObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Spamercz committed Jun 1, 2021
1 parent 7b49033 commit 18c360c
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/Mapping/Settings/Mapping/NestedObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types = 1);

namespace Spameri\ElasticQuery\Mapping\Settings\Mapping;

class NestedObject implements \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldInterface
{

/**
* @var string
*/
private $name;

/**
* @var \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldCollection
*/
private $fields;


public function __construct(
string $name,
\Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldCollection $fields
)
{
$this->name = $name;
$this->fields = $fields;
}


public function key(): string
{
return $this->name;
}


public function toArray(): array
{
$fields = [];
/** @var \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldInterface $field */
foreach ($this->fields as $field) {
if ($field instanceof \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldObject) {
$fields[$field->key()] = $field->toArray();
continue;
}
if ($field instanceof \Spameri\ElasticQuery\Mapping\Settings\Mapping\FieldCollection) {
$fields[$field->key()] = $field->toArray();
continue;
}

$fields[$field->key()] = $field->toArray()[$field->key()];
}

return [
'properties' => $fields,
'type' => 'nested',
];
}

}

0 comments on commit 18c360c

Please sign in to comment.