Skip to content

Commit

Permalink
Fixed: Encoding was not preserved when returning a new instance of th…
Browse files Browse the repository at this point in the history
…e Str class from a method
  • Loading branch information
PHLAK committed Oct 19, 2019
1 parent 2bd304a commit 2f114b0
Show file tree
Hide file tree
Showing 65 changed files with 585 additions and 53 deletions.
23 changes: 13 additions & 10 deletions src/Traits/Caseable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,22 @@ public function uppercase(string $mode = Config\Uppercase::ALL) : self

switch ($mode) {
case Config\Uppercase::ALL:
return new static(mb_strtoupper($this->string, $this->encoding));
return new static(mb_strtoupper($this->string, $this->encoding), $this->encoding);

case Config\Uppercase::FIRST:
return new static(
mb_strtoupper(
mb_substr($this->string, 0, 1, $this->encoding), $this->encoding
) . mb_substr($this->string, 1, null, $this->encoding)
) . mb_substr($this->string, 1, null, $this->encoding),
$this->encoding
);

case Config\Uppercase::WORDS:
$string = preg_replace_callback('/(\p{Ll})[\S]*/u', function ($matched) {
return mb_strtoupper($matched[1], $this->encoding) . mb_substr($matched[0], 1, null, $this->encoding);
}, $this->string);

return new static($string);
return new static($string, $this->encoding);

default:
throw new RuntimeException('Invalid mode');
Expand Down Expand Up @@ -71,19 +72,20 @@ public function lowercase(string $mode = Config\Lowercase::ALL) : self

switch ($mode) {
case Config\Lowercase::ALL:
return new static(mb_strtolower($this->string, $this->encoding));
return new static(mb_strtolower($this->string, $this->encoding), $this->encoding);

case Config\Lowercase::FIRST:
return new static(
mb_strtolower(mb_substr($this->string, 0, 1, $this->encoding), $this->encoding) . mb_substr($this->string, 1, null, $this->encoding)
mb_strtolower(mb_substr($this->string, 0, 1, $this->encoding), $this->encoding) . mb_substr($this->string, 1, null, $this->encoding),
$this->encoding
);

case Config\Lowercase::WORDS:
$string = preg_replace_callback('/(\p{Lu})[\S]*/u', function ($matched) {
return mb_strtolower($matched[1], $this->encoding) . mb_substr($matched[0], 1, null, $this->encoding);
}, $this->string);

return new static($string);
return new static($string, $this->encoding);

default:
throw new RuntimeException('Invalid mode');
Expand All @@ -104,7 +106,8 @@ public function camelCase() : self
$word = implode('', $words);

return new static(
mb_strtolower(mb_substr($word, 0, 1, $this->encoding), $this->encoding) . mb_substr($word, 1, null, $this->encoding)
mb_strtolower(mb_substr($word, 0, 1, $this->encoding), $this->encoding) . mb_substr($word, 1, null, $this->encoding),
$this->encoding
);
}

Expand All @@ -119,7 +122,7 @@ public function studlyCase() : self
return mb_strtoupper(mb_substr($word, 0, 1, $this->encoding), $this->encoding) . mb_substr($word, 1, null, $this->encoding);
}, Support\Str::words($this->string));

return new static(implode('', $words));
return new static(implode('', $words), $this->encoding);
}

/**
Expand All @@ -143,7 +146,7 @@ public function snakeCase() : self
return mb_strtolower($word, $this->encoding);
}, Support\Str::words($this->string));

return new static(implode('_', $words));
return new static(implode('_', $words), $this->encoding);
}

/**
Expand All @@ -157,6 +160,6 @@ public function kebabCase() : self
return mb_strtolower($word, $this->encoding);
}, Support\Str::words($this->string));

return new static(implode('-', $words));
return new static(implode('-', $words), $this->encoding);
}
}
8 changes: 4 additions & 4 deletions src/Traits/Convenience.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function echo() : self
*/
public function format(...$args) : self
{
return new static(sprintf($this->string, ...$args));
return new static(sprintf($this->string, ...$args), $this->encoding);
}

/**
Expand All @@ -63,7 +63,7 @@ public function words() : array
preg_match_all('/\p{Lu}?[\p{Ll}0-9]+/u', $this->string, $matches);

return array_map(function ($words) {
return new static($words);
return new static($words, $this->encoding);
}, $matches[0]);
}

Expand All @@ -83,7 +83,7 @@ public function characters($mode = Config\Characters::ALL) : array
}

return array_map(function ($character) {
return new static($character);
return new static($character, $this->encoding);
}, $characters);
}

Expand All @@ -102,7 +102,7 @@ public function nth(int $step, int $offset = 0) : self

preg_match_all("/(?:^|(?:.|\p{L}|\w){{$length}})(.|\p{L}|\w)/u", $substring, $matches);

return new static(implode($matches[1]));
return new static(implode($matches[1]), $this->encoding);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Traits/Encodable.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function base64(string $mode = Config\Base64::ENCODE) : self
{
Config\Base64::validateOption($mode);

return new static($mode($this->string));
return new static($mode($this->string), $this->encoding);
}

/**
Expand All @@ -45,7 +45,7 @@ public function url(string $mode = Config\Url::ENCODE) : self
{
Config\Url::validateOption($mode);

return new static($mode($this->string));
return new static($mode($this->string), $this->encoding);
}

/**
Expand Down Expand Up @@ -81,6 +81,6 @@ public function hex(string $mode = Config\Hex::ENCODE) : self
throw new RuntimeException('Invalid mode');
}

return new static($string);
return new static($string, $this->encoding);
}
}
4 changes: 2 additions & 2 deletions src/Traits/Encryptable.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function encrypt(string $key, string $cipher = 'AES-128-CBC') : self
throw new EncryptionException('Failed to encrypt the string');
}

return new static(base64_encode($json));
return new static(base64_encode($json), $this->encoding);
}

/**
Expand Down Expand Up @@ -86,7 +86,7 @@ public function decrypt(string $key, string $cipher = 'AES-128-CBC') : self
throw new DecryptionException('Failed to decrypt the string');
}

return new static($plaintext);
return new static($plaintext, $this->encoding);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Traits/Hashable.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function crc32() : int
*/
public function crypt(string $salt) : self
{
return new static(crypt($this->string, $salt));
return new static(crypt($this->string, $salt), $this->encoding);
}

