5 * Contains \Drupal\Console\Core\Utils\StringConverter
8 namespace Drupal\Console\Core\Utils;
11 * Class StringConverter
13 * @package Drupal\Console\Core\Utils
17 // This REGEX captures all uppercase letters after the first character
18 const REGEX_UPPER_CASE_LETTERS = '/(?<=\\w)(?=[A-Z])/';
19 // This REGEX captures non alphanumeric characters and non underscores
20 const REGEX_MACHINE_NAME_CHARS = '@[^a-z0-9_]+@';
21 // This REGEX captures
22 const REGEX_CAMEL_CASE_UNDER = '/([a-z])([A-Z])/';
23 // This REGEX captures spaces around words
24 const REGEX_SPACES = '/\s\s+/';
25 // This REGEX forces max length to 32
26 const MAX_MACHINE_NAME = 32;
29 * Replaces non alphanumeric characters with underscores.
31 * @param String $name User input
33 * @return String $machine_name User input in machine-name format
35 public function createMachineName($name)
37 $machine_name = preg_replace(self::REGEX_MACHINE_NAME_CHARS, '_', strtolower($name));
38 $machine_name = trim($machine_name, '_');
40 if (strlen($machine_name) > self::MAX_MACHINE_NAME) {
41 $machine_name = substr($machine_name, 0, self::MAX_MACHINE_NAME);
48 * Converts camel-case strings to machine-name format.
50 * @param String $name User input
52 * @return String $machine_name User input in machine-name format
54 public function camelCaseToMachineName($name)
56 $machine_name = preg_replace(self::REGEX_UPPER_CASE_LETTERS, '_$1', $name);
57 $machine_name = preg_replace(self::REGEX_MACHINE_NAME_CHARS, '_', strtolower($machine_name));
58 $machine_name = trim($machine_name, '_');
64 * Converts camel-case strings to under-score format.
66 * @param String $camel_case User input
70 public function camelCaseToUnderscore($camel_case)
72 return strtolower(preg_replace(self::REGEX_CAMEL_CASE_UNDER, '$1_$2', $camel_case));
76 * Converts camel-case strings to human readable format.
78 * @param String $camel_case User input
82 public function camelCaseToHuman($camel_case)
84 return ucfirst(strtolower(preg_replace(self::REGEX_CAMEL_CASE_UNDER, '$1 $2', $camel_case)));
91 public function humanToCamelCase($human)
93 return str_replace(' ', '', ucwords($human));
97 * Converts My Name to my name. For permissions.
99 * @param String $permission User input
103 public function camelCaseToLowerCase($permission)
105 return strtolower(preg_replace(self::REGEX_SPACES, ' ', $permission));
109 * Convert the first character of upper case. For permissions.
111 * @param String $permission_title User input
115 public function anyCaseToUcFirst($permission_title)
117 return ucfirst(preg_replace(self::REGEX_SPACES, ' ', $permission_title));
124 public function removeSuffix($className)
133 if (strlen($className) == 0) {
137 foreach ($suffixes as $suffix) {
138 $length = strlen($suffix);
139 if (strlen($className) <= $length) {
143 if (substr($className, -$length) === $suffix) {
144 return substr($className, 0, -$length);
155 public function underscoreToCamelCase($input)
157 return lcfirst(str_replace('_', '', ucwords($input, '_')));