Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
normalise.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage String
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Joomla Platform String Normalise Class
14  *
15  * @package Joomla.Platform
16  * @subpackage String
17  * @since 11.3
18  */
19 abstract class JStringNormalise
20 {
21  /**
22  * Method to convert a string from camel case.
23  *
24  * This method offers two modes. Grouped allows for splitting on groups of uppercase characters as follows:
25  *
26  * "FooBarABCDef" becomes array("Foo", "Bar", "ABC", "Def")
27  * "JFooBar" becomes array("J", "Foo", "Bar")
28  * "J001FooBar002" becomes array("J001", "Foo", "Bar002")
29  * "abcDef" becomes array("abc", "Def")
30  * "abc_defGhi_Jkl" becomes array("abc_def", "Ghi_Jkl")
31  * "ThisIsA_NASAAstronaut" becomes array("This", "Is", "A_NASA", "Astronaut"))
32  * "JohnFitzgerald_Kennedy" becomes array("John", "Fitzgerald_Kennedy"))
33  *
34  * Non-grouped will split strings at each uppercase character.
35  *
36  * @param string $input The string input (ASCII only).
37  * @param boolean $grouped Optionally allows splitting on groups of uppercase characters.
38  *
39  * @return string The space separated string.
40  *
41  * @since 12.1
42  */
43  public static function fromCamelCase($input, $grouped = false)
44  {
45  return $grouped
46  ? preg_split('/(?<=[^A-Z_])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][^A-Z_])/x', $input)
47  : trim(preg_replace('#([A-Z])#', ' $1', $input));
48  }
49 
50  /**
51  * Method to convert a string into camel case.
52  *
53  * @param string $input The string input (ASCII only).
54  *
55  * @return string The camel case string.
56  *
57  * @since 11.3
58  */
59  public static function toCamelCase($input)
60  {
61  // Convert words to uppercase and then remove spaces.
62  $input = self::toSpaceSeparated($input);
63  $input = ucwords($input);
64  $input = str_ireplace(' ', '', $input);
65 
66  return $input;
67  }
68 
69  /**
70  * Method to convert a string into dash separated form.
71  *
72  * @param string $input The string input (ASCII only).
73  *
74  * @return string The dash separated string.
75  *
76  * @since 11.3
77  */
78  public static function toDashSeparated($input)
79  {
80  // Convert spaces and underscores to dashes.
81  $input = preg_replace('#[ \-_]+#', '-', $input);
82 
83  return $input;
84  }
85 
86  /**
87  * Method to convert a string into space separated form.
88  *
89  * @param string $input The string input (ASCII only).
90  *
91  * @return string The space separated string.
92  *
93  * @since 11.3
94  */
95  public static function toSpaceSeparated($input)
96  {
97  // Convert underscores and dashes to spaces.
98  $input = preg_replace('#[ \-_]+#', ' ', $input);
99 
100  return $input;
101  }
102 
103  /**
104  * Method to convert a string into underscore separated form.
105  *
106  * @param string $input The string input (ASCII only).
107  *
108  * @return string The underscore separated string.
109  *
110  * @since 11.3
111  */
112  public static function toUnderscoreSeparated($input)
113  {
114  // Convert spaces and dashes to underscores.
115  $input = preg_replace('#[ \-_]+#', '_', $input);
116 
117  return $input;
118  }
119 
120  /**
121  * Method to convert a string into variable form.
122  *
123  * @param string $input The string input (ASCII only).
124  *
125  * @return string The variable string.
126  *
127  * @since 11.3
128  */
129  public static function toVariable($input)
130  {
131  // Remove dashes and underscores, then convert to camel case.
132  $input = self::toSpaceSeparated($input);
133  $input = self::toCamelCase($input);
134 
135  // Remove leading digits.
136  $input = preg_replace('#^[0-9]+.*$#', '', $input);
137 
138  // Lowercase the first character.
139  $first = substr($input, 0, 1);
140  $first = strtolower($first);
141 
142  // Replace the first character with the lowercase character.
143  $input = substr_replace($input, $first, 0, 1);
144 
145  return $input;
146  }
147 
148  /**
149  * Method to convert a string into key form.
150  *
151  * @param string $input The string input (ASCII only).
152  *
153  * @return string The key string.
154  *
155  * @since 11.3
156  */
157  public static function toKey($input)
158  {
159  // Remove spaces and dashes, then convert to lower case.
160  $input = self::toUnderscoreSeparated($input);
161  $input = strtolower($input);
162 
163  return $input;
164  }
165 }