Completed
Pull Request — 8.x-3.x (#683)
by Sebastian
02:03
created

StringHelper::propCase()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\Utility;
4
5
class StringHelper {
6
7
  /**
8
   * Turn a list of machine names into a camel-cased string.
9
   *
10
   * @return string
11
   *   A camel-cased concatenation of the input components.
12
   *
13
   * @throws \InvalidArgumentException
14
   *   If the provided input does can't be converted to a specification compliant
15
   *   string representation for field or type names.
16
   */
17
  public static function camelCase() {
18
    $components = array_map(function ($component) {
19
      if ($component[0] && is_numeric($component[0])) {
20
        $component = "_$component";
21
      }
22
23
      return str_replace('-', '_', $component);
24
    }, func_get_args());
25
26
    $string = is_array($components) ? implode('_', $components) : $components;
27
    $filtered = preg_replace('/^[^_a-zA-Z]+/', '', $string);
28
    $components = array_filter(preg_split('/[^a-zA-Z0-9]/', $filtered));
29
30
    if (!count($components)) {
31
      throw new \InvalidArgumentException(sprintf("Failed to create a specification compliant string representation for '%s'.", $string));
32
    }
33
34
    return implode('', array_map('ucfirst', $components));
35
  }
36
37
  /**
38
   * Turn a list of machine names into a upper-cased string.
39
   *
40
   * @return string
41
   *   A upper-cased concatenation of the input components.
42
   */
43
  public static function upperCase() {
44
    $result = call_user_func_array([static::class, 'camelCase'], func_get_args());
45
    return strtoupper($result);
46
  }
47
48
  /**
49
   * Turn a list of machine names into a property-cased string.
50
   *
51
   * @return string
52
   *   A camel-cased concatenation of the input components.
53
   */
54
  public static function propCase() {
55
    $result = call_user_func_array([static::class, 'camelCase'], func_get_args());
56
    return ctype_upper($result) ? strtolower($result) : lcfirst($result);
57
  }
58
59
  /**
60
   * Wraps a type string in brackets declaring it as a list.
61
   *
62
   * @param string $type
63
   *   The type to declare as a list.
64
   *
65
   * @return string
66
   *   The decorated type string.
67
   */
68
  public static function listType($type) {
69
    return "[$type]";
70
  }
71
72
  /**
73
   * Appends an exclamation mark to a type string declaring it as non-null.
74
   *
75
   * @param string $type
76
   *   The type to declare as non-null.
77
   *
78
   * @return string
79
   *   The decorated type string.
80
   */
81
  public static function nonNullType($type) {
82
    return "$type!";
83
  }
84
85
  /**
86
   * Decorates a type as non-null and/or as a list.
87
   *
88
   * @param string $type
89
   *   The type to declare as non-null.
90
   * @param bool $list
91
   *   Whether to mark the type as a list.
92
   * @param bool $required
93
   *   Whether to mark the type as required.
94
   *
95
   * @return string
96
   *   The decorated type.
97
   */
98
  public static function decorateType($type, $list = FALSE, $required = FALSE) {
99
    if (!empty($list)) {
100
      $type = static::listType($type);
101
    }
102
103
    if (!empty($required)) {
104
      $type = static::nonNullType($type);
105
    }
106
107
    return $type;
108
  }
109
110
  /**
111
   * Parses a type definition from a string and properly decorates it.
112
   *
113
   * Converts type strings (e.g. [Foo!]) to their object representations.
114
   *
115
   * @param string $type
116
   *   The type string to parse.
117
   *
118
   * @return array
119
   *   The extracted type with the type decorators.
120
   */
121
  public static function parseType($type) {
122
    $decorators = [];
123
    $unwrapped = $type;
124
    $matches = NULL;
125
126
    while (preg_match('/[\[\]!]/', $unwrapped) && preg_match_all('/^(\[?)(.*?)(\]?)(!*?)$/', $unwrapped, $matches)) {
127
      if ($unwrapped === $matches[2][0] || empty($matches[1][0]) !== empty($matches[3][0])) {
128
        throw new \InvalidArgumentException(sprintf("Invalid type declaration '%s'.", $type));
129
      }
130
131
      if (!empty($matches[4][0])) {
132
        array_unshift($decorators, ['GraphQL\Type\Definition\Type', 'nonNull']);
133
      }
134
135
      if (!empty($matches[1][0]) && !empty($matches[3][0])) {
136
        array_unshift($decorators, ['GraphQL\Type\Definition\Type', 'listOf']);
137
      }
138
139
      $unwrapped = $matches[2][0];
140
    }
141
142
    return [$unwrapped, $decorators];
143
  }
144
145
}