From c67a7bc2bfec3982b84ca9eaf72e64a9c510b4dc Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Sat, 20 Mar 2010 07:12:21 +0200 Subject: [PATCH] Feature #272 - text.lib.php: Adding two new functions api_camel_case_to_underscore() and api_underscore_to_camel_case() and tests for them. --- main/inc/lib/fckeditor/fckeditor.php | 13 ++-------- main/inc/lib/text.lib.php | 36 +++++++++++++++++++++++++--- tests/main/inc/lib/text.lib.test.php | 22 +++++++++++++++++ 3 files changed, 57 insertions(+), 14 deletions(-) diff --git a/main/inc/lib/fckeditor/fckeditor.php b/main/inc/lib/fckeditor/fckeditor.php index 922d84b1e5..49252e9e26 100755 --- a/main/inc/lib/fckeditor/fckeditor.php +++ b/main/inc/lib/fckeditor/fckeditor.php @@ -381,7 +381,7 @@ class FCKeditor $toolbar_config[$this->ToolbarSet] = array(); if (preg_match('/[a-zA-Z_]+/', $toolbar_dir) && preg_match('/[a-zA-Z_]+/', $this->ToolbarSet)) { // A security check. // Seeking the toolbar. - @include api_get_path(LIBRARY_PATH).'fckeditor/toolbars/'.$toolbar_dir.'/'.self::camel_case_to_underscore($this->ToolbarSet).'.php'; + @include api_get_path(LIBRARY_PATH).'fckeditor/toolbars/'.$toolbar_dir.'/'.api_camel_case_to_underscore($this->ToolbarSet).'.php'; if (!isset($config['ToolbarSets']['Normal'])) { // No toolbar has been found yet. if ($toolbar_dir == 'default') { @@ -389,7 +389,7 @@ class FCKeditor $this->ToolbarSet = 'Default'; } else { // The custom toolbar does not exist, then trying to load the default one. - @include api_get_path(LIBRARY_PATH).'fckeditor/toolbars/default/'.self::camel_case_to_underscore($this->ToolbarSet).'.php'; + @include api_get_path(LIBRARY_PATH).'fckeditor/toolbars/default/'.api_camel_case_to_underscore($this->ToolbarSet).'.php'; if (!isset($config['ToolbarSets']['Normal'])) { // It does not exist in default toolbar definitions, giving up. $this->ToolbarSet = 'Default'; @@ -664,13 +664,4 @@ class FCKeditor @fclose($fp); return $file_exists; } - - /* - * Convers a string from camel case to underscore. - * @param string $string - * @return string - */ - private function camel_case_to_underscore($string) { - return strtolower(preg_replace('/([a-z])([A-Z])/', "$1_$2", $string)); - } } diff --git a/main/inc/lib/text.lib.php b/main/inc/lib/text.lib.php index ba793a638f..2cfca9d885 100755 --- a/main/inc/lib/text.lib.php +++ b/main/inc/lib/text.lib.php @@ -2,14 +2,44 @@ /* For licensing terms, see /license.txt */ /** - * This is the text library for Chamilo. - * Include/require it in your code to use its functionality. + * This is the text library for Chamilo. + * It is loaded during the global initialization, + * so the functions below are available everywhere. * - * @package chamilo.library + * @package chamilo.library */ /* FUNCTIONS */ +/** + * Convers a string from camel case into underscore. + * Works correctly with ASCII strings only, implementation for human-language strings is not necessary. + * @param string $string The input string (ASCII) + * @return string The converted result string + */ +function api_camel_case_to_underscore($string) { + return strtolower(preg_replace('/([a-z])([A-Z])/', "$1_$2", $string)); +} + +/** + * Converts a string with underscores into camel case. + * Works correctly with ASCII strings only, implementation for human-language strings is not necessary. + * @param string $string The input string (ASCII) + * @param bool $capitalise_first_char (optional) If true (default), the function capitalises the first char in the result string. + * @return string The converted result string + */ +function api_underscore_to_camel_case($string, $capitalise_first_char = true) { + if ($capitalise_first_char) { + $string = ucfirst($string); + } + return preg_replace_callback('/_([a-z])/', '_api_camelize', $string); +} + +// A function for internal use, only for this library. +function _api_camelize($match) { + return strtoupper($match[1]); +} + /** * Truncates a string. * diff --git a/tests/main/inc/lib/text.lib.test.php b/tests/main/inc/lib/text.lib.test.php index c82c38b774..298d69fb72 100755 --- a/tests/main/inc/lib/text.lib.test.php +++ b/tests/main/inc/lib/text.lib.test.php @@ -3,6 +3,28 @@ require_once(api_get_path(LIBRARY_PATH).'text.lib.php'); class TestText extends UnitTestCase { + public function test_api_camel_case_to_underscore() { + $input_strings = array('myDocuments', 'MyProfile', 'CreateNewCourse', 'Create_New_course'); + $expected_results = array('my_documents', 'my_profile', 'create_new_course', 'create_new_course'); + $results = array_map('api_camel_case_to_underscore', $input_strings); + $this->assertTrue($results == $expected_results); + //var_dump($results); + } + + function test_api_underscore_to_camel_case() { + $input_strings = array('my_documents', 'My_profile', 'create_new_course'); + $expected_results1 = array('MyDocuments', 'MyProfile', 'CreateNewCourse'); + $expected_results2 = array('myDocuments', 'MyProfile', 'createNewCourse'); + $func = create_function('$param', 'return api_underscore_to_camel_case($param, false);'); + $results1 = array('MyDocuments', 'MyProfile', 'CreateNewCourse'); + $results2 = array('myDocuments', 'MyProfile', 'createNewCourse'); + $results1 = array_map('api_underscore_to_camel_case', $input_strings); + $results2 = array_map($func, $input_strings); + $this->assertTrue($results1 == $expected_results1 && $results2 == $expected_results2); + //var_dump($results1); + //var_dump($results2); + } + function test_text_parse_glossary() { $input=''; $res=_text_parse_glossary($input);