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 67 68 69 70 71 72 73 74 75 76
<?php
namespace WPGraphQL\Type;
use GraphQL\Type\Definition\InputObjectType;
/**
* Class WPInputObjectType
*
* Input types should extend this class to take advantage of the helper methods for formatting
* and adding consistent filters.
*
* @package WPGraphQL\Type
* @since 0.0.5
*/
class WPInputObjectType extends InputObjectType {
/**
* WPInputObjectType constructor.
*
* @param array $config The configuration for the InputObjectType
*/
public function __construct( $config = [] ) {
parent::__construct( $config );
}
/**
* prepare_fields
*
* This function sorts the fields and applies a filter to allow for easily
* extending/modifying the shape of the Schema for the type.
*
* @param array $fields
* @param string $type_name
* @return mixed
* @since 0.0.5
*/
public static function prepare_fields( array $fields, $type_name ) {
/**
* Pass the fields through a filter
*
* @param array $fields
*
* @since 0.0.5
*/
$fields = apply_filters( 'graphql_' . lcfirst( $type_name ) . '_fields', $fields );
$fields = apply_filters( "graphql_{$type_name}_fields", $fields );
/**
* Sort the fields alphabetically by key. This makes reading through docs much easier
* @since 0.0.2
*/
ksort( $fields );
/**
* Return the filtered, sorted $fields
* @since 0.0.5
*/
return $fields;
}
/**
* format_enum_name
*
* This formats enum_names to be all caps with underscores for spaces/word-breaks
*
* @param $name
* @return string
* @since 0.0.5
*/
public static function format_enum_name( $name ) {
return strtoupper( preg_replace( '/[^A-Za-z0-9]/i', '_', $name ) );
}
}