Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Utility / StringTransformTrait.php
1 <?php
2
3 /**
4  * @file
5  * Contains Drupal\drupalmoduleupgrader\Utility\StringTransformTrait.
6  */
7
8 namespace Drupal\drupalmoduleupgrader\Utility;
9
10 /**
11  * Contains methods for transforming strings in various helpful ways.
12  */
13 trait StringTransformTrait {
14
15   /**
16    * Converts a string toCamelCase :)
17    *
18    * @param string $string
19    *  The string to convert.
20    *
21    * @return string
22    */
23   public function toCamelCase($string) {
24     return preg_replace_callback('/_[a-z]/', function (array $match) { return strToUpper($match[0]{1}); }, $string);
25   }
26
27   /**
28    * Converts a string ToTitleCase.
29    *
30    * @param string $string
31    *  The string to convert.
32    *
33    * @return string
34    */
35   public function toTitleCase($string) {
36     $string = $this->toCamelCase($string);
37     $string{0} = strToUpper($string{0});
38
39     return $string;
40   }
41
42   /**
43    * Trims a prefix (as well as leading or trailing underscore, if any) from a
44    * string.
45    *
46    * @param string $string
47    *  The string to process.
48    * @param string $prefix
49    *  The prefix to trim off, without leading or trailing underscores.
50    *
51    * @return string
52    */
53   public function unPrefix($string, $prefix) {
54     return preg_replace('/^_?' . $prefix . '_/', NULL, $string);
55   }
56
57   /**
58    * Trims a suffix (as well as leading underscore, if any) from a string.
59    *
60    * @param string $string
61    *  The string to process.
62    * @param string $suffix
63    *  The suffix to trim off, without leading underscore.
64    *
65    * @return string
66    */
67   public function unSuffix($string, $suffix) {
68     return preg_replace('/^_?' . $suffix . '$/', NULL, $string);
69   }
70
71   /**
72    * Deletes {wildcards} from a route path.
73    *
74    * @param string $path
75    *
76    * @return string
77    */
78   public function deleteWildcards($path) {
79     return preg_replace('/\/?\{([a-zA-Z0-9_]+)\}/', NULL, $path);
80   }
81
82   /**
83    * Deletes %wildcards from a route path.
84    *
85    * @param string $path
86    *
87    * @return string
88    */
89   public function deleteLegacyWildcards($path) {
90     return preg_replace('/\/?%[a-zA-Z0-9_]+/', NULL, $path);
91   }
92
93   /**
94    * Generates an identifier from a Drupal 7 path.
95    *
96    * @param string $path
97    *  The input path, including any %wildcards.
98    *
99    * @return string
100    *  The identifier
101    */
102   public function getIdentifierFromLegacyPath($path) {
103     return $this->getIdentifierFromPath($this->deleteLegacyWildcards($path));
104   }
105
106   /**
107    * Generates an identifier from a path.
108    *
109    * @param string $path
110    *  The input path, including any {wildcards}.
111    *
112    * @return string
113    *  The identifier.
114    */
115   public function getIdentifierFromPath($path) {
116     return $this->getIdentifier($this->deleteWildcards($path));
117   }
118
119   /**
120    * Generates an identifier (prefixed with the module name, if $this->module exists)
121    * from an arbitrary string.
122    *
123    * @param $string
124    *  The input string.
125    *
126    * @return string
127    *  The identifier.
128    */
129   public function getIdentifier($string) {
130     // Replace all non-alphanumeric character sequences with an underscore.
131     $id = preg_replace('/[^a-zA-Z0-9_]+/', '_', $string);
132
133     if (isset($this->module)) {
134       // If the name begins with MODULE_, replace that underscore with a period. Otherwise,
135       // prefix the key with the module's machine name. We want all routes to look like
136       // MODULE.route.
137       if (strIPos($id, $this->module->getMachineName() . '_') === 0) {
138         $id = preg_replace('/_/', '.', $id, 1);
139       }
140       else {
141         $id = $this->module->getMachineName() . '.' . $id;
142       }
143     }
144
145     return $id;
146   }
147
148 }