/**
Expand All @@ -45,7 +45,7 @@ public function md5(bool $mode = Config\Md5::DEFAULT) : self
{
Config\Md5::validateOption($mode);

return new static(hash('md5', $this->string, $mode));
return new static(hash('md5', $this->string, $mode), $this->encoding);
}

/**
Expand All @@ -64,7 +64,7 @@ public function sha1(bool $mode = Config\Md5::DEFAULT) : self
{
Config\Md5::validateOption($mode);

return new static(hash('sha1', $this->string, $mode));
return new static(hash('sha1', $this->string, $mode), $this->encoding);
}

/**
Expand All @@ -83,7 +83,7 @@ public function sha256(bool $mode = Config\Sha256::DEFAULT) : self
{
Config\Sha256::validateOption($mode);

return new static(hash('sha256', $this->string, $mode));
return new static(hash('sha256', $this->string, $mode), $this->encoding);
}

/**
Expand All @@ -95,6 +95,6 @@ public function sha256(bool $mode = Config\Sha256::DEFAULT) : self
*/
public function bcrypt(array $options = []) : self
{
return new static(password_hash($this->string, PASSWORD_BCRYPT, $options));
return new static(password_hash($this->string, PASSWORD_BCRYPT, $options), $this->encoding);
}
}
6 changes: 3 additions & 3 deletions src/Traits/Joinable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function append(string ...$strings) : self
{
array_unshift($strings, $this->string);

return new static(implode($strings));
return new static(implode($strings), $this->encoding);
}

/**
Expand All @@ -29,7 +29,7 @@ public function prepend(string ...$strings) : self
{
array_push($strings, $this->string);

return new static(implode($strings));
return new static(implode($strings), $this->encoding);
}

/**
Expand All @@ -42,6 +42,6 @@ public function prepend(string ...$strings) : self
*/
public function join(string $string, string $glue = ' ')
{
return new static($this->string . $glue . $string);
return new static($this->string . $glue . $string, $this->encoding);
}
}
4 changes: 2 additions & 2 deletions src/Traits/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function match(string $pattern) : self
{
preg_match($pattern, $this->string, $matches);

return new static($matches[0]);
return new static($matches[0], $this->encoding);
}

/**
Expand All @@ -30,7 +30,7 @@ public function matchAll(string $pattern) : array
preg_match_all($pattern, $this->string, $matches);

return array_map(function ($match) {
return new static($match);
return new static($match, $this->encoding);
}, $matches[0]);
}
}
17 changes: 9 additions & 8 deletions src/Traits/Segmentable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function substring(int $start, int $length = null) : self
{
$length = isset($length) ? $length : $this->length() - $start;

return new static(mb_substr($this->string, $start, $length, $this->encoding));
return new static(mb_substr($this->string, $start, $length, $this->encoding), $this->encoding);
}

/**
Expand All @@ -28,7 +28,7 @@ public function substring(int $start, int $length = null) : self
*/
public function before(string $string) : self
{
return new static(mb_split($string, $this->string, 2)[0]);
return new static(mb_split($string, $this->string, 2)[0], $this->encoding);
}

/**
Expand All @@ -40,7 +40,7 @@ public function before(string $string) : self
*/
public function after(string $string) : self
{
return new static(mb_split($string, $this->string, 2)[1]);
return new static(mb_split($string, $this->string, 2)[1], $this->encoding);
}

/**
Expand All @@ -52,7 +52,7 @@ public function after(string $string) : self
*/
public function from(string $string) : self
{
return new static(mb_strstr($this->string, $string, null, $this->encoding));
return new static(mb_strstr($this->string, $string, null, $this->encoding), $this->encoding);
}

/**
Expand All @@ -66,7 +66,7 @@ public function to(string $string) : self
{
$substring = mb_strstr($this->string, $string, true, $this->encoding);

return new static($substring ? $substring . $string : null);
return new static($substring ? $substring . $string : null, $this->encoding);
}

/**
Expand All @@ -80,7 +80,8 @@ public function to(string $string) : self
public function truncate(int $length, string $suffix = '...') : self
{
return new static(
$this->first($length - mb_strlen($suffix, $this->encoding))->trimRight()->append($suffix)
$this->first($length - mb_strlen($suffix, $this->encoding))->trimRight()->append($suffix),
$this->encoding
);
}

Expand All @@ -96,7 +97,7 @@ public function chunk(int $length) : array
preg_match_all("/(?:.|\p{L}|\w){1,{$length}}/u", $this->string, $chunks);

return array_map(function ($chunk) {
return new static($chunk);
return new static($chunk, $this->encoding);
}, $chunks[0]);
}

Expand All @@ -113,7 +114,7 @@ public function split(int $chunks) : array
preg_match_all("/(?:.|\p{L}|\w){1,{$length}}/u", $this->string, $chunks);

return array_map(function ($chunk) {
return new static($chunk);
return new static($chunk, $this->encoding);
}, $chunks[0]);
}
}
Loading

0 comments on commit 2f114b0

Please sign in to comment.