diff --git a/.travis.yml b/.travis.yml index cdc78f4..014685e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ language: php php: - 5.5 - 5.6 + - 7.0 include: - php: 5.5 diff --git a/src/ElasticquentMultiSearch.php b/src/ElasticquentMultiSearch.php new file mode 100644 index 0000000..ee3cbd7 --- /dev/null +++ b/src/ElasticquentMultiSearch.php @@ -0,0 +1,86 @@ +indices = [$this->TraitGetIndexName()]; + } + + /** + * Set indcies to search from + * @param mixed $indices + */ + public function setIndexName($indices) + { + $this->indices = $indices; + return $this; + } + + public function getIndexName() + { + return $this->indices; + } + + /** + * Set types to search from + * @param mixed $types + */ + public function setTypeName($types) + { + $this->types = $types; + return $this; + + } + + public function getTypeName() + { + return $this->types; + } + + /** + * Create a elacticquent result collection of models from plain arrays. + * + * Use _type to instantiate models + * + * @param array $items + * @param array $meta + * @return \Elasticquent\ElasticquentResultCollection + */ + public static function hydrateElasticquentResult(array $items, $meta = null) + { + // Cache instances + $instances = []; + + $results = []; + + foreach ($items as $item) { + $className = $item['_type']; + if (!class_exists($className)) { + continue; + } + if (!isset($instances[$className])) { + $instances[$className] = new $className; + } + $results[] = $instances[$className]->newFromHitBuilder($item); + } + + return (new static)->newElasticquentResultCollection($results, $meta); + } +} diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index a23e7c4..c08429c 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -71,7 +71,7 @@ public function newCollection(array $models = array()) */ public function getTypeName() { - return $this->getTable(); + return get_class($this); } /** diff --git a/tests/ElasticSearchMethodsTest.php b/tests/ElasticSearchMethodsTest.php index 2fbd0a9..0ac565e 100644 --- a/tests/ElasticSearchMethodsTest.php +++ b/tests/ElasticSearchMethodsTest.php @@ -12,9 +12,9 @@ * specifically returns results consistent with the ElasticSearch PHP client version * 2.0 documentation. * - * The Elasticquent method will then format the response and we test that the resulting - * Elasticquent results collection methods return the results we expect to verify this. - */ + * The Elasticquent method will then format the response and we test that the resulting + * Elasticquent results collection methods return the results we expect to verify this. + */ class ElasticSearchMethodsTest extends PHPUnit_Framework_TestCase { @@ -24,7 +24,7 @@ class ElasticSearchMethodsTest extends PHPUnit_Framework_TestCase 'hits' => [ [ '_index' => 'my_custom_index_name', - '_type' => 'test_table', + '_type' => 'SearchTestModel', '_score' => 0.7768564, '_source' => [ 'name' => 'foo', @@ -32,7 +32,7 @@ class ElasticSearchMethodsTest extends PHPUnit_Framework_TestCase ], [ '_index' => 'my_custom_index_name', - '_type' => 'test_table', + '_type' => 'SearchTestModel', '_score' => 0.5634561, '_source' => [ 'name' => 'bar', diff --git a/tests/ElasticquentTraitTest.php b/tests/ElasticquentTraitTest.php index 5939857..6d57b68 100644 --- a/tests/ElasticquentTraitTest.php +++ b/tests/ElasticquentTraitTest.php @@ -20,7 +20,7 @@ public function setup() */ public function testTypeNameInferredFromTableName() { - $this->assertEquals('test_table', $this->model->getTypeName()); + $this->assertEquals('TestModel', $this->model->getTypeName()); } /** diff --git a/tests/stubs/parameters.php b/tests/stubs/parameters.php index 557b1b9..5c7d567 100644 --- a/tests/stubs/parameters.php +++ b/tests/stubs/parameters.php @@ -8,7 +8,7 @@ function basicParameters() { return [ 'index' => 'my_custom_index_name', - 'type' => 'test_table', + 'type' => 'SearchTestModel', ]; } diff --git a/tests/stubs/results.php b/tests/stubs/results.php index 6283a83..744e971 100644 --- a/tests/stubs/results.php +++ b/tests/stubs/results.php @@ -20,7 +20,7 @@ function successfulResults() 'hits' => [ [ '_index' => 'my_custom_index_name', - '_type' => 'test_table', + '_type' => 'SearchTestModel', '_score' => 0.7768564, '_source' => [ 'name' => 'foo', @@ -28,7 +28,7 @@ function successfulResults() ], [ '_index' => 'my_custom_index_name', - '_type' => 'test_table', + '_type' => 'SearchTestModel', '_score' => 0.5634561, '_source' => [ 'name' => 'bar',