You are here

class LanguageTypeResolver in GraphQL 8

Same name and namespace in other branches
  1. 8.2 src/TypeResolver/LanguageTypeResolver.php \Drupal\graphql\TypeResolver\LanguageTypeResolver

Resolves the schema for the 'language' data type.

Hierarchy

Expanded class hierarchy of LanguageTypeResolver

1 string reference to 'LanguageTypeResolver'
graphql.services.yml in ./graphql.services.yml
graphql.services.yml
1 service uses LanguageTypeResolver
graphql.type_resolver.typed_data.language in ./graphql.services.yml
Drupal\graphql\TypeResolver\LanguageTypeResolver

File

src/TypeResolver/LanguageTypeResolver.php, line 15

Namespace

Drupal\graphql\TypeResolver
View source
class LanguageTypeResolver implements TypeResolverInterface {

  /**
   * Static cache of the language schema definition.
   *
   * @var \Fubhy\GraphQL\Type\Definition\Types\ObjectType
   */
  protected $languageType;

  /**
   * {@inheritdoc}
   */
  public function applies($type) {
    if ($type instanceof DataDefinitionInterface) {
      return $type
        ->getDataType() === 'language';
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function resolveRecursive($type) {
    if ($type instanceof DataDefinitionInterface && $type
      ->getDataType() === 'language') {
      if (!isset($this->languageType)) {
        $this->languageType = new ObjectType('Language', [
          'id' => [
            'type' => new NonNullModifier(Type::idType()),
            'resolve' => [
              __CLASS__,
              'getId',
            ],
          ],
          'name' => [
            'type' => new NonNullModifier(Type::stringType()),
            'resolve' => [
              __CLASS__,
              'getName',
            ],
          ],
          'direction' => [
            'type' => new NonNullModifier(Type::stringType()),
            'resolve' => [
              __CLASS__,
              'getDirection',
            ],
          ],
          'weight' => [
            'type' => new NonNullModifier(Type::intType()),
            'resolve' => [
              __CLASS__,
              'getWeight',
            ],
          ],
          'locked' => [
            'type' => new NonNullModifier(Type::booleanType()),
            'resolve' => [
              __CLASS__,
              'isLocked',
            ],
          ],
          'default' => [
            'type' => new NonNullModifier(Type::booleanType()),
            'resolve' => [
              __CLASS__,
              'isDefault',
            ],
          ],
        ]);
      }
      return $this->languageType;
    }
    return NULL;
  }

  /**
   * Gets the name of the language.
   *
   * @param \Drupal\Core\TypedData\Plugin\DataType\Language
   *   The language object.
   *
   * @return string The human-readable name of the language (in the language that was
   * The human-readable name of the language (in the language that was
   * used to construct this object).
   */
  public static function getName(Language $language) {
    return $language
      ->getValue()
      ->getName();
  }

  /**
   * Gets the ID (language code).
   *
   * @param \Drupal\Core\TypedData\Plugin\DataType\Language
   *   The language object.
   *
   * @return string
   *   The language code.
   */
  public static function getId(Language $language) {
    return $language
      ->getValue()
      ->getId();
  }

  /**
   * Gets the text direction (left-to-right or right-to-left).
   *
   * @param \Drupal\Core\TypedData\Plugin\DataType\Language
   *   The language object.
   *
   * @return string
   *   Either self::DIRECTION_LTR or self::DIRECTION_RTL.
   */
  public static function getDirection(Language $language) {
    return $language
      ->getValue()
      ->getDirection();
  }

  /**
   * Gets the weight of the language.
   *
   * @param \Drupal\Core\TypedData\Plugin\DataType\Language
   *   The language object.
   *
   * @return int
   *   The weight, used to order languages with larger positive weights sinking
   *   items toward the bottom of lists.
   */
  public static function getWeight(Language $language) {
    return $language
      ->getValue()
      ->getWeight();
  }

  /**
   * Returns whether this language is the default language.
   *
   * @param \Drupal\Core\TypedData\Plugin\DataType\Language
   *   The language object.
   *
   * @return bool
   *   Whether the language is the default language.
   */
  public static function isDefault(Language $language) {
    return $language
      ->getValue()
      ->isDefault();
  }

  /**
   * Returns whether this language is locked.
   *
   * @param \Drupal\Core\TypedData\Plugin\DataType\Language
   *   The language object.
   *
   * @return bool
   *   Whether the language is locked or not.
   */
  public static function isLocked(Language $language) {
    return $language
      ->getValue()
      ->isLocked();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LanguageTypeResolver::$languageType protected property Static cache of the language schema definition.
LanguageTypeResolver::applies public function Overrides TypeResolverInterface::applies
LanguageTypeResolver::getDirection public static function Gets the text direction (left-to-right or right-to-left).
LanguageTypeResolver::getId public static function Gets the ID (language code).
LanguageTypeResolver::getName public static function Gets the name of the language.
LanguageTypeResolver::getWeight public static function Gets the weight of the language.
LanguageTypeResolver::isDefault public static function Returns whether this language is the default language.
LanguageTypeResolver::isLocked public static function Returns whether this language is locked.
LanguageTypeResolver::resolveRecursive public function Overrides TypeResolverInterface::resolveRecursive