Skip to content

Commit

Permalink
TASK: Automatically set NEOS_TYPE_FIELD on getData
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellienert committed Apr 5, 2020
1 parent 1a08f8e commit 9be27e6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
62 changes: 33 additions & 29 deletions Classes/Domain/Model/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Document
* With a fresh instance of this document, or a conducted change, this flag gets set to TRUE again.
* When retrieved from the storage, or successfully set to the storage, it's FALSE.
*
* @var boolean
* @var bool
*/
protected $dirty = true;

Expand All @@ -60,7 +60,7 @@ class Document
* @param string $id
* @param int $version
*/
public function __construct(AbstractType $type, array $data = null, $id = null, $version = null)
public function __construct(AbstractType $type, ?array $data = null, ?string $id = null, ?int $version = null)
{
$this->type = $type;
$this->data = $data;
Expand All @@ -84,10 +84,11 @@ public function __clone()
/**
* Stores this document. If ID is given, PUT will be used; else POST
*
* @throws ElasticSearchException
* @return void
* @throws \Neos\Flow\Http\Exception
* @throws ElasticSearchException
*/
public function store()
public function store(): void
{
if ($this->id !== null) {
$method = 'PUT';
Expand All @@ -96,28 +97,15 @@ public function store()
$method = 'POST';
$path = '';
}
$response = $this->request($method, $path, [], json_encode($this->data));

$response = $this->request($method, $path, [], json_encode($this->getData()));
$treatedContent = $response->getTreatedContent();

$this->id = $treatedContent['_id'];
$this->version = $treatedContent['_version'];
$this->dirty = false;
}

/**
* @param string $method
* @param string $path
* @param array $arguments
* @param string $content
* @return Response
* @throws ElasticSearchException
* @throws \Neos\Flow\Http\Exception
*/
protected function request(string $method, ?string $path = null, array $arguments = [], ?string $content = null): Response
{
return $this->type->request($method, $path, $arguments, $content);
}

/**
* @return boolean
*/
Expand All @@ -126,15 +114,6 @@ public function isDirty(): bool
return $this->dirty;
}

/**
* @param boolean $dirty
* @return void
*/
protected function setDirty(bool $dirty = true): void
{
$this->dirty = $dirty;
}

/**
* @return int
*/
Expand All @@ -150,6 +129,7 @@ public function getVersion(): int
*/
public function getData(): array
{
$this->data[Mapping::NEOS_TYPE_FIELD] = $this->type->getName();
return $this->data;
}

Expand Down Expand Up @@ -179,7 +159,7 @@ public function getId(): string
* @return mixed
* @throws ElasticSearchException
*/
public function getField(string $fieldName, $silent = false)
public function getField(string $fieldName, bool $silent = false)
{
if (!array_key_exists($fieldName, $this->data) && $silent === false) {
throw new ElasticSearchException(sprintf('The field %s was not present in data of document in %s/%s.', $fieldName, $this->type->getIndex()->getName(), $this->type->getName()), 1340274696);
Expand All @@ -195,4 +175,28 @@ public function getType(): AbstractType
{
return $this->type;
}

/**
* @param string $method
* @param string $path
* @param array $arguments
* @param string $content
* @return Response
* @throws ElasticSearchException
* @throws \Neos\Flow\Http\Exception
*/
protected function request(string $method, ?string $path = null, array $arguments = [], ?string $content = null): Response
{
return $this->type->request($method, $path, $arguments, $content);
}

/**
* @param boolean $dirty
* @return void
*/
protected function setDirty(bool $dirty = true): void
{
$this->dirty = $dirty;
}

}
8 changes: 0 additions & 8 deletions Classes/Domain/Model/GenericType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,4 @@
*/
class GenericType extends AbstractType
{
/**
* @param Index $index
* @param string $name
*/
public function __construct(Index $index, $name = null)
{
parent::__construct($index, $name);
}
}
6 changes: 3 additions & 3 deletions Classes/Domain/Model/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class Mapping
{

protected const TYPE_PROPERTY_NAME = '_neos_type';
public const NEOS_TYPE_FIELD = '_neosType';

/**
* @var AbstractType
Expand Down Expand Up @@ -55,7 +55,7 @@ class Mapping
public function __construct(AbstractType $type)
{
$this->type = $type;
$this->properties[static::TYPE_PROPERTY_NAME] = ['type' => 'keyword'];
$this->properties[static::NEOS_TYPE_FIELD] = ['type' => 'keyword'];
}

/**
Expand Down Expand Up @@ -100,7 +100,7 @@ public function apply(): Response
{
$content = json_encode($this->asArray());

return $this->type->request('PUT', '/_mapping', [], $content);
return $this->type->request('PUT', '/_mapping', ['include_type_name' => 'false'], $content);
}

/**
Expand Down

0 comments on commit 9be27e6

Please sign in to comment.