Automattic\WooCommerce\Utilities

ArrayUtil::get_selector_callback()private staticWC 1.0

Helper function to generate a callback which can be executed on an array to select a value from each item.

Метод класса: ArrayUtil{}

No Hooks.

Return

\Closure. Callback to select the value.

Usage

$result = ArrayUtil::get_selector_callback( $selector_name, $selector_type ): \Closure;
$selector_name(string) (required)
Field/property/method name to select.
$selector_type(int)
Selector type.
Default: self::SELECT_BY_AUTO

Code of ArrayUtil::get_selector_callback() WC 7.1.0

private static function get_selector_callback( string $selector_name, int $selector_type = self::SELECT_BY_AUTO ): \Closure {
	if ( self::SELECT_BY_OBJECT_METHOD === $selector_type ) {
		$callback = function( $item ) use ( $selector_name ) {
			return $item->$selector_name();
		};
	} elseif ( self::SELECT_BY_OBJECT_PROPERTY === $selector_type ) {
		$callback = function( $item ) use ( $selector_name ) {
			return $item->$selector_name;
		};
	} elseif ( self::SELECT_BY_ARRAY_KEY === $selector_type ) {
		$callback = function( $item ) use ( $selector_name ) {
			return $item[ $selector_name ];
		};
	} else {
		$callback = function( $item ) use ( $selector_name ) {
			if ( is_array( $item ) ) {
				return $item[ $selector_name ];
			} elseif ( method_exists( $item, $selector_name ) ) {
				return $item->$selector_name();
			} else {
				return $item->$selector_name;
			}
		};
	}
	return $callback;
}