Skip to content

Commit 63ecb81

Browse files
authored
Merge pull request #53 from apisearch-io/feature/configure-index
Refactored a little bit config endpoint
2 parents 77c3d2b + 4810fdf commit 63ecb81

20 files changed

+363
-220
lines changed

App/AppRepository.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
namespace Apisearch\App;
1717

1818
use Apisearch\Config\Config;
19-
use Apisearch\Config\ImmutableConfig;
2019
use Apisearch\Exception\ResourceExistsException;
2120
use Apisearch\Exception\ResourceNotAvailableException;
2221
use Apisearch\Model\Index;
@@ -66,14 +65,14 @@ public function getIndices(): array;
6665
/**
6766
* Create an index.
6867
*
69-
* @param IndexUUID $indexUUID
70-
* @param ImmutableConfig $config
68+
* @param IndexUUID $indexUUID
69+
* @param Config $config
7170
*
7271
* @throws ResourceExistsException
7372
*/
7473
public function createIndex(
7574
IndexUUID $indexUUID,
76-
ImmutableConfig $config
75+
Config $config
7776
);
7877

7978
/**

App/HttpAppRepository.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
namespace Apisearch\App;
1717

1818
use Apisearch\Config\Config;
19-
use Apisearch\Config\ImmutableConfig;
2019
use Apisearch\Exception\ResourceExistsException;
2120
use Apisearch\Exception\ResourceNotAvailableException;
2221
use Apisearch\Http\Http;
@@ -143,20 +142,20 @@ public function getIndices(): array
143142
/**
144143
* Create an index.
145144
*
146-
* @param IndexUUID $indexUUID
147-
* @param ImmutableConfig $config
145+
* @param IndexUUID $indexUUID
146+
* @param Config $config
148147
*
149148
* @throws ResourceExistsException
150149
*/
151150
public function createIndex(
152151
IndexUUID $indexUUID,
153-
ImmutableConfig $config
152+
Config $config
154153
) {
155154
$response = $this
156155
->httpClient
157156
->get(
158157
'/index',
159-
'post',
158+
'put',
160159
Http::getAppQueryValues($this),
161160
[
162161
Http::INDEX_FIELD => $indexUUID->toArray(),
@@ -246,7 +245,7 @@ public function configureIndex(
246245
$response = $this
247246
->httpClient
248247
->get(
249-
'/index/config',
248+
'/index',
250249
'post',
251250
Http::getAppQueryValues($this, $indexUUID),
252251
[

App/InMemoryAppRepository.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
namespace Apisearch\App;
1717

1818
use Apisearch\Config\Config;
19-
use Apisearch\Config\ImmutableConfig;
2019
use Apisearch\Exception\ResourceExistsException;
2120
use Apisearch\Exception\ResourceNotAvailableException;
2221
use Apisearch\Model\AppUUID;
@@ -77,14 +76,14 @@ public function getIndices(): array
7776
/**
7877
* Create an index.
7978
*
80-
* @param IndexUUID $indexUUID
81-
* @param ImmutableConfig $config
79+
* @param IndexUUID $indexUUID
80+
* @param Config $config
8281
*
8382
* @throws ResourceExistsException
8483
*/
8584
public function createIndex(
8685
IndexUUID $indexUUID,
87-
ImmutableConfig $config
86+
Config $config
8887
) {
8988
if (array_key_exists($this->getIndexKey($indexUUID), $this->indices)) {
9089
throw ResourceExistsException::indexExists();

App/MockAppRepository.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
namespace Apisearch\App;
1717

1818
use Apisearch\Config\Config;
19-
use Apisearch\Config\ImmutableConfig;
2019
use Apisearch\Exception\MockException;
2120
use Apisearch\Exception\ResourceExistsException;
2221
use Apisearch\Exception\ResourceNotAvailableException;
@@ -82,14 +81,14 @@ public function getIndices(): array
8281
/**
8382
* Create an index.
8483
*
85-
* @param IndexUUID $indexUUID
86-
* @param ImmutableConfig $config
84+
* @param IndexUUID $indexUUID
85+
* @param Config $config
8786
*
8887
* @throws ResourceExistsException
8988
*/
9089
public function createIndex(
9190
IndexUUID $indexUUID,
92-
ImmutableConfig $config
91+
Config $config
9392
) {
9493
$this->throwMockException();
9594
}

Config/Config.php

+111-10
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,114 @@
1515

1616
namespace Apisearch\Config;
1717

18-
use Apisearch\Exception\InvalidFormatException;
1918
use Apisearch\Model\HttpTransportable;
2019

2120
/**
2221
* Class Config.
2322
*/
2423
class Config implements HttpTransportable
2524
{
25+
/**
26+
* @var string|null
27+
*
28+
* Language
29+
*/
30+
private $language;
31+
32+
/**
33+
* @var bool
34+
*
35+
* Store searchable metadata
36+
*/
37+
private $storeSearchableMetadata;
38+
39+
/**
40+
* @var Synonym[]
41+
*
42+
* Synonyms
43+
*/
44+
private $synonyms = [];
45+
2646
/**
2747
* @var Campaigns
2848
*
2949
* Campaigns
3050
*/
31-
private $campaigns;
51+
private $campaigns = [];
3252

3353
/**
3454
* Config constructor.
55+
*
56+
* @param null|string $language
57+
* @param bool $storeSearchableMetadata
3558
*/
36-
public function __construct()
37-
{
59+
public function __construct(
60+
?string $language = null,
61+
bool $storeSearchableMetadata = true
62+
) {
63+
$this->language = $language;
64+
$this->storeSearchableMetadata = $storeSearchableMetadata;
3865
$this->campaigns = new Campaigns();
3966
}
4067

68+
/**
69+
* Get language.
70+
*
71+
* @return null|string
72+
*/
73+
public function getLanguage(): ? string
74+
{
75+
return $this->language;
76+
}
77+
78+
/**
79+
* Get if searchable metadata is stored.
80+
*
81+
* @return bool
82+
*/
83+
public function shouldSearchableMetadataBeStored(): ? bool
84+
{
85+
return $this->storeSearchableMetadata;
86+
}
87+
88+
/**
89+
* Add synonym.
90+
*
91+
* @param Synonym $synonym
92+
*
93+
* @return Config
94+
*/
95+
public function addSynonym(Synonym $synonym): Config
96+
{
97+
$this->synonyms[] = $synonym;
98+
99+
return $this;
100+
}
101+
102+
/**
103+
* get synonyms.
104+
*
105+
* @return Synonym[]
106+
*/
107+
public function getSynonyms(): array
108+
{
109+
return $this->synonyms;
110+
}
111+
41112
/**
42113
* Add campaign.
43114
*
44115
* @param Campaign $campaign
116+
*
117+
* @return Config
45118
*/
46-
public function addCampaign(Campaign $campaign)
119+
public function addCampaign(Campaign $campaign): Config
47120
{
48121
$this
49122
->campaigns
50123
->addCampaign($campaign);
124+
125+
return $this;
51126
}
52127

53128
/**
@@ -68,10 +143,21 @@ public function getCampaigns(): Campaigns
68143
public function toArray(): array
69144
{
70145
return array_filter([
146+
'language' => $this->language,
147+
'store_searchable_metadata' => ($this->storeSearchableMetadata ? null : false),
148+
'synonyms' => array_map(function (Synonym $synonym) {
149+
return $synonym->toArray();
150+
}, $this->synonyms),
71151
'campaigns' => $this
72152
->campaigns
73153
->toArray(),
74-
]);
154+
], function ($element) {
155+
return
156+
!(
157+
is_null($element) ||
158+
(is_array($element) && empty($element))
159+
);
160+
});
75161
}
76162

77163
/**
@@ -80,14 +166,29 @@ public function toArray(): array
80166
* @param array $array
81167
*
82168
* @return self
83-
*
84-
* @throws InvalidFormatException
85169
*/
86-
public static function createFromArray(array $array)
170+
public static function createFromArray(array $array): self
87171
{
88-
$config = new self();
172+
$config = new self(
173+
($array['language'] ?? null),
174+
($array['store_searchable_metadata'] ?? true)
175+
);
176+
89177
$config->campaigns = Campaigns::createFromArray($array['campaigns'] ?? []);
178+
$config->synonyms = array_map(function (array $synonym) {
179+
return Synonym::createFromArray($synonym);
180+
}, $array['synonyms'] ?? []);
90181

91182
return $config;
92183
}
184+
185+
/**
186+
* Create empty.
187+
*
188+
* @return self
189+
*/
190+
public static function createEmpty(): self
191+
{
192+
return self::createFromArray([]);
193+
}
93194
}

0 commit comments

Comments
 (0)