Breaks x,y,z and x+y+z into an array. Works for strings.
string $str: The string to parse.
Drupal\views\Plugin\views\HandlerBase|null $handler: The object to use as a base. If not specified one will be created.
Drupal\views\Plugin\views\HandlerBase|stdClass $handler The new handler object.
public static function breakPhraseString($str, &$handler = NULL) {
if (!$handler) {
$handler = new \stdClass();
}
// Set up defaults:
if (!isset($handler->value)) {
$handler->value = array();
}
if (!isset($handler->operator)) {
$handler->operator = 'or';
}
if ($str == '') {
return $handler;
}
// Determine if the string has 'or' operators (plus signs) or 'and' operators
// (commas) and split the string accordingly. If we have an 'and' operator,
// spaces are treated as part of the word being split, but otherwise they are
// treated the same as a plus sign.
$or_wildcard = '[^\\s+,]';
$and_wildcard = '[^+,]';
if (preg_match("/^({$or_wildcard}+[+ ])+{$or_wildcard}+\$/", $str)) {
$handler->operator = 'or';
$handler->value = preg_split('/[+ ]/', $str);
}
elseif (preg_match("/^({$and_wildcard}+,)*{$and_wildcard}+\$/", $str)) {
$handler->operator = 'and';
$handler->value = explode(',', $str);
}
// Keep an 'error' value if invalid strings were given.
if (!empty($str) && (empty($handler->value) || !is_array($handler->value))) {
$handler->value = array(
-1,
);
return $handler;
}
// Doubly ensure that all values are strings only.
foreach ($handler->value as $id => $value) {
$handler->value[$id] = (string) $value;
}
return $handler;
}