1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-13 10:15:28 +02:00

Add a Selectors::getOperators() method that provides a lot of options for retrieving info about available operators

This commit is contained in:
Ryan Cramer
2020-04-10 12:42:16 -04:00
parent 9040bdc8d4
commit 9cd3fa677b

View File

@@ -95,7 +95,7 @@ class Selectors extends WireArray {
'[' => ']',
'{' => '}',
'(' => ')',
);
);
/**
* Given a selector string, extract it into one or more corresponding Selector objects, iterable in this object.
@@ -204,6 +204,78 @@ class Selectors extends WireArray {
}
}
/**
* Get all operators allowed by selectors
*
* #pw-group-static-helpers
*
* @param array $options
* - `operator` (string): Return info for only this operator. When specified, only value is returned (default='').
* - `compareType` (int): Return only operators matching given `Selector::compareType*` constant (default=0).
* - `getIndexType` (string): Index type to use in returned array: 'operator' or 'class' (default='class')
* - `getValueType` (string): Value type to use in returned array: 'operator', 'class', 'compareType', 'verbose' (default='operator').
* If 'verbose' option used then assoc array returned for each operator containing 'class', 'operator', 'compareType'.
* @return array|string|int Returned array where both keys and values are operators (or values are requested 'valueType' option)
* If 'operator' option specified, return value is string, int or array (requested 'valueType'), and there is no indexType.
* @since 3.0.154
*
*/
static public function getOperators(array $options = array()) {
$defaults = array(
'operator' => '',
'getIndexType' => 'class',
'getValueType' => 'operator',
'compareType' => 0,
);
$options = array_merge($defaults, $options);
$operators = array();
$compareType = (int) $options['compareType'];
$indexType = $options['getIndexType'];
$valueType = $options['getValueType'];
$selectorTypes = self::$selectorTypes;
if(!empty($options['operator'])) {
$selectorTypes = array($selectorTypes[$options['operator']]);
}
foreach($selectorTypes as $operator => $typeName) {
$className = __NAMESPACE__ . "\\$typeName";
if($compareType) {
if(!($className::getCompareType() & $options['compareType'])) continue;
}
if($valueType === 'class') {
$value = $typeName;
} else if($valueType === 'className') {
$value = $className;
} else if($valueType === 'compareType') {
$value = $className::getCompareType();
} else if($valueType === 'verbose') {
$value = array(
'operator' => $operator,
'class' => $typeName,
'className' => $className,
'compareType' => $className::getCompareType(),
);
} else {
$value = $operator;
}
if($indexType === 'class') {
$key = $typeName;
} else if($indexType === 'className') {
$key = $className;
} else {
$key = $operator;
}
$operators[$key] = $value;
}
if(!empty($options['operator'])) return reset($operators);
return $operators;
}
/**
* Return array of all valid operator characters
*