|
21 | 21 |
|
22 | 22 | namespace MacFJA\RedisSearch\Index; |
23 | 23 |
|
| 24 | +use function array_map; |
| 25 | +use function array_shift; |
| 26 | +use function in_array; |
24 | 27 | use MacFJA\RedisSearch\Helper\DataHelper; |
| 28 | +use MacFJA\RedisSearch\Helper\RedisHelper; |
| 29 | +use MacFJA\RedisSearch\Index\Builder\Field; |
| 30 | +use MacFJA\RedisSearch\Index\Builder\GeoField; |
| 31 | +use MacFJA\RedisSearch\Index\Builder\NumericField; |
| 32 | +use MacFJA\RedisSearch\Index\Builder\TagField; |
| 33 | +use MacFJA\RedisSearch\Index\Builder\TextField; |
| 34 | +use function sprintf; |
| 35 | +use UnexpectedValueException; |
25 | 36 |
|
26 | 37 | class InfoResult |
27 | 38 | { |
@@ -127,6 +138,51 @@ public function getFields(): array |
127 | 138 | return $this->fields; |
128 | 139 | } |
129 | 140 |
|
| 141 | + /** |
| 142 | + * @return array<Field> |
| 143 | + */ |
| 144 | + public function getFieldsAsObject(): array |
| 145 | + { |
| 146 | + return array_map(function (array $field): Field { |
| 147 | + $name = (string) array_shift($field); |
| 148 | + array_shift($field); // type |
| 149 | + $type = (string) array_shift($field); |
| 150 | + |
| 151 | + switch ($type) { |
| 152 | + case 'GEO': |
| 153 | + return new GeoField( |
| 154 | + $name, |
| 155 | + in_array('NOINDEX', $field, true) |
| 156 | + ); |
| 157 | + case 'NUMERIC': |
| 158 | + return new NumericField( |
| 159 | + $name, |
| 160 | + in_array('SORTABLE', $field, true), |
| 161 | + in_array('NOINDEX', $field, true) |
| 162 | + ); |
| 163 | + case 'TAG': |
| 164 | + return new TagField( |
| 165 | + $name, |
| 166 | + RedisHelper::getValue($field, 'SEPARATOR'), |
| 167 | + in_array('SORTABLE', $field, true), |
| 168 | + in_array('NOINDEX', $field, true) |
| 169 | + ); |
| 170 | + case 'TEXT': |
| 171 | + return new TextField( |
| 172 | + $name, |
| 173 | + in_array('NOSTEM', $field, true), |
| 174 | + /** @phan-suppress-next-line PhanPartialTypeMismatchArgument */ |
| 175 | + DataHelper::nullOrCast(RedisHelper::getValue($field, 'WEIGHT'), 'float'), |
| 176 | + RedisHelper::getValue($field, 'PHONETIC'), |
| 177 | + in_array('SORTABLE', $field, true), |
| 178 | + in_array('NOINDEX', $field, true) |
| 179 | + ); |
| 180 | + default: |
| 181 | + throw new UnexpectedValueException(sprintf('Unknown field type "%s" for field "%s"', $type, $name)); |
| 182 | + } |
| 183 | + }, $this->fields); |
| 184 | + } |
| 185 | + |
130 | 186 | public function getDocumentCount(): int |
131 | 187 | { |
132 | 188 | return $this->documentCount; |
|
0 commit comments