X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fdrupalmoduleupgrader%2Fsrc%2FUtility%2FStringTransformTrait.php;fp=web%2Fmodules%2Fcontrib%2Fdrupalmoduleupgrader%2Fsrc%2FUtility%2FStringTransformTrait.php;h=c340b47056cee91947534b00230154cca722bcb2;hp=0000000000000000000000000000000000000000;hb=8acec36f19c470dfcda1ae2336826a782f41874c;hpb=e0411c4e83ba0d079034db83c3f7f55be24a0e35 diff --git a/web/modules/contrib/drupalmoduleupgrader/src/Utility/StringTransformTrait.php b/web/modules/contrib/drupalmoduleupgrader/src/Utility/StringTransformTrait.php new file mode 100644 index 000000000..c340b4705 --- /dev/null +++ b/web/modules/contrib/drupalmoduleupgrader/src/Utility/StringTransformTrait.php @@ -0,0 +1,148 @@ +toCamelCase($string); + $string{0} = strToUpper($string{0}); + + return $string; + } + + /** + * Trims a prefix (as well as leading or trailing underscore, if any) from a + * string. + * + * @param string $string + * The string to process. + * @param string $prefix + * The prefix to trim off, without leading or trailing underscores. + * + * @return string + */ + public function unPrefix($string, $prefix) { + return preg_replace('/^_?' . $prefix . '_/', NULL, $string); + } + + /** + * Trims a suffix (as well as leading underscore, if any) from a string. + * + * @param string $string + * The string to process. + * @param string $suffix + * The suffix to trim off, without leading underscore. + * + * @return string + */ + public function unSuffix($string, $suffix) { + return preg_replace('/^_?' . $suffix . '$/', NULL, $string); + } + + /** + * Deletes {wildcards} from a route path. + * + * @param string $path + * + * @return string + */ + public function deleteWildcards($path) { + return preg_replace('/\/?\{([a-zA-Z0-9_]+)\}/', NULL, $path); + } + + /** + * Deletes %wildcards from a route path. + * + * @param string $path + * + * @return string + */ + public function deleteLegacyWildcards($path) { + return preg_replace('/\/?%[a-zA-Z0-9_]+/', NULL, $path); + } + + /** + * Generates an identifier from a Drupal 7 path. + * + * @param string $path + * The input path, including any %wildcards. + * + * @return string + * The identifier + */ + public function getIdentifierFromLegacyPath($path) { + return $this->getIdentifierFromPath($this->deleteLegacyWildcards($path)); + } + + /** + * Generates an identifier from a path. + * + * @param string $path + * The input path, including any {wildcards}. + * + * @return string + * The identifier. + */ + public function getIdentifierFromPath($path) { + return $this->getIdentifier($this->deleteWildcards($path)); + } + + /** + * Generates an identifier (prefixed with the module name, if $this->module exists) + * from an arbitrary string. + * + * @param $string + * The input string. + * + * @return string + * The identifier. + */ + public function getIdentifier($string) { + // Replace all non-alphanumeric character sequences with an underscore. + $id = preg_replace('/[^a-zA-Z0-9_]+/', '_', $string); + + if (isset($this->module)) { + // If the name begins with MODULE_, replace that underscore with a period. Otherwise, + // prefix the key with the module's machine name. We want all routes to look like + // MODULE.route. + if (strIPos($id, $this->module->getMachineName() . '_') === 0) { + $id = preg_replace('/_/', '.', $id, 1); + } + else { + $id = $this->module->getMachineName() . '.' . $id; + } + } + + return $id; + } + +}