Skip to content

Commit 9be27e6

Browse files
committed
TASK: Automatically set NEOS_TYPE_FIELD on getData
1 parent 1a08f8e commit 9be27e6

3 files changed

Lines changed: 36 additions & 40 deletions

File tree

Classes/Domain/Model/Document.php

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Document
5050
* With a fresh instance of this document, or a conducted change, this flag gets set to TRUE again.
5151
* When retrieved from the storage, or successfully set to the storage, it's FALSE.
5252
*
53-
* @var boolean
53+
* @var bool
5454
*/
5555
protected $dirty = true;
5656

@@ -60,7 +60,7 @@ class Document
6060
* @param string $id
6161
* @param int $version
6262
*/
63-
public function __construct(AbstractType $type, array $data = null, $id = null, $version = null)
63+
public function __construct(AbstractType $type, ?array $data = null, ?string $id = null, ?int $version = null)
6464
{
6565
$this->type = $type;
6666
$this->data = $data;
@@ -84,10 +84,11 @@ public function __clone()
8484
/**
8585
* Stores this document. If ID is given, PUT will be used; else POST
8686
*
87-
* @throws ElasticSearchException
8887
* @return void
88+
* @throws \Neos\Flow\Http\Exception
89+
* @throws ElasticSearchException
8990
*/
90-
public function store()
91+
public function store(): void
9192
{
9293
if ($this->id !== null) {
9394
$method = 'PUT';
@@ -96,28 +97,15 @@ public function store()
9697
$method = 'POST';
9798
$path = '';
9899
}
99-
$response = $this->request($method, $path, [], json_encode($this->data));
100+
101+
$response = $this->request($method, $path, [], json_encode($this->getData()));
100102
$treatedContent = $response->getTreatedContent();
101103

102104
$this->id = $treatedContent['_id'];
103105
$this->version = $treatedContent['_version'];
104106
$this->dirty = false;
105107
}
106108

107-
/**
108-
* @param string $method
109-
* @param string $path
110-
* @param array $arguments
111-
* @param string $content
112-
* @return Response
113-
* @throws ElasticSearchException
114-
* @throws \Neos\Flow\Http\Exception
115-
*/
116-
protected function request(string $method, ?string $path = null, array $arguments = [], ?string $content = null): Response
117-
{
118-
return $this->type->request($method, $path, $arguments, $content);
119-
}
120-
121109
/**
122110
* @return boolean
123111
*/
@@ -126,15 +114,6 @@ public function isDirty(): bool
126114
return $this->dirty;
127115
}
128116

129-
/**
130-
* @param boolean $dirty
131-
* @return void
132-
*/
133-
protected function setDirty(bool $dirty = true): void
134-
{
135-
$this->dirty = $dirty;
136-
}
137-
138117
/**
139118
* @return int
140119
*/
@@ -150,6 +129,7 @@ public function getVersion(): int
150129
*/
151130
public function getData(): array
152131
{
132+
$this->data[Mapping::NEOS_TYPE_FIELD] = $this->type->getName();
153133
return $this->data;
154134
}
155135

@@ -179,7 +159,7 @@ public function getId(): string
179159
* @return mixed
180160
* @throws ElasticSearchException
181161
*/
182-
public function getField(string $fieldName, $silent = false)
162+
public function getField(string $fieldName, bool $silent = false)
183163
{
184164
if (!array_key_exists($fieldName, $this->data) && $silent === false) {
185165
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);
@@ -195,4 +175,28 @@ public function getType(): AbstractType
195175
{
196176
return $this->type;
197177
}
178+
179+
/**
180+
* @param string $method
181+
* @param string $path
182+
* @param array $arguments
183+
* @param string $content
184+
* @return Response
185+
* @throws ElasticSearchException
186+
* @throws \Neos\Flow\Http\Exception
187+
*/
188+
protected function request(string $method, ?string $path = null, array $arguments = [], ?string $content = null): Response
189+
{
190+
return $this->type->request($method, $path, $arguments, $content);
191+
}
192+
193+
/**
194+
* @param boolean $dirty
195+
* @return void
196+
*/
197+
protected function setDirty(bool $dirty = true): void
198+
{
199+
$this->dirty = $dirty;
200+
}
201+
198202
}

Classes/Domain/Model/GenericType.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,4 @@
1818
*/
1919
class GenericType extends AbstractType
2020
{
21-
/**
22-
* @param Index $index
23-
* @param string $name
24-
*/
25-
public function __construct(Index $index, $name = null)
26-
{
27-
parent::__construct($index, $name);
28-
}
2921
}

Classes/Domain/Model/Mapping.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
class Mapping
2323
{
2424

25-
protected const TYPE_PROPERTY_NAME = '_neos_type';
25+
public const NEOS_TYPE_FIELD = '_neosType';
2626

2727
/**
2828
* @var AbstractType
@@ -55,7 +55,7 @@ class Mapping
5555
public function __construct(AbstractType $type)
5656
{
5757
$this->type = $type;
58-
$this->properties[static::TYPE_PROPERTY_NAME] = ['type' => 'keyword'];
58+
$this->properties[static::NEOS_TYPE_FIELD] = ['type' => 'keyword'];
5959
}
6060

6161
/**
@@ -100,7 +100,7 @@ public function apply(): Response
100100
{
101101
$content = json_encode($this->asArray());
102102

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

106106
/**

0 commit comments

Comments
 (0)