Skip to content

Commit 5467fbb

Browse files
committed
Move index payloads onto value objects
1 parent b1c1fc8 commit 5467fbb

5 files changed

Lines changed: 98 additions & 19 deletions

File tree

src/Indices/Alias.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,24 @@ public function routing(): ?string
4545
{
4646
return $this->routing;
4747
}
48+
49+
/**
50+
* Get the OpenSearch alias body payload.
51+
*
52+
* @return array<string, mixed>
53+
*/
54+
public function toArray(): array
55+
{
56+
$body = [];
57+
58+
if ($this->routing) {
59+
$body['routing'] = $this->routing;
60+
}
61+
62+
if ($this->filter) {
63+
$body['filter'] = $this->filter;
64+
}
65+
66+
return $body;
67+
}
4868
}

src/Indices/IndexBlueprint.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,26 @@ public function settings(): ?Settings
4141
{
4242
return $this->settings;
4343
}
44+
45+
/**
46+
* Get the OpenSearch create index payload.
47+
*
48+
* @return array<string, mixed>
49+
*/
50+
public function toArray(): array
51+
{
52+
$params = [
53+
'index' => $this->name,
54+
];
55+
56+
if ($mapping = $this->mapping?->toArray()) {
57+
$params['body']['mappings'] = $mapping;
58+
}
59+
60+
if ($settings = $this->settings?->toArray()) {
61+
$params['body']['settings'] = $settings;
62+
}
63+
64+
return $params;
65+
}
4466
}

src/Indices/IndexManager.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,7 @@ public function exists(string $indexName): bool
6565
*/
6666
public function create(IndexBlueprint $index): self
6767
{
68-
$params = [
69-
'index' => $index->name(),
70-
];
71-
72-
if ($mapping = $index->mapping()?->toArray()) {
73-
$params['body']['mappings'] = $mapping;
74-
}
75-
76-
if ($settings = $index->settings()?->toArray()) {
77-
$params['body']['settings'] = $settings;
78-
}
79-
80-
$this->indices->create($params);
68+
$this->indices->create($index->toArray());
8169

8270
return $this;
8371
}
@@ -158,12 +146,8 @@ public function putAlias(string $indexName, Alias $alias): self
158146
'name' => $alias->name(),
159147
];
160148

161-
if ($alias->routing()) {
162-
$params['body']['routing'] = $alias->routing();
163-
}
164-
165-
if ($alias->filter()) {
166-
$params['body']['filter'] = $alias->filter();
149+
if ($body = $alias->toArray()) {
150+
$params['body'] = $body;
167151
}
168152

169153
$this->indices->putAlias($params);

tests/Unit/Indices/AliasTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,22 @@
1111
$this->assertSame(['term' => ['year' => 2030]], $alias->filter());
1212
$this->assertSame('year', $alias->routing());
1313
});
14+
15+
test('array casting without filter and routing', function () {
16+
$alias = new Alias('2030');
17+
18+
$this->assertSame([], $alias->toArray());
19+
});
20+
21+
test('array casting with filter and routing', function () {
22+
$alias = new Alias('2030', ['term' => ['year' => 2030]], 'year');
23+
24+
$this->assertSame([
25+
'routing' => 'year',
26+
'filter' => [
27+
'term' => [
28+
'year' => 2030,
29+
],
30+
],
31+
], $alias->toArray());
32+
});

tests/Unit/Indices/IndexBlueprintTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,37 @@
2222
$this->assertSame($mapping, $index->mapping());
2323
$this->assertSame($settings, $index->settings());
2424
});
25+
26+
test('array casting without mapping and settings', function () {
27+
$index = new IndexBlueprint('foo');
28+
29+
$this->assertSame([
30+
'index' => 'foo',
31+
], $index->toArray());
32+
});
33+
34+
test('array casting with mapping and settings', function () {
35+
$index = new IndexBlueprint(
36+
'foo',
37+
(new Mapping)->text('title'),
38+
(new Settings)->index(['number_of_replicas' => 2])
39+
);
40+
41+
$this->assertSame([
42+
'index' => 'foo',
43+
'body' => [
44+
'mappings' => [
45+
'properties' => [
46+
'title' => [
47+
'type' => 'text',
48+
],
49+
],
50+
],
51+
'settings' => [
52+
'index' => [
53+
'number_of_replicas' => 2,
54+
],
55+
],
56+
],
57+
], $index->toArray());
58+
});

0 commit comments

Comments
 (0)