Commit b91f9709 authored by Taddeus Kroes's avatar Taddeus Kroes

Moved asprintf() to utilities file

parent a8d161eb
...@@ -120,25 +120,4 @@ interface Singleton { ...@@ -120,25 +120,4 @@ interface Singleton {
public static function getInstance(); public static function getInstance();
} }
/**
* Format a string using parameters in an associative array.
*
* <code>
* echo asprintf('foo %(bar)', array('bar' => 'baz')); // prints 'foo baz'
* </code>
*
* @param string $format The string to format.
* @param array $params An associative array with parameters that are used in $format.
* @package WebBasics
*/
function asprintf($format, array $params) {
return preg_replace_callback(
'/%\(([a-z0-9-_ ]*)\)/i',
function($matches) use ($params) {
return (string)$params[$matches[1]];
},
$format
);
}
?> ?>
\ No newline at end of file
...@@ -15,14 +15,6 @@ class BaseTest extends PHPUnit_Framework_TestCase { ...@@ -15,14 +15,6 @@ class BaseTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(BaseExtension::create('a', 'b'), new BaseExtension('a', 'b')); $this->assertEquals(BaseExtension::create('a', 'b'), new BaseExtension('a', 'b'));
} }
function testAsprintf() {
$this->assertEquals(webbasics\asprintf('%(foo) baz', array('foo' => 'bar')), 'bar baz');
$this->assertEquals(webbasics\asprintf('%(foo) baz %(foo)',
array('foo' => 'bar')), 'bar baz bar');
$this->assertEquals(webbasics\asprintf('%(bar) baz %(foo)',
array('foo' => 'bar', 'bar' => 'foobar')), 'foobar baz bar');
}
function testPathWithSlash() { function testPathWithSlash() {
$this->assertEquals(Base::pathWithSlash('dirname'), 'dirname/'); $this->assertEquals(Base::pathWithSlash('dirname'), 'dirname/');
$this->assertEquals(Base::pathWithSlash('dirname/'), 'dirname/'); $this->assertEquals(Base::pathWithSlash('dirname/'), 'dirname/');
......
...@@ -4,7 +4,15 @@ require_once 'utils.php'; ...@@ -4,7 +4,15 @@ require_once 'utils.php';
use webbasics as wb; use webbasics as wb;
class UtilsTest extends PHPUnit_Framework_TestCase { class UtilsTest extends PHPUnit_Framework_TestCase {
function testCamelize() { function testAsprintf() {
$this->assertEquals(webbasics\asprintf('%(foo) baz', array('foo' => 'bar')), 'bar baz');
$this->assertEquals(webbasics\asprintf('%(foo) baz %(foo)',
array('foo' => 'bar')), 'bar baz bar');
$this->assertEquals(webbasics\asprintf('%(bar) baz %(foo)',
array('foo' => 'bar', 'bar' => 'foobar')), 'foobar baz bar');
}
function testCamelizeSimple() {
$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_bar_baz'));
$this->assertEquals('fooBarBaz', wb\camelize('foo-bar-baz')); $this->assertEquals('fooBarBaz', wb\camelize('foo-bar-baz'));
......
...@@ -9,6 +9,27 @@ ...@@ -9,6 +9,27 @@
namespace webbasics; namespace webbasics;
/**
* Format a string using parameters in an associative array.
*
* <code>
* echo asprintf('foo %(bar)', array('bar' => 'baz')); // prints 'foo baz'
* </code>
*
* @param string $format The string to format.
* @param array $params An associative array with parameters that are used in $format.
* @package WebBasics
*/
function asprintf($format, array $params) {
return preg_replace_callback(
'/%\(([a-z0-9-_ ]*)\)/i',
function($matches) use ($params) {
return (string)$params[$matches[1]];
},
$format
);
}
/** /**
* Camelize a string. * Camelize a string.
* *
......
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