Skip to content

Commit

Permalink
[+]: extend "titleize" -> allow to specify what a word is
Browse files Browse the repository at this point in the history
  • Loading branch information
voku committed Aug 21, 2019
1 parent e4af1e6 commit 84b6c1e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
5 changes: 4 additions & 1 deletion src/StaticStringy.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
* @method static Stringy safeTruncate(string $stringInput, int $length, string $substring = '', string $encoding = null)
* @method static Stringy shuffle(string $stringInput, string $encoding = null)
* @method static Stringy shortenAfterWord(string $stringInput, int $length, string $strAddOn)
* @method static Stringy slugify(string $stringInput, string $separator = '-', string $language = 'en', array $replacements = [])
* @method static Stringy slugify(string $stringInput, string $separator = '-', string $language = 'en', array $replacements = [], string $encoding = null)
* @method static Stringy stripeCssMediaQueries(string $stringInput)
* @method static Stringy stripeEmptyHtmlTags(string $stringInput)
* @method static Stringy utf8ify(string $stringInput)
Expand Down Expand Up @@ -104,6 +104,9 @@
* @method static Stringy underscored(string $stringInput, string $encoding = null)
* @method static Stringy upperCamelize(string $stringInput, string $encoding = null)
* @method static Stringy upperCaseFirst(string $stringInput, string $encoding = null)
*
* @deprecated <p>Please use e.g. (new Stringy('foo'))->upperCaseFirst() instead or write your own small wrapper, because I can't protect you from BC from new parameters,
* if you use this class, thanks.</p>
*/
class StaticStringy
{
Expand Down
41 changes: 30 additions & 11 deletions src/Stringy.php
Original file line number Diff line number Diff line change
Expand Up @@ -1865,9 +1865,9 @@ public function slice(int $start, int $end = null): self
* is also converted to lowercase. The language of the source string can
* also be supplied for language-specific transliteration.
*
* @param string $separator The string used to replace whitespace
* @param string $language Language of the source string
* @param string[] $replacements A map of replaceable strings
* @param string $separator [optional] <p>The string used to replace whitespace.</p>
* @param string $language [optional] <p>Language of the source string.</p>
* @param string[] $replacements [optional] <p>A map of replaceable strings.</p>
*
* @return static Object whose $str has been converted to an URL slug
*/
Expand Down Expand Up @@ -2183,15 +2183,30 @@ public function tidy(): self
* Also accepts an array, $ignore, allowing you to list words not to be
* capitalized.
*
* @param array|string[]|null $ignore [optional] <p>An array of words not to capitalize or null. Default: null</p>
* @param array|string[]|null $ignore [optional] <p>An array of words not to capitalize or null. Default: null</p>
* @param string|null $word_define_chars [optional] <p>An string of chars that will be used as whitespace separator === words.</p>
* @param string|null $language [optional] <p>Language of the source string.</p>
*
* @return static
* <p>Object with a titleized $str.</p>
*/
public function titleize(array $ignore = null): self
public function titleize(
array $ignore = null,
string $word_define_chars = null,
string $language = null
): self
{
return static::create(
$this->utf8::str_titleize($this->str, $ignore, $this->encoding),
$this->utf8::str_titleize(
$this->str,
$ignore,
$this->encoding,
false,
$language,
false,
true,
$word_define_chars
),
$this->encoding
);
}
Expand All @@ -2214,7 +2229,11 @@ public function titleize(array $ignore = null): self
public function titleizeForHumans(array $ignore = []): self
{
return static::create(
$this->utf8::str_titleize_for_humans($this->str, $ignore, $this->encoding),
$this->utf8::str_titleize_for_humans(
$this->str,
$ignore,
$this->encoding
),
$this->encoding
);
}
Expand Down Expand Up @@ -2246,9 +2265,9 @@ public function toTransliterate(bool $strict = false): self
* en, en_GB, or en-GB. For example, passing "de" results in "äöü" mapping
* to "aeoeue" rather than "aou" as in other languages.
*
* @param string $language Language of the source string
* @param bool $removeUnsupported Whether or not to remove the
* unsupported characters
* @param string $language [optional] <p>Language of the source string.</p>
* @param bool $removeUnsupported [optional] <p>Whether or not to remove the
* unsupported characters.</p>
*
* @return static
* <p>Object whose $str contains only ASCII characters.</p>
Expand Down Expand Up @@ -3242,7 +3261,7 @@ protected function matchesPattern(string $pattern): bool
* For example, German will map 'ä' to 'ae', while other languages
* will simply return 'a'.
*
* @param string $language Language of the source string
* @param string $language [optional] <p>Language of the source string</p>
*
* @return array an array of replacements
*/
Expand Down
19 changes: 12 additions & 7 deletions tests/StringyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3780,15 +3780,16 @@ public function testTidy($expected, $str)
/**
* @dataProvider titleizeProvider()
*
* @param $expected
* @param $str
* @param null $ignore
* @param null $encoding
* @param $expected
* @param $str
* @param array|null $ignore
* @param string|null $word_define_chars
* @param string|null $encoding
*/
public function testTitleize($expected, $str, $ignore = null, $encoding = null)
public function testTitleize($expected, $str, $ignore = null, $word_define_chars = null, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->titleize($ignore);
$result = $stringy->titleize($ignore, $word_define_chars);
$this->assertStringy($result);
static::assertSame($expected, $result->toString());
static::assertSame($str, $stringy->toString());
Expand Down Expand Up @@ -4314,14 +4315,18 @@ public function titleizeProvider(): array

return [
['Title Case', 'TITLE CASE'],
['Up-to-Date', 'up-to-date', ['to'], '-'],
['Up-to-Date', 'up-to-date', ['to'], '-*'],
['Up-To-Date', 'up-to-date', [], '-*'],
['Up-To-D*A*T*E*', 'up-to-d*a*t*e*', [], '-*'],
['Testing The Method', 'testing the method'],
['Testing the Method', 'testing the method', $ignore],
[
'I Like to Watch Dvds at Home',
'i like to watch DVDs at home',
$ignore,
],
['Θα Ήθελα Να Φύγει', ' Θα ήθελα να φύγει ', null, 'UTF-8'],
['Θα Ήθελα Να Φύγει', ' Θα ήθελα να φύγει ', null, null, 'UTF-8'],
];
}

Expand Down

0 comments on commit 84b6c1e

Please sign in to comment.