1: <?php
2: /**
3: *
4: * @package XCube
5: * @version $Id: XCube_Utils.class.php,v 1.5 2008/10/12 04:30:27 minahito Exp $
6: * @copyright Copyright 2005-2007 XOOPS Cube Project <https://github.com/xoopscube/legacy>
7: * @license https://github.com/xoopscube/legacy/blob/master/docs/bsd_licenses.txt Modified BSD license
8: *
9: */
10:
11: /**
12: * @public
13: * @brief The utility class collecting static helper functions.
14: */
15: class XCube_Utils
16: {
17: /**
18: * @private
19: * @brief Private Constructor. In other words, it's impossible to generate an instance of this class.
20: */
21: function XCube_Utils()
22: {
23: }
24:
25: /**
26: * @public
27: * @brief [Static] The alias for the current controller::redirectHeader(). This function will be deprecated.
28: * @param $url string
29: * @param $time int
30: * @param $message mixed - string or string[] - If you want to multiline message, you must set message as array.
31: * @return void
32: *
33: * @deprecated XCube 1.0 will remove this method. Don't use static function of XCube
34: * layer for redirect.
35: */
36: function redirectHeader($url, $time, $messages = null)
37: {
38: $root =& XCube_Root::getSingleton();
39: $root->mController->executeRedirect($url, $time, $messages);
40: }
41:
42: /**
43: * @public
44: * @brief [Static] Formats string with special care for international.
45: * @return string - Rendered string.
46: *
47: * This method renders string with modifiers and parameters. Parameters are .NET
48: * style, not printf style. Because .NET style is clear.
49: *
50: * \code
51: * XCube_Utils::formatString("{0} is {1}", "Time", "Money");
52: * // Result "Time is Money"
53: * \endcode
54: *
55: * It's possible to add a modifier to parameter place holders with ':' mark.
56: *
57: * \code
58: * XCube_Utils::formatString("{0:ucFirst} is {1:toLower}", "Time", "Money");
59: * // Result "Time is Money"
60: * \endcode
61: *
62: * This feature is useful for automatic combined messages by message catalogs.
63: *
64: * \par Modifiers
65: * -li ufFirst - Upper the first charactor of the parameter's value.
66: * -li toUpper - Upper the parameter's value.
67: * -li toLower - Lower the parameter's value.
68: *
69: * @remarks
70: * This method doesn't implement the provider which knows how to format
71: * for each locales. So, this method is interim implement.
72: */
73: function formatString()
74: {
75: $arr = func_get_args();
76:
77: if(count($arr)==0)
78: return null;
79:
80: $message = $arr[0];
81:
82: $variables = array();
83: if (is_array($arr[1])) {
84: $variables = $arr[1];
85: }
86: else {
87: $variables = $arr;
88: array_shift($variables);
89: }
90:
91: for ($i = 0; $i < count($variables); $i++) {
92: $message = str_replace("{" . ($i) . "}", $variables[$i], $message);
93:
94: // Temporary....
95: $message = str_replace("{" . ($i) . ":ucFirst}", ucfirst($variables[$i]), $message);
96: $message = str_replace("{" . ($i) . ":toLower}", strtolower($variables[$i]), $message);
97: $message = str_replace("{" . ($i) . ":toUpper}", strtoupper($variables[$i]), $message);
98: }
99:
100: return $message;
101: }
102:
103: /**
104: * @deprecated XCube 1.0 will remove this method.
105: * @see XCube_Utils::formatString()
106: */
107: function formatMessage()
108: {
109: $arr = func_get_args();
110:
111: if (count($arr) == 0) {
112: return null;
113: }
114: else if (count($arr) == 1) {
115: return XCube_Utils::formatString($arr[0]);
116: }
117: else if (count($arr) > 1) {
118: $vals = $arr;
119: array_shift($vals);
120: return XCube_Utils::formatString($arr[0], $vals);
121: }
122: }
123:
124: /**
125: * @deprecated XCube 1.0 will remove this method.
126: */
127: function formatMessageByMap($subject,$arr)
128: {
129: $searches=array();
130: $replaces=array();
131: foreach($arr as $key=>$value) {
132: $searches[]="{".$key."}";
133: $replaces[]=$value;
134: }
135:
136: return str_replace($searches,$replaces,$subject);
137: }
138: }
139:
140: ?>