Completed
Push — 8.x-3.x ( e35352...c6b219 )
by Sebastian
04:10
created

StringHelper::propCase()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
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 = func_get_args();
19
    $string = is_array($components) ? implode('_', $components) : $components;
20
    $filtered = preg_replace('/^[^_a-zA-Z]+/', '', $string);
21
    $components = array_filter(preg_split('/[^a-zA-Z0-9]/', $filtered));
22
23
    if (!count($components)) {
24
      throw new \InvalidArgumentException(sprintf("Failed to create a specification compliant string representation for '%s'.", $string));
25
    }
26
27
    return implode('', array_map('ucfirst', $components));
28
  }
29
30
  /**
31
   * Turn a list of machine names into a property-cased string.
32
   *
33
   * @return string
34
   *   A camel-cased concatenation of the input components.
35
   */
36
  public static function propCase() {
37
    $result = call_user_func_array([static::class, 'camelCase'], func_get_args());
38
    return ctype_upper($result) ? strtolower($result) : lcfirst($result);
39
  }
40
41
  /**
42
   * Wraps a type string in brackets declaring it as a list.
43
   *
44
   * @param string $type
45
   *   The type to declare as a list.
46
   *
47
   * @return string
48
   *   The decorated type string.
49
   */
50
  public static function listType($type) {
51
    return "[$type]";
52
  }
53
54
  /**
55
   * Appends an exclamation mark to a type string declaring it as non-null.
56
   *
57
   * @param string $type
58
   *   The type to declare as non-null.
59
   *
60
   * @return string
61
   *   The decorated type string.
62
   */
63
  public static function nonNullType($type) {
64
    return "$type!";
65
  }
66
67
}