Skip to content

Commit 5d156fc

Browse files
committed
add boot method
2 parents 244d553 + 220753c commit 5d156fc

File tree

4 files changed

+51
-75
lines changed

4 files changed

+51
-75
lines changed

src/Collection.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Support\Collection as BaseCollection;
66

7-
87
class Collection extends BaseCollection
98
{
109

@@ -19,16 +18,4 @@ public function toJson($options = 0)
1918
return json_encode($this->toArray(), $options);
2019
}
2120

22-
/**
23-
* Get the collection of items as Array.
24-
*
25-
* @return string
26-
*/
27-
public function toArray()
28-
{
29-
return array_map(function($item){
30-
return $item->toArray();
31-
}, $this->items);
32-
}
33-
3421
}

src/Connection.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Basemkhirat\Elasticsearch;
44

5+
use Elasticsearch\Client;
56
use Elasticsearch\ClientBuilder;
7+
use Symfony\Component\HttpKernel\Exception\HttpException as HttpException;
68

79
/**
810
* Class Connection
@@ -13,7 +15,7 @@ class Connection
1315

1416
/**
1517
* Laravel app instance
16-
* @var \Illuminate\Foundation\Application|mixed
18+
* @var \Illuminate\contracts\Foundation\Application|mixed
1719
*/
1820
protected $app;
1921

@@ -25,13 +27,13 @@ class Connection
2527

2628
/**
2729
* The current connection
28-
* @var
30+
* @var Client
2931
*/
3032
protected $connection;
3133

3234
/**
3335
* all available connections
34-
* @var array
36+
* @var Client[]
3537
*/
3638
protected $connections = [];
3739

@@ -77,11 +79,11 @@ public static function create($config)
7779
/**
7880
* Create a connection for laravel or lumen frameworks
7981
* @param $name
82+
* @throws HttpException
8083
* @return Query
8184
*/
8285
function connection($name)
8386
{
84-
8587
// Check if connection is already loaded.
8688

8789
if ($this->isLoaded($name)) {
@@ -121,6 +123,16 @@ function connection($name)
121123
}
122124

123125

126+
/**
127+
* @param string $name
128+
* @throws HttpException
129+
* @return Query
130+
*/
131+
function connectionByName($name = 'default') {
132+
return $this->connection($this->config[$name]);
133+
}
134+
135+
124136
/**
125137
* route the request to the query class
126138
* @param $connection
@@ -148,35 +160,22 @@ function newQuery($connection)
148160
function isLoaded($name)
149161
{
150162

151-
if (array_key_exists($name, $this->connections)) {
152-
return true;
153-
}
154-
155-
return false;
163+
return array_key_exists($name, $this->connections);
156164
}
157165

158166

159167
/**
160168
* Set the default connection
161169
* @param $name
162170
* @param $arguments
171+
* @throws HttpException
163172
* @return mixed
164173
*/
165174
function __call($name, $arguments)
166175
{
167-
if (method_exists($this, $name)) {
168-
169-
return call_user_func_array([$this, $name], $arguments);
170-
171-
} else {
172176

173-
// if no connection, use default.
177+
$query = $this->connectionByName('default');
174178

175-
$query = $this->connection($this->config["default"]);
176-
177-
return call_user_func_array([$query, $name], $arguments);
178-
179-
}
179+
return call_user_func_array([$query, $name], $arguments);
180180
}
181-
182181
}

src/ElasticsearchServiceProvider.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ public function boot()
7373
// Laravel Scout service provider was not loaded yet.
7474

7575
}
76-
7776
}
78-
79-
8077
}
8178

8279
/**

src/Query.php

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
use Basemkhirat\Elasticsearch\Classes\Bulk;
66
use Basemkhirat\Elasticsearch\Classes\Search;
7-
use Basemkhirat\Elasticsearch\Collection;
7+
use Elasticsearch\Client;
8+
use Elasticsearch\Common\Exceptions\BadMethodCallException;
89

910

1011
/**
@@ -16,13 +17,13 @@ class Query
1617

1718
/**
1819
* Native elasticsearch connection instance
19-
* @var Connection
20+
* @var Client
2021
*/
2122
public $connection;
2223

2324
/**
2425
* Ignored HTTP errors
25-
* @var array
26+
* @var string[]
2627
*/
2728
public $ignores = [];
2829

@@ -157,14 +158,14 @@ class Query
157158

158159
/**
159160
* Elastic model instance
160-
* @var \Basemkhirat\Elasticsearch\Model
161+
* @var Model
161162
*/
162163
public $model;
163164

164165

165166
/**
166167
* Query constructor.
167-
* @param $connection
168+
* @param Client|NULL $connection
168169
*/
169170
function __construct($connection = NULL)
170171
{
@@ -196,7 +197,7 @@ public function getIndex()
196197
/**
197198
* Set the type name
198199
* @param $type
199-
* @return $this
200+
* @return Query $this
200201
*/
201202
public function type($type)
202203
{
@@ -256,7 +257,7 @@ public function searchType($type)
256257

257258
/**
258259
* get the query search type
259-
* @return $this
260+
* @return int
260261
*/
261262
public function getSearchType()
262263
{
@@ -265,7 +266,7 @@ public function getSearchType()
265266

266267
/**
267268
* Get the query scroll
268-
* @return $this
269+
* @return string
269270
*/
270271
public function getScroll()
271272
{
@@ -362,11 +363,7 @@ public function orderBy($field, $direction = "asc")
362363
protected function isOperator($string)
363364
{
364365

365-
if (in_array($string, $this->operators)) {
366-
return true;
367-
}
368-
369-
return false;
366+
return in_array($string, $this->operators);
370367
}
371368

372369
/**
@@ -656,6 +653,7 @@ public function distance($name, $value, $distance)
656653
/**
657654
* Search the entire document fields
658655
* @param null $q
656+
* @param NULL $settings
659657
* @return $this
660658
*/
661659
public function search($q = NULL, $settings = NULL)
@@ -792,22 +790,21 @@ public function clear($scroll_id = NULL)
792790

793791
/**
794792
* Get the collection of results
795-
* @param string $scroll_id
796-
* @return array|Collection
793+
* @throws \Exception
794+
* @return Collection
797795
*/
798-
public function get($scroll_id = NULL)
796+
public function get()
799797
{
800798

801-
$scroll_id = NULL;
802-
803-
$result = $this->getResult($scroll_id);
799+
$result = $this->getResult(NULL);
804800

805801
return $this->getAll($result);
806802
}
807803

808804
/**
809805
* Get the first object of results
810806
* @param string $scroll_id
807+
* @throws \Exception
811808
* @return object
812809
*/
813810
public function first($scroll_id = NULL)
@@ -823,6 +820,7 @@ public function first($scroll_id = NULL)
823820
/**
824821
* Get query result
825822
* @param $scroll_id
823+
* @throws \Exception
826824
* @return mixed
827825
*/
828826
protected function getResult($scroll_id)
@@ -846,7 +844,8 @@ protected function getResult($scroll_id)
846844
/**
847845
* Get non cached results
848846
* @param null $scroll_id
849-
* @return mixed
847+
* @throws \Exception
848+
* @return array
850849
*/
851850
public function response($scroll_id = NULL)
852851
{
@@ -909,7 +908,7 @@ function setModel($model)
909908
/**
910909
* Retrieve all records
911910
* @param array $result
912-
* @return array|Collection
911+
* @return Collection
913912
*/
914913
protected function getAll($result = [])
915914
{
@@ -993,6 +992,7 @@ protected function getFirst($result = [])
993992
* @param int $per_page
994993
* @param $page_name
995994
* @param null $page
995+
* @throws \Exception
996996
* @return Pagination
997997
*/
998998
public function paginate($per_page = 10, $page_name = "page", $page = null)
@@ -1013,7 +1013,7 @@ public function paginate($per_page = 10, $page_name = "page", $page = null)
10131013
* Insert a document
10141014
* @param $data
10151015
* @param null $_id
1016-
* @return object
1016+
* @return array
10171017
*/
10181018
public function insert($data, $_id = NULL)
10191019
{
@@ -1039,12 +1039,12 @@ public function insert($data, $_id = NULL)
10391039
$parameters["id"] = $this->_id;
10401040
}
10411041

1042-
return (object)$this->connection->index($parameters);
1042+
return $this->connection->index($parameters);
10431043
}
10441044

10451045
/**
10461046
* Insert a bulk of documents
1047-
* @param $data multidimensional array of [id => data] pairs
1047+
* @param $data callable|array : multidimensional array of [id => data] pairs
10481048
* @return object
10491049
*/
10501050
public function bulk($data)
@@ -1321,7 +1321,6 @@ public function CachePrefix($prefix)
13211321

13221322
/**
13231323
* Get a unique cache key for the complete query.
1324-
*
13251324
* @return string
13261325
*/
13271326
public function getCacheKey()
@@ -1357,7 +1356,7 @@ public function remember($minutes, $key = null)
13571356
/**
13581357
* Indicate that the query results should be cached forever.
13591358
* @param string $key
1360-
* @return \Illuminate\Database\Query\Builder|static
1359+
* @return $this
13611360
*/
13621361
public function rememberForever($key = null)
13631362
{
@@ -1372,20 +1371,14 @@ public function rememberForever($key = null)
13721371
function __call($method, $parameters)
13731372
{
13741373

1375-
if (method_exists($this, $method)) {
1376-
return $this->$method(...$parameters);
1377-
} else {
1378-
1379-
// Check for model scopes
1374+
$method = "scope" . ucfirst($method);
13801375

1381-
$method = "scope" . ucfirst($method);
1382-
1383-
if (method_exists($this->model, $method)) {
1384-
$parameters = array_merge([$this], $parameters);
1385-
$this->model->$method(...$parameters);
1386-
return $this;
1387-
}
1376+
if (method_exists($this->model, $method)) {
1377+
$parameters = array_merge([$this], $parameters);
1378+
$this->model->$method(...$parameters);
1379+
return $this;
13881380
}
13891381

1382+
throw new BadMethodCallException("Method $method does not exist.");
13901383
}
13911384
}

0 commit comments

Comments
 (0)