Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating from PHP 7.x to PHP 8.x #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Laralastica [![Build Status](https://travis-ci.org/michaeljennings/laralastica.svg?branch=master)](https://travis-ci.org/michaeljennings/laralastica) [![Latest Stable Version](https://poser.pugx.org/michaeljennings/laralastica/v/stable)](https://packagist.org/packages/michaeljennings/laralastica) [![Coverage Status](https://coveralls.io/repos/github/michaeljennings/laralastica/badge.svg?branch=master)](https://coveralls.io/github/michaeljennings/laralastica?branch=master) [![License](https://poser.pugx.org/michaeljennings/laralastica/license)](https://packagist.org/packages/michaeljennings/laralastica)

A laravel 5 package that adds the ability to search eloquent models using elasticsearch results, it also handles
A laravel package that adds the ability to search eloquent models using elasticsearch results, it also handles
indexing and removing documents when you save or delete models.

- [Installation](#installation)
Expand All @@ -18,7 +18,7 @@ indexing and removing documents when you save or delete models.
- [Paginate Results](#paginate-results)
- [The Result Collection](#the-result-collection)

## Upgrading from 3.0 to 3.1+
## Upgrading from 3.0 to 4.0

When hitting the `search` method on a model the query builder will return an instance of `Michaeljennings\Laralastica\Eloquent\ResultCollection` instead of `Illuminate\Database\Eloquent\Collection`.

Expand Down Expand Up @@ -52,14 +52,15 @@ Check the table below to see which version you will need.

|Laralastica|Laravel|Elasticsearch|PHP
|---|---|---|---|
|4.x|^8.x|7.x|^8.0
|3.x|^5.1|6.x|^7.0
|2.x|^5.1|2.x-5.x|^5.5.9|

To install through composer either run `composer require michaeljennings/laralastica` or add the package to you
composer.json.

```php
"michaeljennings/laralastica": "^3.1"
"michaeljennings/laralastica": "^4.0"
```

For Laravel 5.5 and upwards, the service provider and facade will be loaded automatically.
Expand Down Expand Up @@ -293,7 +294,7 @@ You can also set whether the query must, should or must not match the value you
```php
Foo::search(function(Builder $query) {

$query->match('foo', 'bar')->must();
$query->matchQuery('foo', 'bar')->must();
$query->terms('bar', ['baz'])->should();
$query->wildcard('baz', 'qux*', 1.0)->mustNot();

Expand All @@ -305,7 +306,7 @@ You may also chain any Laravel query builder methods before or after searching.
```php
Foo::where('foo', 'bar')->search(function(Builder $query) {

$query->match('foo', 'bar');
$query->matchQuery('foo', 'bar');

})->orderBy('baz')->get();
```
Expand Down Expand Up @@ -435,7 +436,7 @@ Each of the queries can optionally be passed a callback as the final parameter w
$laralastica->search('foo', function($query) {

$query->bool(function($query) {
$query->match('foo', 'bar');
$query->matchQuery('foo', 'bar');
});

});
Expand Down Expand Up @@ -482,9 +483,9 @@ $laralastica->search('foo', function($query) {
```php
$laralastica->search('foo', function($query) {

$query->match('baz', 'qux');
$query->match('baz', 'qux', function($matchQuery) {
$matchQuery->setFieldBoost('foo');
$query->matchQuery('baz', 'qux');
$query->matchQuery('baz', 'qux', function($query) {
$query->setFieldBoost('foo');
});

});
Expand Down
17 changes: 12 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
}
],
"require": {
"php": "^7.0",
"ruflin/elastica": "^6.0",
"php": "^7.0 || ^8.0",
"ruflin/elastica": "^7.2.0",
"illuminate/database": ">=5.0",
"illuminate/events": ">=5.0",
"illuminate/http": ">=5.0",
Expand All @@ -25,10 +25,12 @@
"illuminate/bus": ">=5.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"mockery/mockery": "^1.0",
"orchestra/testbench": "^3.0",
"orchestra/testbench": ">=3.0",
"php-coveralls/php-coveralls": "^2.1",
"orchestra/database": "^3.0"
"orchestra/database": ">=3.0",
"laravel/legacy-factories": "^1.3"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -57,5 +59,10 @@
}
}
},
"minimum-stability": "stable"
"minimum-stability": "stable",
"config": {
"allow-plugins": {
"kylekatarnls/update-helper": false
}
}
}
2 changes: 1 addition & 1 deletion src/Contracts/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function fuzzy(string $field, string $value, callable $callback = null);
* @param callable|null $callback
* @return mixed
*/
public function match(string $field = null, string $value = null, callable $callback = null);
public function matchQuery(string $field = null, string $value = null, callable $callback = null);

