Skip to content

Commit 7a472e1

Browse files
authored
Replace dashes with underscores in image style ids. (#683)
1 parent ab4f496 commit 7a472e1

4 files changed

Lines changed: 26 additions & 17 deletions

File tree

modules/graphql_core/src/Plugin/Deriver/Enums/DisplayModeIdDeriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected function getDisplayModes() {
6666
$target = $current->getTargetType();
6767
list(, $id) = explode('.', $current->id());
6868

69-
$carry[$target][strtoupper($id)] = [
69+
$carry[$target][StringHelper::upperCase($id)] = [
7070
'value' => $id,
7171
'description' => $this->t("The '@label' display mode for '@type' entities.", [
7272
'@label' => $current->label(),

modules/graphql_core/src/Plugin/GraphQL/Enums/Images/ImageStyleId.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Drupal\Core\Entity\EntityTypeManagerInterface;
66
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
77
use Drupal\graphql\Plugin\GraphQL\Enums\EnumPluginBase;
8+
use Drupal\graphql\Utility\StringHelper;
89
use Symfony\Component\DependencyInjection\ContainerInterface;
910

1011
/**
@@ -60,13 +61,7 @@ public function buildEnumValues($definition) {
6061

6162
$storage = $this->entityTypeManager->getStorage('image_style');
6263
foreach ($storage->loadMultiple() as $imageStyle) {
63-
$id = $imageStyle->id();
64-
// Add underscore prefix to numeric image styles.
65-
if (is_numeric($id[0])) {
66-
$id = "_$id";
67-
}
68-
69-
$items[strtoupper($id)] = [
64+
$items[StringHelper::upperCase($imageStyle->id())] = [
7065
'value' => $imageStyle->id(),
7166
'description' => $imageStyle->label(),
7267
];

src/Utility/StringHelper.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,30 @@ class StringHelper {
1515
* string representation for field or type names.
1616
*/
1717
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));
18+
$args = func_get_args();
19+
$components = array_map(function ($component) {
20+
return preg_replace('/[^a-zA-Z0-9_]/', '_', $component);
21+
}, $args);
2222

23+
$components = array_filter(explode('_', implode('_', $components)));
2324
if (!count($components)) {
24-
throw new \InvalidArgumentException(sprintf("Failed to create a specification compliant string representation for '%s'.", $string));
25+
throw new \InvalidArgumentException(sprintf("Failed to create a specification compliant string representation for '%s'.", implode('', $args)));
2526
}
2627

27-
return implode('', array_map('ucfirst', $components));
28+
$string = implode('', array_map('ucfirst', $components));
29+
$string = $string && is_numeric($string[0]) ? "_$string" : $string;
30+
return $string;
31+
}
32+
33+
/**
34+
* Turn a list of machine names into a upper-cased string.
35+
*
36+
* @return string
37+
* A upper-cased concatenation of the input components.
38+
*/
39+
public static function upperCase() {
40+
$result = call_user_func_array([static::class, 'camelCase'], func_get_args());
41+
return strtoupper($result);
2842
}
2943

3044
/**

tests/src/Unit/StringFormattingTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class StringFormattingTest extends UnitTestCase {
1717
* @expectedExceptionMessageRegExp /Failed to create a specification compliant string representation for '.+'\./
1818
*/
1919
public function testFailureOnInvalidInput() {
20-
StringHelper::camelCase('123456', '^%!@#&');
20+
StringHelper::camelCase('^%!@#&');
2121
}
2222

2323
/**
@@ -43,9 +43,9 @@ public function testPropCaseFormatting($input, $expected) {
4343
public function providerTestStringFormatting() {
4444
return [
4545
[['simple-name'], 'SimpleName'],
46-
[['123-name-with*^&!@some-SPECIAL-chars'], 'NameWithSomeSPECIALChars'],
46+
[['123-name-with*^&!@some-SPECIAL-chars'], '_123NameWithSomeSPECIALChars'],
4747
[['simple', 'name-of-string', 'components'], 'SimpleNameOfStringComponents'],
48-
[['123', 'array', '%^!@&#*', 'of', 'STRING', '(*&', 'components', 'with', 'SPEcial', 'chars'], 'ArrayOfSTRINGComponentsWithSPEcialChars']
48+
[['123', 'array', '%^!@&#*', 'of', 'STRING', '(*&', 'components', 'with', 'SPEcial', 'chars'], '_123ArrayOfSTRINGComponentsWithSPEcialChars']
4949
];
5050
}
5151

0 commit comments

Comments
 (0)