1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
<?php
namespace WPGraphQL\Type;
use GraphQL\Type\Definition\EnumType;
/**
* Class WPEnumType
*
* EnumTypes should extend this class to have filters and sorting applied, etc.
*
* @package WPGraphQL\Type
*/
class WPEnumType extends EnumType {
/**
* WPInputObjectType constructor.
*
* @param array $config
*/
public function __construct( $config ) {
$config['name'] = ucfirst( $config['name'] );
$config['values'] = self::prepare_values( $config['values'], $config['name'] );
parent::__construct( $config );
}
/**
* prepare_values
*
* This function sorts the values and applies a filter to allow for easily
* extending/modifying the shape of the Schema for the enum.
*
* @param array $values
* @param string $type_name
* @return mixed
* @since 0.0.5
*/
private static function prepare_values( $values, $type_name ) {
/**
* Pass the values through a filter
*
* lcfirst( $type_name ) filter was added for backward compatibility
*
* @param array $values
*
* @since 0.0.5
*/
$values = apply_filters( 'graphql_' . lcfirst( $type_name ) . '_values', $values );
$values = apply_filters( 'graphql_' . $type_name . '_values', $values );
/**
* Sort the values alphabetically by key. This makes reading through docs much easier
* @since 0.0.5
*/
ksort( $values );
/**
* Return the filtered, sorted $fields
* @since 0.0.5
*/
return $values;
}
}