Skip to content

Commit 099686b

Browse files
authored
Merge pull request #145 from apisearch-io/feature/some-new-methods
Added some extra methods in model classes
2 parents cebcee2 + c52aad3 commit 099686b

File tree

4 files changed

+114
-6
lines changed

4 files changed

+114
-6
lines changed

Model/Index.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Index implements HttpTransportable
7777
*
7878
* @var string[]
7979
*/
80-
private $fields = [];
80+
private $fields;
8181

8282
/**
8383
* Metadata.
@@ -199,6 +199,16 @@ public function getFields(): array
199199
return $this->fields;
200200
}
201201

202+
/**
203+
* @param array $fields
204+
*
205+
* @return void
206+
*/
207+
public function withFields(array $fields)
208+
{
209+
$this->fields = $fields;
210+
}
211+
202212
/**
203213
* Get Metadata.
204214
*

Query/Query.php

+28-5
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,7 @@ public static function create(
257257
*/
258258
public static function createMatchAll(): self
259259
{
260-
return static::create(
261-
'',
262-
self::DEFAULT_PAGE,
263-
self::DEFAULT_SIZE
264-
);
260+
return static::create('');
265261
}
266262

267263
/**
@@ -342,6 +338,16 @@ public function getFields(): array
342338
return $this->fields;
343339
}
344340

341+
/**
342+
* @param int $size
343+
*
344+
* @return void
345+
*/
346+
public function forceSize(int $size)
347+
{
348+
$this->size = $size;
349+
}
350+
345351
/**
346352
* Filter universe by types.
347353
*
@@ -867,6 +873,23 @@ public function getAggregation(string $aggregationName): ? Aggregation
867873
return $this->aggregations[$aggregationName] ?? null;
868874
}
869875

876+
/**
877+
* @param string $aggregationField
878+
*
879+
* @return void
880+
*/
881+
public function deleteAggregationByField(string $aggregationField): void
882+
{
883+
$aggregationField = Item::getPathByField($aggregationField);
884+
foreach ($this->aggregations as $key => $aggregation) {
885+
if ($aggregation->getField() === $aggregationField) {
886+
unset($this->aggregations[$key]);
887+
888+
return;
889+
}
890+
}
891+
}
892+
870893
/**
871894
* Return Querytext.
872895
*

Tests/Model/IndexTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,35 @@ public function testWithMetadataValue()
145145
$index = Index::createFromArray($index->toArray());
146146
$this->assertEquals('val1', $index->getMetadataValue('key1'));
147147
}
148+
149+
public function testWithFields()
150+
{
151+
$index = Index::createFromArray([
152+
'uuid' => [
153+
'id' => 'testId',
154+
],
155+
'app_id' => [
156+
'id' => 'testAppId',
157+
],
158+
]);
159+
160+
$this->assertEquals([], $index->getFields());
161+
162+
$index = Index::createFromArray([
163+
'uuid' => [
164+
'id' => 'testId',
165+
],
166+
'app_id' => [
167+
'id' => 'testAppId',
168+
],
169+
'is_ok' => true,
170+
'doc_count' => 10,
171+
'size' => '1kb',
172+
'fields' => ['f1', 'f2', 'f3'],
173+
]);
174+
175+
$this->assertEquals(['f1', 'f2', 'f3'], $index->getFields());
176+
$index->withFields(['f4', 'f5']);
177+
$this->assertEquals(['f4', 'f5'], $index->getFields());
178+
}
148179
}

Tests/Query/QueryTest.php

+44
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,48 @@ public function testAggregationPromoted()
330330
$this->assertEquals(['item1', 'item3'], $query->getAggregation('field2')->getPromoted());
331331
$this->assertEquals(['item4'], $query->getAggregation('field3')->getPromoted());
332332
}
333+
334+
public function testDeleteAggregationByField()
335+
{
336+
$query = Query::createMatchAll()
337+
->aggregateBy('field1', 'field1', Filter::AT_LEAST_ONE, Aggregation::SORT_BY_COUNT_ASC)
338+
->aggregateBy('field2', 'field2', Filter::AT_LEAST_ONE, Aggregation::SORT_BY_COUNT_ASC);
339+
340+
$this->assertCount(2, $query->getAggregations());
341+
$this->assertNotNull($query->getAggregation('field1'));
342+
$this->assertNotNull($query->getAggregation('field2'));
343+
$this->assertNull($query->getAggregation('field3'));
344+
345+
$query->deleteAggregationByField('field1');
346+
$this->assertCount(1, $query->getAggregations());
347+
$this->assertNull($query->getAggregation('field1'));
348+
$this->assertNotNull($query->getAggregation('field2'));
349+
$this->assertNull($query->getAggregation('field3'));
350+
351+
$query->deleteAggregationByField('field1');
352+
$this->assertCount(1, $query->getAggregations());
353+
$this->assertNull($query->getAggregation('field1'));
354+
$this->assertNotNull($query->getAggregation('field2'));
355+
$this->assertNull($query->getAggregation('field3'));
356+
357+
$query->deleteAggregationByField('field2');
358+
$this->assertCount(0, $query->getAggregations());
359+
$this->assertNull($query->getAggregation('field1'));
360+
$this->assertNull($query->getAggregation('field2'));
361+
$this->assertNull($query->getAggregation('field3'));
362+
363+
$query->deleteAggregationByField('field3');
364+
$this->assertCount(0, $query->getAggregations());
365+
$this->assertNull($query->getAggregation('field1'));
366+
$this->assertNull($query->getAggregation('field2'));
367+
$this->assertNull($query->getAggregation('field3'));
368+
}
369+
370+
public function testForceSize()
371+
{
372+
$query = Query::create('x', 1, 10);
373+
$this->assertEquals(10, $query->getSize());
374+
$query->forceSize(7);
375+
$this->assertEquals(7, $query->getSize());
376+
}
333377
}

0 commit comments

Comments
 (0)