Skip to content

Commit aa2f7f0

Browse files
committed
Merge branch 'master' into 2.1.2
2 parents de9041a + cb15b4e commit aa2f7f0

File tree

7 files changed

+55
-10
lines changed

7 files changed

+55
-10
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
matrix:
1616
os: ['ubuntu-latest']
17-
php-version: ['7.1', '7.3', '7.4', '8.0']
17+
php-version: ['7.1', '7.3', '7.4', '8.0', '8.1']
1818
phpunit-version: ['latest']
1919
transport: ['', 'PhpHttp']
2020

docs/searchclass.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,15 @@ You can enable weight calculation by setting the `track_scores` option to `true`
219219
$search->trackScores(null); // unset track_scores option
220220
```
221221

222+
### stripBadUtf8()
223+
You can enable removal of bad utf8 characters from the results by setting the `strip_bad_utf8` option to `true`.
224+
225+
```php
226+
$search->stripBadUtf8(true); // enable removal of bad utf8 characters
227+
$search->stripBadUtf8(false); // disable removal of bad utf8 characters
228+
$search->stripBadUtf8(null); // unset strip_bad_utf8 option
229+
```
230+
222231
### profile()
223232

224233
If included, result set will provide query profiling.

src/Manticoresearch/Response.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ public function getResponse()
7070
{
7171
if (null === $this->response) {
7272
$this->response = json_decode($this->string, true);
73-
7473
if (json_last_error() !== JSON_ERROR_NONE) {
75-
throw new RuntimeException('fatal error while trying to decode JSON response');
74+
if (json_last_error() === JSON_ERROR_UTF8 && $this->stripBadUtf8()) {
75+
$this->response = json_decode(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $this->string), true);
76+
} else {
77+
throw new RuntimeException('fatal error while trying to decode JSON response');
78+
}
7679
}
7780

7881
if (empty($this->response)) {
@@ -81,6 +84,15 @@ public function getResponse()
8184
}
8285
return $this->response;
8386
}
87+
88+
/**
89+
* check if strip_bad_utf8 as been set to true
90+
* @return boolean
91+
*/
92+
private function stripBadUtf8()
93+
{
94+
return !empty($this->transportInfo['body']) && !empty($this->transportInfo['body']['strip_bad_utf8']);
95+
}
8496

8597
/*
8698
* Check whenever response has error

src/Manticoresearch/ResultSet.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,27 @@ public function __construct($responseObj)
5555
}
5656
}
5757

58-
public function rewind()
58+
public function rewind(): void
5959
{
6060
$this->position = 0;
6161
}
6262

63-
public function current()
63+
public function current(): ResultHit
6464
{
6565
return new ResultHit($this->array[$this->position]);
6666
}
6767

68-
public function next()
68+
public function next(): void
6969
{
7070
$this->position++;
7171
}
7272

73-
public function valid()
73+
public function valid(): bool
7474
{
7575
return isset($this->array[$this->position]);
7676
}
7777

78-
public function key()
78+
public function key(): int
7979
{
8080
return $this->position;
8181
}
@@ -103,7 +103,7 @@ public function getResponse()
103103
return $this->response;
104104
}
105105

106-
public function count()
106+
public function count(): int
107107
{
108108
return count($this->array);
109109
}

src/Manticoresearch/Results/PercolateResultSet.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
namespace Manticoresearch\Results;
55

66
use Manticoresearch\ResultSet;
7+
use Manticoresearch\ResultHit;
78

89
class PercolateResultSet extends ResultSet
910
{
10-
public function current()
11+
public function current(): ResultHit
1112
{
1213
return new PercolateResultHit($this->array[$this->position]);
1314
}

src/Manticoresearch/Search.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ public function trackScores($trackScores): self
7575
return $this;
7676
}
7777

78+
public function stripBadUtf8($stripBadUtf8): self
79+
{
80+
if (is_null($stripBadUtf8)) {
81+
unset($this->params['strip_bad_utf8']);
82+
} else {
83+
$this->params['strip_bad_utf8'] = (bool)$stripBadUtf8;
84+
}
85+
86+
return $this;
87+
}
88+
7889
/**
7990
* @param string $queryString
8091
* @return $this

test/Manticoresearch/SearchTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,18 @@ public function testTrackScores()
774774
$this->assertGreaterThan(1, $resultHit->getScore());
775775
}
776776
}
777+
778+
public function testStripBadUtf8Compiles()
779+
{
780+
$body = self::$search->stripBadUtf8(true)->compile();
781+
$this->assertTrue($body['strip_bad_utf8']);
782+
783+
$body = self::$search->stripBadUtf8(false)->compile();
784+
$this->assertFalse($body['strip_bad_utf8']);
785+
786+
$body = self::$search->stripBadUtf8(null)->compile();
787+
$this->assertArrayNotHasKey('strip_bad_utf8', $body);
788+
}
777789

778790
public function testResultHitGetData()
779791
{

0 commit comments

Comments
 (0)