Skip to content

Commit c3ece8f

Browse files
committedJan 20, 2021
Fix prefixes index information + Syntax error detection
1 parent 4bf7b72 commit c3ece8f

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed
 

‎src/Helper/RedisHelper.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ public static function buildQueryPartial(array $query, array $partials): array
108108
}
109109

110110
/**
111-
* @param array<float|int|string> $notKeyedArray
111+
* @param array<float|int|mixed|string> $notKeyedArray
112112
*
113-
* @return array<string,float|int|string>
113+
* @return array<string,mixed>
114114
*/
115115
public static function getPairs(array $notKeyedArray): array
116116
{

‎src/Index.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use function array_keys;
2525
use function array_map;
2626
use function array_merge;
27+
use function is_array;
2728
use function is_string;
2829
use MacFJA\RediSearch\Helper\DataHelper;
2930
use MacFJA\RediSearch\Helper\RedisHelper;
@@ -146,11 +147,20 @@ public function getStats(): InfoResult
146147
DataHelper::handleRawResult($result);
147148

148149
$stats = RedisHelper::getPairs($result);
150+
$definitions = RedisHelper::getPairs($stats['index_definition'] ?? []);
151+
$definitions = array_map(function ($item) {
152+
if (!is_array($item)) {
153+
return (string) $item;
154+
}
155+
156+
return $item;
157+
}, $definitions);
158+
$definitions['prefixes'] = array_map('strval', (array) ($definitions['prefixes'] ?? []));
149159

150160
return new InfoResult(
151161
(string) $stats['index_name'],
152162
array_map('strval', (array) ($stats['index_options'] ?? [])),
153-
array_map('strval', (array) ($stats['index_definition'] ?? [])),
163+
$definitions,
154164
(array) ($stats['fields'] ?? []),
155165
(int) $stats['num_docs'],
156166
(int) $stats['max_doc_id'],

‎src/Index/InfoResult.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424
use function array_map;
2525
use function array_shift;
26+
use function assert;
2627
use function in_array;
28+
use function is_array;
2729
use MacFJA\RediSearch\Helper\DataHelper;
2830
use MacFJA\RediSearch\Helper\RedisHelper;
2931
use MacFJA\RediSearch\Index\Builder\Field;
@@ -45,7 +47,7 @@ class InfoResult
4547
/** @var array<string> */
4648
private $indexOptions = [];
4749

48-
/** @var array<string> */
50+
/** @var array<array<string>|string> */
4951
private $definition = [];
5052

5153
/** @var array<mixed> */
@@ -81,18 +83,26 @@ class InfoResult
8183
/**
8284
* InfoResult constructor.
8385
*
84-
* @param array<string> $indexOptions
85-
* @param array<string> $definition
86-
* @param array<mixed> $fields
87-
* @param array<string> $stopWords
86+
* @param array<string> $indexOptions
87+
* @param array<array<string>|string> $definition
88+
* @param array<mixed> $fields
89+
* @param array<string> $stopWords
8890
*
8991
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
9092
*/
9193
public function __construct(string $indexName, array $indexOptions, array $definition, array $fields, int $documentCount, int $maxDocumentId, int $termsCount, float $averageRecordSize, float $indexSize, int $indexBlockCount, bool $indexing, int $indexingFailureCount, float $percentIndexed, array $stopWords)
9294
{
95+
/** @var array<string> $prefixes */
96+
$prefixes = $definition['prefixes'] ?? [];
97+
assert(is_array($prefixes));
98+
unset($definition['prefixes']);
99+
/** @var array<string> $definition */
93100
DataHelper::assertArrayOf($indexOptions, 'string');
94101
DataHelper::assertArrayOf($definition, 'string');
102+
DataHelper::assertArrayOf($prefixes, 'string');
95103
DataHelper::assertArrayOf($stopWords, 'string');
104+
$definition['prefixes'] = $prefixes;
105+
96106
$this->indexName = $indexName;
97107
$this->indexOptions = $indexOptions;
98108
$this->definition = $definition;
@@ -123,7 +133,7 @@ public function getIndexOptions(): array
123133
}
124134

125135
/**
126-
* @return array<string>
136+
* @return array<array<string>|string>
127137
*/
128138
public function getDefinition(): array
129139
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright MacFJA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
10+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
14+
* Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
namespace MacFJA\RediSearch\Search\Exception;
23+
24+
use RuntimeException;
25+
26+
class SyntaxErrorException extends RuntimeException
27+
{
28+
}

0 commit comments

Comments
 (0)