ExtBase:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Utility / Strings.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * A collection of utilities for extensions
27 *
28 * @package TYPO3
29 * @subpackage extbase
30 * @version $ID:$
31 */
32 abstract class Tx_ExtBase_Utility_Strings {
33
34 /**
35 * Returns a given string with underscores as UpperCamelCase (not UTF8 safe)
36 *
37 * @param string String to be converted to camel case
38 * @return string UpperCamelCasedWord
39 */
40 public static function underscoredToUpperCamelCase($string) {
41 $upperCamelCase = str_replace(' ', '', ucwords(preg_replace('![^A-Z^a-z^0-9]+!', ' ', strtolower($string))));
42 return $upperCamelCase;
43 }
44
45 /**
46 * Returns a given string with underscores as lowerCamelCase (not UTF8 safe)
47 *
48 * @param string String to be converted to camel case
49 * @return string lowerCamelCasedWord
50 */
51 public static function underscoredToLowerCamelCase($string) {
52 $upperCamelCase = str_replace(' ', '', ucwords(preg_replace('![^A-Z^a-z^0-9]+!', ' ', strtolower($string))));
53 $lowerCamelCase = strtolower(substr($upperCamelCase,0,1) ) . substr($upperCamelCase,1);
54 return $lowerCamelCase;
55 }
56
57 /**
58 * Returns a given CamelCasedString as an lowercase string with underscores (not UTF8 safe)
59 *
60 * @param string String to be converted to lowercase underscore
61 * @return string lowercase_and_underscored_string
62 */
63 public static function camelCaseToLowerCaseUnderscored($string) {
64 return strtolower(preg_replace('/(?<=\w)([A-Z])/', '_\\1', $string));
65 }
66
67 /**
68 * Sets the first char of a string to lowercase (not UTF8 safe)
69 *
70 * @param string $string
71 * @return void
72 */
73 public static function lowercaseFirst($string) {
74 return strtolower(substr($string,0,1) ) . substr($string,1);
75 }
76
77 /**
78 * Returns the extension key. Automatically detects the extension key from the classname.
79 *
80 * @return string The extension key
81 */
82 public static function getExtensionKey() {
83 if(preg_match('/^Tx_([^_]+)/', get_class($this), $matches)) {
84 $possibleExtensionKey = $matches[1];
85 if($possibleExtensionKey != 'lib') {
86 $loadedExtensionKeys = t3lib_div::trimExplode(',', $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList']);
87 foreach($loadedExtensionKeys as $extensionKey) {
88 if($possibleExtensionKey == str_replace('_', '', $extensionKey)) {
89 return $extensionKey;
90 }
91 }
92 }
93 }
94 }
95
96 }
97 ?>