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