diff --git a/couscous.yml b/couscous.yml index c72d2c5..b67fbaf 100644 --- a/couscous.yml +++ b/couscous.yml @@ -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 diff --git a/docs/Methods/format.md b/docs/Methods/format.md new file mode 100644 index 0000000..249399c --- /dev/null +++ b/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_ \ No newline at end of file diff --git a/docs/Methods/reverse.md b/docs/Methods/reverse.md index f974375..3d946fb 100644 --- a/docs/Methods/reverse.md +++ b/docs/Methods/reverse.md @@ -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 \ No newline at end of file diff --git a/src/Methods/Misc.php b/src/Methods/Misc.php index 43919d8..23ec9d5 100644 --- a/src/Methods/Misc.php +++ b/src/Methods/Misc.php @@ -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)); + } } diff --git a/tests/MiscTest.php b/tests/MiscTest.php index f4095f5..49749aa 100644 --- a/tests/MiscTest.php +++ b/tests/MiscTest.php @@ -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') + ); + } }