5 * Contains \Drupal\Console\Core\Utils\StringConverter
8 namespace Drupal\Console\Core\Utils;
11 * Class StringConverter
12 * @package Drupal\Console\Core\Utils
16 // This REGEX captures all uppercase letters after the first character
17 const REGEX_UPPER_CASE_LETTERS = '/(?<=\\w)(?=[A-Z])/';
18 // This REGEX captures non alphanumeric characters and non underscores
19 const REGEX_MACHINE_NAME_CHARS = '@[^a-z0-9_]+@';
20 // This REGEX captures
21 const REGEX_CAMEL_CASE_UNDER = '/([a-z])([A-Z])/';
22 // This REGEX captures spaces around words
23 const REGEX_SPACES = '/\s\s+/';
24 // This REGEX forces max length to 32
25 const MAX_MACHINE_NAME = 32;
28 * Replaces non alphanumeric characters with underscores.
30 * @param String $name User input
32 * @return String $machine_name User input in machine-name format
34 public function createMachineName($name)
36 $machine_name = preg_replace(self::REGEX_MACHINE_NAME_CHARS, '_', strtolower($name));
37 $machine_name = trim($machine_name, '_');
39 if (strlen($machine_name) > self::MAX_MACHINE_NAME) {
40 $machine_name = substr($machine_name, 0, self::MAX_MACHINE_NAME);
47 * Converts camel-case strings to machine-name format.
49 * @param String $name User input
51 * @return String $machine_name User input in machine-name format
53 public function camelCaseToMachineName($name)
55 $machine_name = preg_replace(self::REGEX_UPPER_CASE_LETTERS, '_$1', $name);
56 $machine_name = preg_replace(self::REGEX_MACHINE_NAME_CHARS, '_', strtolower($machine_name));
57 $machine_name = trim($machine_name, '_');
63 * Converts camel-case strings to under-score format.
65 * @param String $camel_case User input
69 public function camelCaseToUnderscore($camel_case)
71 return strtolower(preg_replace(self::REGEX_CAMEL_CASE_UNDER, '$1_$2', $camel_case));
75 * Converts camel-case strings to human readable format.
77 * @param String $camel_case User input
81 public function camelCaseToHuman($camel_case)
83 return ucfirst(strtolower(preg_replace(self::REGEX_CAMEL_CASE_UNDER, '$1 $2', $camel_case)));
90 public function humanToCamelCase($human)
92 return str_replace(' ', '', ucwords($human));
96 * Converts My Name to my name. For permissions.
98 * @param String $permission User input
102 public function camelCaseToLowerCase($permission)
104 return strtolower(preg_replace(self::REGEX_SPACES, ' ', $permission));
108 * Convert the first character of upper case. For permissions.
110 * @param String $permission_title User input
114 public function anyCaseToUcFirst($permission_title)
116 return ucfirst(preg_replace(self::REGEX_SPACES, ' ', $permission_title));
123 public function removeSuffix($className)
132 if (strlen($className) == 0) {
136 foreach ($suffixes as $suffix) {
137 $length = strlen($suffix);
138 if (strlen($className) <= $length) {
142 if (substr($className, -$length) === $suffix) {
143 return substr($className, 0, -$length);
154 public function underscoreToCamelCase($input)
156 return lcfirst(str_replace('_', '', ucwords($input, '_')));