Commit a733b45e authored by Taddeus Kroes's avatar Taddeus Kroes

Added utilities file with camelize() function

parent b09f6f6c
......@@ -8,6 +8,7 @@
namespace webbasics;
require_once 'utils.php';
require_once 'logger.php';
/**
......
<?php
require_once 'utils.php';
use webbasics as wb;
class UtilsTest extends PHPUnit_Framework_TestCase {
function testCamelize() {
$this->assertEquals('fooBarBaz', wb\camelize('foo bar baz'));
$this->assertEquals('fooBarBaz', wb\camelize('foo_bar_baz'));
$this->assertEquals('fooBarBaz', wb\camelize('foo-bar-baz'));
$this->assertEquals('fooBarBaz', wb\camelize('foo_barBaz'));
}
/*
* @depends testCamelize
*/
function testCamelizePascalCase() {
$this->assertEquals('FooBarBaz', wb\camelize('foo_bar_baz', true));
}
}
?>
\ No newline at end of file
<?php
/**
* Utility functions for WebBasics library.
*
* @author Taddeus Kroes
* @date 05-10-2012
* @since 0.2
*/
namespace webbasics;
/**
* Camelize a string.
*
* @param string $string The string to camelize.
* @param bool $upper Whether to make the first character uppercase (defaults to FALSE).
* @return string The camelized string.
*/
function camelize($string, $upper=false) {
$camel = preg_replace_callback('/[_ -]([a-z])/', function($matches) {
return strtoupper($matches[1]);
}, $string);
if ($upper)
return ucfirst($camel);
return $camel;
}
?>
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment