Skip to content

Commit

Permalink
Set the string's internal encoding on Str object creation
Browse files Browse the repository at this point in the history
  • Loading branch information
PHLAK committed Oct 19, 2019
1 parent 2f114b0 commit c9b96e7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Str implements \ArrayAccess, \JsonSerializable, \Serializable
*/
public function __construct($string = '', $encoding = null)
{
$this->string = (string) $string;
$this->encoding = $encoding ?? Config\Str::getEncoding();
$this->string = mb_convert_encoding((string) $string, $this->encoding);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Methods/Md5Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function test_it_can_be_hashed_with_md5()

$this->assertInstanceOf(Twine\Str::class, $md5);
$this->assertEquals('30cac4703a16a2201ec5cafbd600d803', $md5);
$this->assertEquals(base64_decode('MMrEcDoWoiAexcr71gDYAw=='), $raw);
$this->assertEquals(base64_decode('MD8/cDoWPyAePz8/PwA/Aw=='), $raw);
}

public function test_a_multibyte_string_can_be_hashed_with_md5()
Expand All @@ -28,7 +28,7 @@ public function test_a_multibyte_string_can_be_hashed_with_md5()

$this->assertInstanceOf(Twine\Str::class, $md5);
$this->assertEquals('c5d37b31d718f00ddb370839a847f44f', $md5);
$this->assertEquals(base64_decode('xdN7MdcY8A3bNwg5qEf0Tw=='), $raw);
$this->assertEquals(base64_decode('Pz97MT8YPw0/Nwg5P0c/Tw=='), $raw);
}

public function test_it_preserves_encoding()
Expand Down
4 changes: 2 additions & 2 deletions tests/Methods/Sha1Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function test_it_can_be_hashed_with_sha1()

$this->assertInstanceOf(Twine\Str::class, $sha1);
$this->assertEquals('fcaf28c7705ba8f267472bb5aa8ad883f6bf0427', $sha1);
$this->assertEquals(base64_decode('/K8ox3BbqPJnRyu1qorYg/a/BCc='), $raw);
$this->assertEquals(base64_decode('Pz8oP3BbPz9nRys/Pz/Ygz8/BCc='), $raw);
}

public function test_a_multibyte_string_can_be_hashed_with_sha1()
Expand All @@ -28,7 +28,7 @@ public function test_a_multibyte_string_can_be_hashed_with_sha1()

$this->assertInstanceOf(Twine\Str::class, $sha1);
$this->assertEquals('606d67644969b213dbd54696de4b428caa4acb1f', $sha1);
$this->assertEquals(base64_decode('YG1nZElpshPb1UaW3ktCjKpKyx8='), $raw);
$this->assertEquals(base64_decode('YG1nZElpPxM/P0Y/P0tCPz9KPx8='), $raw);
}

public function test_it_preserves_encoding()
Expand Down
4 changes: 2 additions & 2 deletions tests/Methods/Sha256Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function test_it_can_be_hashed_with_sha256()

$this->assertInstanceOf(Twine\Str::class, $sha256);
$this->assertEquals('7434f26c8c2fc83e57347feb2dfb235c2f47b149b54b80692beca9d565159dfd', $sha256);
$this->assertEquals(base64_decode('dDTybIwvyD5XNH/rLfsjXC9HsUm1S4BpK+yp1WUVnf0='), $raw);
$this->assertEquals(base64_decode('dDQ/bD8vPz5XNH8/LT8jXC9HP0k/Sz9pKz8/ZRU/Pw=='), $raw);
}

public function test_a_multibyte_string_can_be_hashed_with_sha256()
Expand All @@ -28,7 +28,7 @@ public function test_a_multibyte_string_can_be_hashed_with_sha256()

$this->assertInstanceOf(Twine\Str::class, $sha256);
$this->assertEquals('23b55193cb08e619247b7e1ba65bfc0f5863f73ee3615e5b0dc7101c80c4302f', $sha256);
$this->assertEquals(base64_decode('I7VRk8sI5hkke34bplv8D1hj9z7jYV5bDccQHIDEMC8='), $raw);
$this->assertEquals(base64_decode('Iz9RPz8IPxkke34bP1s/D1hjPz4/YV5bDT8QHD8/MC8='), $raw);
}

public function test_it_preserves_encoding()
Expand Down

0 comments on commit c9b96e7

Please sign in to comment.