Skip to content

Commit 4b505ff

Browse files
authored
Merge pull request #32 from dfeyer/bugfix/support-array-as-sugestion-input
BUGFIX: Support Array as Suggestion input
2 parents 872f362 + 328bfc5 commit 4b505ff

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

Classes/EelHelper/SuggestionIndexHelper.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* source code.
1212
*/
1313

14+
use Flowpack\SearchPlugin\Exception;
1415
use Neos\Eel\ProtectedContextAwareInterface;
1516
use Neos\Flow\Annotations as Flow;
1617

@@ -21,9 +22,8 @@
2122
*/
2223
class SuggestionIndexHelper implements ProtectedContextAwareInterface
2324
{
24-
2525
/**
26-
* @param string $input The input to store, this can be a an array of strings or just a string. This field is mandatory.
26+
* @param string|array $input The input to store, this can be a an array of strings or just a string. This field is mandatory.
2727
* @param string $output The result is de-duplicated if several documents have the same output, i.e. only one is returned as part of the suggest result.
2828
* @param array $payload An arbitrary JSON object, which is simply returned in the suggest option.
2929
* @param int $weight A positive integer or a string containing a positive integer, which defines a weight and allows you to rank your suggestions.
@@ -40,13 +40,30 @@ public function build($input, $output = '', array $payload = [], $weight = 1)
4040
}
4141

4242
/**
43-
* @param string $input
43+
* @param string|array $input
4444
* @return array
45+
* @throws Exception
4546
*/
4647
protected function prepareInput($input)
4748
{
48-
$input = preg_replace( "/\r|\n/", '', $input);
49-
return array_values(array_filter(explode(' ', preg_replace("/[^[:alnum:][:space:]]/u", ' ', strip_tags($input)))));
49+
$process = function ($input) {
50+
if (!\is_string($input)) {
51+
throw new Exception('Only string are supported as input', 1512733297);
52+
}
53+
$input = preg_replace("/\r|\n/", '', $input);
54+
return array_values(array_filter(explode(' ', preg_replace("/[^[:alnum:][:space:]]/u", ' ', strip_tags($input)))));
55+
};
56+
if (\is_string($input)) {
57+
return $process($input);
58+
} elseif (\is_array($input)) {
59+
$data = [];
60+
foreach (array_map($process, $input) as $values) {
61+
$data = \array_merge($data, $values);
62+
}
63+
return $data;
64+
} else {
65+
throw new Exception('Only string or array are supported as input', 1512733287);
66+
}
5067
}
5168

5269
/**

Classes/Exception.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Flowpack\SearchPlugin;
4+
5+
/*
6+
* This file is part of the Flowpack.SearchPlugin package.
7+
*
8+
* (c) Contributors of the Flowpack Team - flowpack.org
9+
*
10+
* This package is Open Source Software. For the full copyright and license
11+
* information, please view the LICENSE file which was distributed with this
12+
* source code.
13+
*/
14+
15+
class Exception extends \Neos\Flow\Exception
16+
{
17+
18+
}

0 commit comments

Comments
 (0)