/**
* Create a new match phrase query.
Expand Down
20 changes: 13 additions & 7 deletions src/Drivers/ElasticaDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Elastica\Query\Common;
use Elastica\Query\Exists;
use Elastica\Query\Fuzzy;
use Elastica\Query\Match;
use Elastica\Query\MatchQuery;
use Elastica\Query\MatchAll;
use Elastica\Query\MatchPhrase;
use Elastica\Query\MatchPhrasePrefix;
Expand Down Expand Up @@ -233,7 +233,7 @@ protected function addDocumentsToIndex(string $index, array $documents)
//
// See: https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html
foreach ($documents as $document) {
$document->setType($index);
$document->setIndex($index);
}

$index = $this->getIndex($this->getIndexName($index));
Expand Down Expand Up @@ -344,11 +344,13 @@ public function fuzzy(string $field, string $value, callable $callback = null)
* @param callable|null $callback
* @return AbstractQuery
*/
public function match(string $field = null, string $value = null, callable $callback = null)
public function matchQuery(string $field = null, string $value = null, callable $callback = null)
{
$query = new Match();
$query = new MatchQuery();

$query->setFieldQuery($field, $value);
if (!is_null($field) && !is_null($value)) {
$query->setFieldQuery($field, $value);
}

return $this->returnQuery($query, $callback);
}
Expand All @@ -367,7 +369,9 @@ public function matchPhrase(string $field = null, string $value = null, callable
{
$query = new MatchPhrase();

$query->setFieldQuery($field, $value);
if (!is_null($field) && !is_null($value)) {
$query->setFieldQuery($field, $value);
}

return $this->returnQuery($query, $callback);
}
Expand All @@ -386,7 +390,9 @@ public function matchPhrasePrefix(string $field = null, string $value = null, ca
{
$query = new MatchPhrasePrefix();

$query->setFieldQuery($field, $value);
if (!is_null($field) && !is_null($value)) {
$query->setFieldQuery($field, $value);
}

return $this->returnQuery($query, $callback);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/NullDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function fuzzy(string $field, string $value, callable $callback = null)
* @param callable|null $callback
* @return mixed
*/
public function match(string $field = null, string $value = null, callable $callback = null)
public function matchQuery(string $field = null, string $value = null, callable $callback = null)
{
//
}
Expand Down
6 changes: 4 additions & 2 deletions src/IndexPrefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Michaeljennings\Laralastica;

use Illuminate\Support\Str;

class IndexPrefixer
{
/**
Expand All @@ -12,10 +14,10 @@ class IndexPrefixer
*/
public function prefix(string $index)
{
if ( ! starts_with($index, config('laralastica.index_prefix'))) {
if ( ! Str::startsWith($index, config('laralastica.index_prefix'))) {
return config('laralastica.index_prefix') . $index;
}

return $index;
}
}
}
6 changes: 3 additions & 3 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Michaeljennings\Laralastica\Tests;

use Elastica\Query\Exists;
use Elastica\Query\Match;
use Elastica\Query\MatchQuery;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Michaeljennings\Laralastica\Builder;
use Michaeljennings\Laralastica\ClientManager;
Expand All @@ -27,7 +27,7 @@ public function it_dynamically_calls_methods_on_the_driver()
{
$builder = $this->makeBuilder();

$query = $builder->match('foo', 'bar');
$query = $builder->matchQuery('foo', 'bar');

$this->assertInstanceOf(Query::class, $query);
}
Expand All @@ -36,7 +36,7 @@ public function it_dynamically_calls_methods_on_the_driver()
public function it_adds_a_raw_query()
{
$builder = $this->makeBuilder();
$query = new Match();
$query = new MatchQuery();

$query = $builder->query($query);

Expand Down
20 changes: 10 additions & 10 deletions tests/ElasticaDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use Elastica\Query\Common;
use Elastica\Query\Exists;
use Elastica\Query\Fuzzy;
use Elastica\Query\Match;
use Elastica\Query\MatchAll;
use Elastica\Query\MatchPhrase;
use Elastica\Query\MatchPhrasePrefix;
use Elastica\Query\MatchQuery;
use Elastica\Query\MultiMatch;
use Elastica\Query\QueryString;
use Elastica\Query\Range;
Expand Down Expand Up @@ -105,20 +105,20 @@ public function it_returns_a_fuzzy_query_and_runs_a_callback_on_it()
public function it_returns_a_match_query()
{
$driver = $this->makeDriver();
$query = $driver->match('foo', 'bar');
$query = $driver->matchQuery('foo', 'bar');

$this->assertInstanceOf(Match::class, $query);
$this->assertInstanceOf(MatchQuery::class, $query);
}

/** @test */
public function it_returns_a_match_query_and_runs_a_callback_on_it()
{
$driver = $this->makeDriver();
$query = $driver->match(null, null, function($query) {
$query = $driver->matchQuery(null, null, function($query) {
$query->setFieldBoost('foo');
});

$this->assertInstanceOf(Match::class, $query);
$this->assertInstanceOf(MatchQuery::class, $query);
$this->assertEquals(1, $query->toArray()['match']['foo']['boost']);
}

Expand Down Expand Up @@ -281,11 +281,11 @@ public function it_returns_a_terms_query_and_run_a_callback_on_it()
{
$driver = $this->makeDriver();
$query = $driver->terms('foo', ['bar', 'baz'], function($query) {
$query->setMinimumMatch(5);
$query->setBoost(2.0);
});

$this->assertInstanceOf(Terms::class, $query);
$this->assertEquals(5, $query->toArray()['terms']['minimum_match']);
$this->assertEquals(2.0, $query->toArray()['terms']['boost']);
}

/** @test */
Expand Down Expand Up @@ -689,7 +689,7 @@ public function it_sets_the_type_of_query_to_must()
],
]);

$query = $driver->match('foo', 'bar');
$query = $driver->matchQuery('foo', 'bar');
$query = new Query($query);

$this->assertInstanceOf(Query::class, $query->must());
Expand All @@ -716,7 +716,7 @@ public function it_sets_the_type_of_query_to_should()
],
]);

