From 9cd3fa677b676530d0396a1ef36a864ccfb0eb16 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 10 Apr 2020 12:42:16 -0400 Subject: [PATCH] Add a Selectors::getOperators() method that provides a lot of options for retrieving info about available operators --- wire/core/Selectors.php | 74 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/wire/core/Selectors.php b/wire/core/Selectors.php index fc3fb00d..73258af2 100644 --- a/wire/core/Selectors.php +++ b/wire/core/Selectors.php @@ -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 *