c19ae95c59c46936a9402a7e6b1486177d1baa56
[yaffs-website] / vendor / drupal / console-core / src / Utils / StringConverter.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\Utils\StringConverter
6  */
7
8 namespace Drupal\Console\Core\Utils;
9
10 /**
11  * Class StringConverter
12  * @package Drupal\Console\Core\Utils
13  */
14 class StringConverter
15 {
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;
26
27     /**
28      * Replaces non alphanumeric characters with underscores.
29      *
30      * @param String $name User input
31      *
32      * @return String $machine_name User input in machine-name format
33      */
34     public function createMachineName($name)
35     {
36         $machine_name = preg_replace(self::REGEX_MACHINE_NAME_CHARS, '_', strtolower($name));
37         $machine_name = trim($machine_name, '_');
38
39         if (strlen($machine_name) > self::MAX_MACHINE_NAME) {
40             $machine_name = substr($machine_name, 0, self::MAX_MACHINE_NAME);
41         }
42
43         return $machine_name;
44     }
45
46     /**
47      *  Converts camel-case strings to machine-name format.
48      *
49      * @param String $name User input
50      *
51      * @return String $machine_name  User input in machine-name format
52      */
53     public function camelCaseToMachineName($name)
54     {
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, '_');
58
59         return $machine_name;
60     }
61
62     /**
63      *  Converts camel-case strings to under-score format.
64      *
65      * @param String $camel_case User input
66      *
67      * @return String
68      */
69     public function camelCaseToUnderscore($camel_case)
70     {
71         return strtolower(preg_replace(self::REGEX_CAMEL_CASE_UNDER, '$1_$2', $camel_case));
72     }
73
74     /**
75      * Converts camel-case strings to human readable format.
76      *
77      * @param String $camel_case User input
78      *
79      * @return String
80      */
81     public function camelCaseToHuman($camel_case)
82     {
83         return ucfirst(strtolower(preg_replace(self::REGEX_CAMEL_CASE_UNDER, '$1 $2', $camel_case)));
84     }
85
86     /**
87      * @param $human
88      * @return mixed
89      */
90     public function humanToCamelCase($human)
91     {
92         return str_replace(' ', '', ucwords($human));
93     }
94
95     /**
96      * Converts My Name to my name. For permissions.
97      *
98      * @param String $permission User input
99      *
100      * @return String
101      */
102     public function camelCaseToLowerCase($permission)
103     {
104         return strtolower(preg_replace(self::REGEX_SPACES, ' ', $permission));
105     }
106
107     /**
108      * Convert the first character of upper case. For permissions.
109      *
110      * @param String $permission_title User input
111      *
112      * @return String
113      */
114     public function anyCaseToUcFirst($permission_title)
115     {
116         return ucfirst(preg_replace(self::REGEX_SPACES, ' ', $permission_title));
117     }
118
119     /**
120      * @param $className
121      * @return string
122      */
123     public function removeSuffix($className)
124     {
125         $suffixes = [
126             'Form',
127             'Controller',
128             'Service',
129             'Command'
130         ];
131
132         if (strlen($className) == 0) {
133             return $className;
134         }
135
136         foreach ($suffixes as $suffix) {
137             $length = strlen($suffix);
138             if (strlen($className) <= $length) {
139                 continue;
140             }
141
142             if (substr($className, -$length) === $suffix) {
143                 return substr($className, 0, -$length);
144             }
145         }
146
147         return $className;
148     }
149
150     /**
151      * @param $input
152      * @return string
153      */
154     public function underscoreToCamelCase($input)
155     {
156         return lcfirst(str_replace('_', '', ucwords($input, '_')));
157     }
158 }