comparison vendor/chi-teck/drupal-code-generator/src/Utils.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children 12f9dff5fda9
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
30 return trim(preg_replace( 30 return trim(preg_replace(
31 ['/^[0-9]+/', '/[^a-z0-9_]+/'], 31 ['/^[0-9]+/', '/[^a-z0-9_]+/'],
32 '_', 32 '_',
33 strtolower($human_name) 33 strtolower($human_name)
34 ), '_'); 34 ), '_');
35 }
36
37 /**
38 * Transforms a camelized sting to machine name.
39 */
40 public static function camel2machine($input) {
41 return self::human2machine(preg_replace('/[A-Z]/', ' \0', $input));
35 } 42 }
36 43
37 /** 44 /**
38 * Camelize a string. 45 * Camelize a string.
39 */ 46 */
63 * @see http://php.net/manual/en/language.oop5.basic.php 70 * @see http://php.net/manual/en/language.oop5.basic.php
64 */ 71 */
65 public static function validateClassName($value) { 72 public static function validateClassName($value) {
66 if (!preg_match('/^[A-Z][a-zA-Z0-9]+$/', $value)) { 73 if (!preg_match('/^[A-Z][a-zA-Z0-9]+$/', $value)) {
67 throw new \UnexpectedValueException('The value is not correct class name.'); 74 throw new \UnexpectedValueException('The value is not correct class name.');
75 }
76 return $value;
77 }
78
79 /**
80 * Service name validator.
81 */
82 public static function validateServiceName($value) {
83 if ($value !== '' && !preg_match('/^[a-z][a-z0-9_\.]*[a-z0-9]$/', $value)) {
84 throw new \UnexpectedValueException('The value is not correct service name.');
68 } 85 }
69 return $value; 86 return $value;
70 } 87 }
71 88
72 /** 89 /**
152 */ 169 */
153 public static function getExtensionRoot($directory) { 170 public static function getExtensionRoot($directory) {
154 $extension_root = FALSE; 171 $extension_root = FALSE;
155 for ($i = 1; $i <= 5; $i++) { 172 for ($i = 1; $i <= 5; $i++) {
156 $info_file = $directory . '/' . basename($directory) . '.info'; 173 $info_file = $directory . '/' . basename($directory) . '.info';
157 if (file_exists($info_file) || file_exists($info_file . '.yml')) { 174 if ((file_exists($info_file) && basename($directory) !== 'drush') || file_exists($info_file . '.yml')) {
158 $extension_root = $directory; 175 $extension_root = $directory;
159 break; 176 break;
160 } 177 }
161 $directory = dirname($directory); 178 $directory = dirname($directory);
162 } 179 }
196 } 213 }
197 } 214 }
198 return str_replace(array_keys($tokens), array_values($tokens), $text); 215 return str_replace(array_keys($tokens), array_values($tokens), $text);
199 } 216 }
200 217
218 /**
219 * Pluralizes a noun.
220 *
221 * @param string $string
222 * A noun to pluralize.
223 *
224 * @return string
225 * The pluralized noun.
226 */
227 public static function pluralize($string) {
228 switch (substr($string, -1)) {
229 case 'y':
230 return substr($string, 0, -1) . 'ies';
231
232 case 's':
233 return $string . 'es';
234
235 default:
236 return $string . 's';
237 }
238 }
239
240 /**
241 * Prepares choices.
242 *
243 * @param array $raw_choices
244 * The choices to be prepared.
245 *
246 * @return array
247 * The prepared choices.
248 */
249 public static function prepareChoices(array $raw_choices) {
250 // The $raw_choices can be an associative array.
251 $choices = array_values($raw_choices);
252 // Start choices list form '1'.
253 array_unshift($choices, NULL);
254 unset($choices[0]);
255 return $choices;
256 }
257
201 } 258 }