Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webbasics
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
webbasics
Commits
b91f9709
Commit
b91f9709
authored
Oct 08, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved asprintf() to utilities file
parent
a8d161eb
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
30 deletions
+30
-30
base.php
base.php
+0
-21
tests/test_base.php
tests/test_base.php
+0
-8
tests/test_utils.php
tests/test_utils.php
+9
-1
utils.php
utils.php
+21
-0
No files found.
base.php
View file @
b91f9709
...
...
@@ -120,25 +120,4 @@ interface Singleton {
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
tests/test_base.php
View file @
b91f9709
...
...
@@ -15,14 +15,6 @@ class BaseTest extends PHPUnit_Framework_TestCase {
$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
()
{
$this
->
assertEquals
(
Base
::
pathWithSlash
(
'dirname'
),
'dirname/'
);
$this
->
assertEquals
(
Base
::
pathWithSlash
(
'dirname/'
),
'dirname/'
);
...
...
tests/test_utils.php
View file @
b91f9709
...
...
@@ -4,7 +4,15 @@ require_once 'utils.php';
use
webbasics
as
wb
;
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'
));
...
...
utils.php
View file @
b91f9709
...
...
@@ -9,6 +9,27 @@
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.
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment