Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.

Commit e39f99a

Browse files
authored
Merge pull request #116 from apisearch-io/feature/added-index-config-metadata
Added metadata in config
2 parents d0895bf + 237b574 commit e39f99a

File tree

5 files changed

+81
-10
lines changed

5 files changed

+81
-10
lines changed

Config/Config.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,32 @@ class Config implements HttpTransportable
7171
*/
7272
private $replicas;
7373

74+
/**
75+
* @var array
76+
*/
77+
private $metadata;
78+
7479
/**
7580
* Config constructor.
7681
*
7782
* @param string|null $language
7883
* @param bool $storeSearchableMetadata
7984
* @param int $shards
8085
* @param int $replicas
86+
* @param array $metadata
8187
*/
8288
public function __construct(
8389
?string $language = null,
8490
bool $storeSearchableMetadata = true,
8591
int $shards = self::DEFAULT_SHARDS,
86-
int $replicas = self::DEFAULT_REPLICAS
92+
int $replicas = self::DEFAULT_REPLICAS,
93+
array $metadata = []
8794
) {
8895
$this->language = $language;
8996
$this->storeSearchableMetadata = $storeSearchableMetadata;
9097
$this->shards = $shards;
9198
$this->replicas = $replicas;
99+
$this->metadata = $metadata;
92100
}
93101

94102
/**
@@ -155,6 +163,29 @@ public function getReplicas(): int
155163
return $this->replicas;
156164
}
157165

166+
/**
167+
* @param string $key
168+
* @param $value
169+
*
170+
* @return Config
171+
*/
172+
public function addMetadataValue(
173+
string $key,
174+
$value
175+
): Config {
176+
$this->metadata[$key] = $value;
177+
178+
return $this;
179+
}
180+
181+
/**
182+
* @return array
183+
*/
184+
public function getMetadata(): array
185+
{
186+
return $this->metadata;
187+
}
188+
158189
/**
159190
* To array.
160191
*
@@ -170,6 +201,7 @@ public function toArray(): array
170201
}, $this->synonyms),
171202
'shards' => $this->shards,
172203
'replicas' => $this->replicas,
204+
'metadata' => $this->metadata,
173205
], function ($element) {
174206
return
175207
!(
@@ -198,6 +230,7 @@ public static function createFromArray(array $array): self
198230
}, $array['synonyms'] ?? []);
199231
$config->shards = (int) ($array['shards'] ?? self::DEFAULT_SHARDS);
200232
$config->replicas = (int) ($array['replicas'] ?? self::DEFAULT_REPLICAS);
233+
$config->metadata = $array['metadata'] ?? [];
201234

202235
return $config;
203236
}

Model/Index.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function __construct(
103103
IndexUUID $uuid,
104104
AppUUID $appUUID,
105105
bool $isOK,
106-
int $docCount = 0,
106+
int $docCount,
107107
string $size,
108108
int $shards,
109109
int $replicas,
@@ -209,6 +209,18 @@ public function getMetadata(): array
209209
return $this->metadata;
210210
}
211211

212+
/**
213+
* Get Metadata value.
214+
*
215+
* @param string $key
216+
*
217+
* @return mixed|null
218+
*/
219+
public function getMetadataValue(string $key)
220+
{
221+
return $this->metadata[$key] ?? null;
222+
}
223+
212224
/**
213225
* To array.
214226
*

Tests/Config/ConfigTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function testCreate()
3434
$this->assertTrue($config->shouldSearchableMetadataBeStored());
3535
$this->assertEquals(Config::DEFAULT_SHARDS, $config->getShards());
3636
$this->assertEquals(Config::DEFAULT_REPLICAS, $config->getReplicas());
37+
$this->assertEmpty($config->getMetadata());
3738
}
3839

3940
/**
@@ -156,4 +157,28 @@ public function testShardsAndReplicas()
156157
$this->assertEquals(5, $config->getShards());
157158
$this->assertEquals(10, $config->getReplicas());
158159
}
160+
161+
/**
162+
* Test metadata.
163+
*/
164+
public function testMetadata()
165+
{
166+
$config = new Config('es', true, 6, 3, [
167+
'key1' => 'val1',
168+
'key2' => 'val2',
169+
]);
170+
$config->addMetadataValue('key3', 'val3');
171+
172+
$this->assertEquals('val1', $config->getMetadata()['key1']);
173+
$this->assertEquals('val2', $config->getMetadata()['key2']);
174+
$this->assertEquals('val3', $config->getMetadata()['key3']);
175+
$this->assertFalse(array_key_exists('key4', $config->getMetadata()));
176+
177+
$newConfig = Config::createFromArray($config->toArray());
178+
179+
$this->assertEquals('val1', $newConfig->getMetadata()['key1']);
180+
$this->assertEquals('val2', $newConfig->getMetadata()['key2']);
181+
$this->assertEquals('val3', $config->getMetadata()['key3']);
182+
$this->assertFalse(array_key_exists('key4', $config->getMetadata()));
183+
}
159184
}

Transformer/ItemTransformed.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,30 @@
1616
namespace Apisearch\Transformer;
1717

1818
use Apisearch\Model\Item;
19-
use Symfony\Contracts\EventDispatcher\Event as ContractEvent;
2019
use Symfony\Component\EventDispatcher\Event as ComponentEvent;
20+
use Symfony\Contracts\EventDispatcher\Event as ContractEvent;
2121

22-
/**
22+
/*
2323
* This code adds usability with Symfony 3.4 -> 5.0
2424
*/
2525
if (class_exists("Symfony\Contracts\EventDispatcher\Event")) {
26-
2726
/**
2827
* Class BaseTransformed.
2928
*/
30-
class BaseTransformed extends ContractEvent {}
29+
class BaseTransformed extends ContractEvent
30+
{
31+
}
3132
} else {
32-
3333
/**
3434
* Class BaseTransformed.
3535
*/
36-
class BaseTransformed extends ComponentEvent {}
36+
class BaseTransformed extends ComponentEvent
37+
{
38+
}
3739
}
3840

3941
/**
40-
* Class ItemTransformed
42+
* Class ItemTransformed.
4143
*/
4244
class ItemTransformed extends BaseTransformed
4345
{

Transformer/Transformer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ public function toItem($object): ? Item
166166
);
167167
}
168168

169-
170169
return $item;
171170
}
172171
}

0 commit comments

Comments
 (0)