Преглед на файлове

Added utilities file with camelize() function

Taddeus Kroes преди 13 години
родител
ревизия
a733b45e0f
променени са 3 файла, в които са добавени 53 реда и са изтрити 0 реда
  1. 1 0
      base.php
  2. 22 0
      tests/test_utils.php
  3. 30 0
      utils.php

+ 1 - 0
base.php

@@ -8,6 +8,7 @@
 
 namespace webbasics;
 
+require_once 'utils.php';
 require_once 'logger.php';
 
 /**

+ 22 - 0
tests/test_utils.php

@@ -0,0 +1,22 @@
+<?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));
+	}
+}
+
+?>

+ 30 - 0
utils.php

@@ -0,0 +1,30 @@
+<?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;
+}
+
+?>