$query = $driver->match('foo', 'bar');
$query = $driver->matchQuery('foo', 'bar');
$query = new Query($query);

$this->assertInstanceOf(Query::class, $query->should());
Expand All @@ -743,7 +743,7 @@ public function it_sets_the_type_of_query_to_must_not()
],
]);

$query = $driver->match('foo', 'bar');
$query = $driver->matchQuery('foo', 'bar');
$query = new Query($query);

$this->assertInstanceOf(Query::class, $query->mustNot());
Expand Down
2 changes: 1 addition & 1 deletion tests/NullDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function it_returns_null_when_creating_a_match_query()
{
$driver = $this->makeDriver();

$this->assertNull($driver->match());
$this->assertNull($driver->matchQuery());
}

/** @test */
Expand Down
7 changes: 4 additions & 3 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Michaeljennings\Laralastica\Tests;

use Elastica\Query\Match;
use Elastica\Query\MatchQuery;
use Michaeljennings\Laralastica\Query;

class QueryTest extends TestCase
Expand Down Expand Up @@ -47,11 +48,11 @@ public function it_gets_the_raw_query()
{
$query = $this->makeQuery();

$this->assertInstanceOf(Match::class, $query->getQuery());
$this->assertInstanceOf(MatchQuery::class, $query->getQuery());
}

protected function makeQuery()
{
return new Query(new Match());
return new Query(new MatchQuery());
}
}
}
6 changes: 3 additions & 3 deletions tests/SearchSoftDeletesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function it_gets_the_search_results()
$shouldNotMatch = factory(TestSoftDeleteModel::class)->create(['name' => 'Test', 'deleted_at' => (string) new Carbon()]);

$results = TestSoftDeleteModel::search(function($builder) {
$builder->match('name', 'Test', function($query) {
$builder->matchQuery('name', 'Test', function($query) {
$query->setFieldFuzziness('name', 2);
});
})->get();
Expand All @@ -135,7 +135,7 @@ public function it_searches_only_trashed()
$shouldNotMatch = factory(TestSoftDeleteModel::class)->create(['name' => 'Test']);

$results = TestSoftDeleteModel::searchOnlyTrashed(function($builder) {
$builder->match('name', 'Test', function($query) {
$builder->matchQuery('name', 'Test', function($query) {
$query->setFieldFuzziness('name', 2);
});
})->get();
Expand All @@ -156,7 +156,7 @@ public function it_searches_with_trashed()
$notTrashed = factory(TestSoftDeleteModel::class)->create(['name' => 'Test']);

$results = TestSoftDeleteModel::searchWithTrashed(function($builder) {
$builder->match('name', 'Test', function($query) {
$builder->matchQuery('name', 'Test', function($query) {
$query->setFieldFuzziness('name', 2);
});
})->get();
Expand Down
4 changes: 2 additions & 2 deletions tests/SearchableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function it_gets_the_search_results()
factory(TestModel::class)->create(['name' => 'Test']);

$results = TestModel::search(function($builder) {
$builder->match('name', 'Test', function($query) {
$builder->matchQuery('name', 'Test', function($query) {
$query->setFieldFuzziness('name', 2);
});
})->get();
Expand All @@ -136,7 +136,7 @@ public function it_paginates_the_search_results()
factory(TestModel::class)->create(['name' => 'Test']);

$results = TestModel::search(function($builder) {
$builder->match('name', 'Test', function($query) {
$builder->matchQuery('name', 'Test', function($query) {
$query->setFieldFuzziness('name', 2);
});
})->paginate(1);
Expand Down