Skip to content

Commit

Permalink
determine default encoding upon use (instead of at instanciation)
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Feb 11, 2019
1 parent a7cf005 commit d4ebeb0
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Str implements PrimitiveInterface, StringableInterface
public function __construct(string $value, string $encoding = null)
{
$this->value = $value;
$this->encoding = $encoding ?? \mb_internal_encoding();
$this->encoding = $encoding;
}

public static function of(string $value, string $encoding = null): self
Expand All @@ -52,6 +52,10 @@ public function __toString(): string

public function encoding(): self
{
if (\is_null($this->encoding)) {
$this->encoding = \mb_internal_encoding();
}

return new self($this->encoding);
}

Expand Down Expand Up @@ -115,7 +119,7 @@ public function chunk(int $size = 1): StreamInterface
*/
public function position(string $needle, int $offset = 0): int
{
$position = \mb_strpos($this->value, $needle, $offset, $this->encoding);
$position = \mb_strpos($this->value, $needle, $offset, (string) $this->encoding());

if ($position === false) {
throw new SubstringException(\sprintf(
Expand Down Expand Up @@ -157,7 +161,7 @@ public function replace(string $search, string $replacement): self
*/
public function str(string $delimiter): self
{
$sub = \mb_strstr($this->value, $delimiter, false, $this->encoding);
$sub = \mb_strstr($this->value, $delimiter, false, (string) $this->encoding());

if ($sub === false) {
throw new SubstringException(\sprintf(
Expand Down Expand Up @@ -196,7 +200,7 @@ public function toLower(): self
*/
public function length(): int
{
return \mb_strlen($this->value, $this->encoding);
return \mb_strlen($this->value, (string) $this->encoding());
}

public function empty(): bool
Expand Down Expand Up @@ -482,7 +486,7 @@ public function substring(int $start, int $length = null): self
return $this;
}

$sub = \mb_substr($this->value, $start, $length, $this->encoding);
$sub = \mb_substr($this->value, $start, $length, (string) $this->encoding());

return new self($sub, $this->encoding);
}
Expand Down Expand Up @@ -556,7 +560,7 @@ public function camelize(): self
return $part->ucfirst();
})
->join('')
->toEncoding($this->encoding);
->toEncoding((string) $this->encoding());
}

/**
Expand Down

0 comments on commit d4ebeb0

Please sign in to comment.