Skip to content

Commit 0743c99

Browse files
committed
Response object
- Envelopes response from ES to objects - Stats object for response time and total count - Shards object with some numbers - Hit object for info about result hit and semi object access to data - Aggregation object for info about result aggregation and buckets in objects - ResultMapper helps map ES array response to objects
1 parent 108e494 commit 0743c99

File tree

13 files changed

+782
-0
lines changed

13 files changed

+782
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Exception;
4+
5+
6+
class AggregationNotFound extends InvalidArgumentException
7+
{
8+
9+
}

src/Exception/HitNotFound.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Exception;
4+
5+
6+
class HitNotFound extends InvalidArgumentException
7+
{
8+
9+
}

src/Response/Result.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Response;
4+
5+
6+
class Result
7+
{
8+
9+
/**
10+
* @var \Spameri\ElasticQuery\Response\Stats
11+
*/
12+
private $stats;
13+
/**
14+
* @var \Spameri\ElasticQuery\Response\Shards
15+
*/
16+
private $shards;
17+
/**
18+
* @var \Spameri\ElasticQuery\Response\Result\HitCollection
19+
*/
20+
private $hitCollection;
21+
/**
22+
* @var \Spameri\ElasticQuery\Response\Result\AggregationCollection
23+
*/
24+
private $aggregationCollection;
25+
26+
27+
public function __construct(
28+
Stats $stats
29+
, Shards $shards
30+
, \Spameri\ElasticQuery\Response\Result\HitCollection $hitCollection
31+
, \Spameri\ElasticQuery\Response\Result\AggregationCollection $aggregationCollection
32+
)
33+
{
34+
$this->stats = $stats;
35+
$this->shards = $shards;
36+
$this->hitCollection = $hitCollection;
37+
$this->aggregationCollection = $aggregationCollection;
38+
}
39+
40+
41+
public function stats() : \Spameri\ElasticQuery\Response\Stats
42+
{
43+
return $this->stats;
44+
}
45+
46+
47+
public function shards() : \Spameri\ElasticQuery\Response\Shards
48+
{
49+
return $this->shards;
50+
}
51+
52+
53+
public function hits() : \Spameri\ElasticQuery\Response\Result\HitCollection
54+
{
55+
return $this->hitCollection;
56+
}
57+
58+
59+
public function aggregations() : \Spameri\ElasticQuery\Response\Result\AggregationCollection
60+
{
61+
return $this->aggregationCollection;
62+
}
63+
64+
65+
public function getHit(
66+
string $id
67+
) : \Spameri\ElasticQuery\Response\Result\Hit
68+
{
69+
/** @var \Spameri\ElasticQuery\Response\Result\Hit $hit */
70+
foreach ($this->hitCollection as $hit) {
71+
if ($hit->id() === $id) {
72+
return $hit;
73+
}
74+
}
75+
76+
throw new \Spameri\ElasticQuery\Exception\HitNotFound(
77+
'Hit with id: ' . $id . 'not found.'
78+
);
79+
}
80+
81+
82+
public function getAggregation(
83+
string $name
84+
) : \Spameri\ElasticQuery\Response\Result\Aggregation
85+
{
86+
/** @var \Spameri\ElasticQuery\Response\Result\Aggregation $aggregation */
87+
foreach ($this->aggregationCollection as $aggregation) {
88+
if ($aggregation->name() === $name) {
89+
return $aggregation;
90+
}
91+
}
92+
93+
throw new \Spameri\ElasticQuery\Exception\AggregationNotFound(
94+
'Aggregation with name: ' . $name . ' has not been found.'
95+
);
96+
}
97+
98+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Response\Result;
4+
5+
6+
class Aggregation
7+
{
8+
9+
/**
10+
* @var string
11+
*/
12+
private $name;
13+
/**
14+
* @var int
15+
*/
16+
private $position;
17+
/**
18+
* @var \Spameri\ElasticQuery\Response\Result\Aggregation\BucketCollection
19+
*/
20+
private $bucketCollection;
21+
/**
22+
* @var \Spameri\ElasticQuery\Response\Result\AggregationCollection
23+
*/
24+
private $aggregations;
25+
26+
27+
public function __construct(
28+
string $name
29+
, int $position
30+
, \Spameri\ElasticQuery\Response\Result\Aggregation\BucketCollection $bucketCollection
31+
, \Spameri\ElasticQuery\Response\Result\AggregationCollection $subAggregations
32+
)
33+
{
34+
$this->name = $name;
35+
$this->position = $position;
36+
$this->bucketCollection = $bucketCollection;
37+
$this->aggregations = $subAggregations;
38+
}
39+
40+
41+
public function name() : string
42+
{
43+
return $this->name;
44+
}
45+
46+
47+
public function position() : int
48+
{
49+
return $this->position;
50+
}
51+
52+
53+
public function bucketCollection() : \Spameri\ElasticQuery\Response\Result\Aggregation\BucketCollection
54+
{
55+
return $this->bucketCollection;
56+
}
57+
58+
59+
public function aggregations() : \Spameri\ElasticQuery\Response\Result\AggregationCollection
60+
{
61+
return $this->aggregations;
62+
}
63+
64+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Response\Result\Aggregation;
4+
5+
6+
class Bucket
7+
{
8+
9+
/**
10+
* @var string
11+
*/
12+
private $key;
13+
/**
14+
* @var int
15+
*/
16+
private $docCount;
17+
/**
18+
* @var int
19+
*/
20+
private $position;
21+
22+
23+
public function __construct(
24+
string $key
25+
, int $docCount
26+
, int $position
27+
)
28+
{
29+
$this->key = $key;
30+
$this->docCount = $docCount;
31+
$this->position = $position;
32+
}
33+
34+
35+
public function key() : string
36+
{
37+
return $this->key;
38+
}
39+
40+
41+
public function docCount() : int
42+
{
43+
return $this->docCount;
44+
}
45+
46+
47+
public function position() : int
48+
{
49+
return $this->position;
50+
}
51+
52+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Response\Result\Aggregation;
4+
5+
6+
class BucketCollection implements \IteratorAggregate
7+
{
8+
9+
/**
10+
* @var \Spameri\ElasticQuery\Response\Result\Aggregation\Bucket[]
11+
*/
12+
private $buckets;
13+
14+
15+
public function __construct(
16+
\Spameri\ElasticQuery\Response\Result\Aggregation\Bucket ... $buckets
17+
)
18+
{
19+
$this->buckets = $buckets;
20+
}
21+
22+
23+
public function getIterator() : \ArrayIterator
24+
{
25+
return new \ArrayIterator($this->buckets);
26+
}
27+
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Response\Result;
4+
5+
6+
class AggregationCollection implements \IteratorAggregate
7+
{
8+
9+
/**
10+
* @var \Spameri\ElasticQuery\Response\Result\Aggregation[]
11+
*/
12+
private $aggregations;
13+
14+
15+
public function __construct(
16+
\Spameri\ElasticQuery\Response\Result\Aggregation ... $aggregations
17+
)
18+
{
19+
$this->aggregations = $aggregations;
20+
}
21+
22+
23+
public function getIterator() : \ArrayIterator
24+
{
25+
return new \ArrayIterator($this->aggregations);
26+
}
27+
28+
}

src/Response/Result/Hit.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Spameri\ElasticQuery\Response\Result;
4+
5+
6+
class Hit
7+
{
8+
9+
/**
10+
* @var array
11+
*/
12+
private $source;
13+
/**
14+
* @var int
15+
*/
16+
private $position;
17+
/**
18+
* @var string
19+
*/
20+
private $index;
21+
/**
22+
* @var string
23+
*/
24+
private $type;
25+
/**
26+
* @var string
27+
*/
28+
private $id;
29+
/**
30+
* @var string
31+
*/
32+
private $score;
33+
34+
35+
public function __construct(
36+
array $source
37+
, int $position
38+
, string $index
39+
, string $type
40+
, string $id
41+
, string $score
42+
)
43+
{
44+
$this->source = $source;
45+
$this->position = $position;
46+
$this->index = $index;
47+
$this->type = $type;
48+
$this->id = $id;
49+
$this->score = $score;
50+
}
51+
52+
53+
public function source() : array
54+
{
55+
return $this->source;
56+
}
57+
58+
59+
public function getValue(
60+
string $key
61+
)
62+
{
63+
return $this->source[$key];
64+
}
65+
66+
67+
public function position() : int
68+
{
69+
return $this->position;
70+
}
71+
72+
73+
public function index() : string
74+
{
75+
return $this->index;
76+
}
77+
78+
79+
public function type() : string
80+
{
81+
return $this->type;
82+
}
83+
84+
85+
public function id() : string
86+
{
87+
return $this->id;
88+
}
89+
90+
91+
public function score() : string
92+
{
93+
return $this->score;
94+
}
95+
96+
}

0 commit comments

Comments
 (0)