Skip to content
This repository has been archived by the owner on Sep 28, 2018. It is now read-only.

Commit

Permalink
Added format() into the main Str class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brad Jones committed Nov 30, 2016
1 parent c90ba4a commit 9d8c26d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
3 changes: 3 additions & 0 deletions couscous.yml
Expand Up @@ -159,6 +159,9 @@ menu:
countSubstr:
text: countSubstr
relativeUrl: docs/Methods/countsubstr.html
format:
text: format
relativeUrl: docs/Methods/format.html
htmlDecode:
text: htmlDecode
relativeUrl: docs/Methods/htmldecode.html
Expand Down
17 changes: 17 additions & 0 deletions docs/Methods/format.md
@@ -0,0 +1,17 @@
# format
Formats the current string, using the provided array of arguments.

## Description
`static format(array $args)`

For details on the syntax of the $format string:
http://php.net/manual/en/function.sprintf.php

### Parameters
* _array_ __$args__
The arguments that will be inserted
into the $format string.


### Return Value
_static_
4 changes: 2 additions & 2 deletions docs/Methods/reverse.md
Expand Up @@ -2,9 +2,9 @@
Returns a reversed string. A multibyte version of strrev().

## Description
`\Stringy reverse()`
`static reverse()`


### Return Value
_\Stringy_
_static_
Object with a reversed $str
16 changes: 16 additions & 0 deletions src/Methods/Misc.php
Expand Up @@ -229,4 +229,20 @@ public function countSubstr($substring, $caseSensitive = true)
$substring = UTF8::strtoupper($substring, $this->encoding);
return UTF8::substr_count($str, $substring);
}

/**
* Formats the current string, using the provided array of arguments.
*
* For details on the syntax of the $format string:
* http://php.net/manual/en/function.sprintf.php
*
* @param array $args The arguments that will be inserted
* into the $format string.
*
* @return static
*/
public function format($args)
{
return $this->newSelf(vsprintf($this->scalarString, $args));
}
}
21 changes: 21 additions & 0 deletions tests/MiscTest.php
Expand Up @@ -336,4 +336,25 @@ public function countSubstrProvider()
array(2, 'συγγραφέας', 'Σ', false, 'UTF-8')
);
}

/**
* @dataProvider formatProvider()
*/
public function testFormat($expected, $string, $args, $encoding = null)
{
$str = new Str($string, $encoding);
$result = $str->format($args);
$this->assertInstanceOf('Gears\\String\\Str', $result);
$this->assertEquals($expected, $result);
$this->assertEquals($string, $str);
}

public function formatProvider()
{
return array
(
array('hello world', 'hello %s', ['world']),
array('fòô bàř', 'fòô %s', ['bàř'], 'UTF-8')
);
}
}

0 comments on commit 9d8c26d

Please sign in to comment.