forked from hechoendrupal/drupal-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtils.php
More file actions
90 lines (77 loc) · 2.59 KB
/
Copy pathStringUtils.php
File metadata and controls
90 lines (77 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* @file
* Contains \Drupal\AppConsole\Utils\StringUtils
* Utility functions
*/
namespace Drupal\AppConsole\Utils;
use Symfony\Component\Console\Helper\Helper;
class StringUtils extends Helper
{
// This REGEX captures all uppercase letters after the first character
const REGEX_UPPER_CASE_LETTERS = '/(?<=\\w)(?=[A-Z])/';
// This REGEX captures non alphanumeric characters and non underscores
const REGEX_MACHINE_NAME_CHARS = '@[^a-z0-9_]+@';
// This REGEX captures
const REGEX_CAMEL_CASE_UNDER = '/([a-z])([A-Z])/';
// This REGEX captures spaces around words
const REGEX_SPACES = '/\s\s+/';
/**
* Replaces non alphanumeric characters with underscores
* @param String $name User input
* @return String $machine_name User input in machine-name format
*/
public function createMachineName($name)
{
$machine_name = preg_replace(self::REGEX_MACHINE_NAME_CHARS, '_', strtolower($name));
$machine_name = trim($machine_name, '_');
return $machine_name;
}
/**
* Converts camel-case strings to machine-name format
* @param String $name User input
* @return String $machine_name User input in machine-name format
*/
public function camelCaseToMachineName($name)
{
$machine_name = preg_replace(self::REGEX_UPPER_CASE_LETTERS, "_$1", $name);
$machine_name = preg_replace(self::REGEX_MACHINE_NAME_CHARS, '_', strtolower($machine_name));
$machine_name = trim($machine_name, '_');
return $machine_name;
}
/**
* Converts camel-case strings to under-score format
* @param String $camel_case User input
* @return String
*/
public function camelCaseToUnderscore($camel_case)
{
return strtolower(preg_replace(self::REGEX_CAMEL_CASE_UNDER, '$1_$2', $camel_case));
}
public function getName()
{
return "stringUtils";
}
public function humanToCamelCase($human)
{
return str_replace(' ', '', ucwords($human));
}
/**
* Converts My Name to my name. For permissions
* @param String $permission User input
* @return String
*/
public function camelCaseToLowerCase($permission)
{
return strtolower(preg_replace(self::REGEX_SPACES, ' ', $permission));
}
/**
* Convert the first character of upper case. For permissions
* @param String $permission_title User input
* @return String
*/
public function anyCaseToUcFirst($permission_title)
{
return ucfirst(preg_replace(self::REGEX_SPACES, ' ', $permission_title));
}
}