Skip to content

Commit 3398134

Browse files
committed
Adding fetch support to Result for dot notation fetching.
Adding __get support to alias the fetch function. Created test cases.
1 parent 588feb6 commit 3398134

19 files changed

+73
-0
lines changed

.gitignore

100644100755
File mode changed.

LICENSE

100644100755
File mode changed.

README.md

100644100755
+14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ To do so, you need to create the method name you wish as protected in the query
4141
You can extend the Model class easily. Just extend it!
4242
In case you were using the namespaces, you can set the models namespace in the query class by overriding the modelNamespace public method. This method should return a string ending with \
4343

44+
##Retrieving results
45+
The returned result set implements the ArrayAccess interface to access specific document inside the result. i.e.
46+
```PHP
47+
$result = SomeQuery::all();
48+
```
49+
You can then get a document like this:
50+
```PHP
51+
$doc = $result[1]; //gets the second document
52+
```
53+
Or you can use the dot notation like that:
54+
```
55+
$result->fetch('hits.hits.0'); //for any absolute access
56+
```
57+
4458
##Examples
4559
Please check [tests/](/tests) folder. Basically, the case1.php is the main file.
4660

composer.json

100644100755
File mode changed.

ideh/classes.php

100644100755
File mode changed.

src/Model.php

100644100755
File mode changed.

src/MultiGetResult.php

100644100755
File mode changed.

src/Query.php

100644100755
File mode changed.

src/Result.php

100644100755
+24
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,28 @@ public function first() {
229229
}
230230
}
231231

232+
/**
233+
* Gets specific value from within the result set using dot notation
234+
*
235+
* The path should be absolution from the beginning, and include the normal
236+
* elastic search structure depending on the query type
237+
*
238+
* @param string $path
239+
*/
240+
public function fetch($path) {
241+
$result = $this->result;
242+
$keys = explode(".", $path);
243+
foreach ($keys as $key) {
244+
if (!array_key_exists($key, $result)) {
245+
return null;
246+
}
247+
$result = $result[$key];
248+
}
249+
return $result;
250+
}
251+
252+
public function __get($name) {
253+
return $this->fetch($name);
254+
}
255+
232256
}

src/TypeQuery.php

100644100755
File mode changed.

src/TypeQueryInterface.php

100644100755
File mode changed.

src/TypeQueryTrait.php

100644100755
File mode changed.

tests/BarModel.php

100644100755
File mode changed.

tests/BarTypeQuery.php

100644100755
File mode changed.

tests/FooModel.php

100644100755
File mode changed.

tests/FooTypeQuery.php

100644100755
File mode changed.

tests/TestsIndexQuery.php

100644100755
File mode changed.

tests/case1.php

100644100755
File mode changed.

tests/case2.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/* * ***
4+
* All the examples suppose there is an ES instance with the following
5+
* index/type/doc architecture:
6+
*
7+
* - tests
8+
* - foo
9+
* - foo1: {name: Fooa, id: 1, age: 33, alive: true}
10+
* - foo2: {name: Foob, id: 2, age: 18, alive: false}
11+
* - bar
12+
* - bar1: {name:
13+
* - bar2
14+
*/
15+
16+
require '../vendor/autoload.php';
17+
18+
require_once 'TestsIndexQuery.php';
19+
require_once 'FooTypeQuery.php';
20+
require_once 'BarTypeQuery.php';
21+
require_once 'FooModel.php';
22+
require_once 'BarModel.php';
23+
24+
$result = Tests\FooTypeQuery::query([
25+
"size" => 0,
26+
"aggregations" => [
27+
"alive" => [
28+
"terms" => [
29+
"field" => "alive"
30+
]
31+
]
32+
]
33+
]);
34+
35+
var_dump($result->fetch('aggregations.alive'));

0 commit comments

Comments
 (0)