Page MenuHomeDevCentral

No OneTemporary

diff --git a/src/Strings/Multibyte/StringUtilities.php b/src/Strings/Multibyte/StringUtilities.php
index c06d9f2..2dbf319 100644
--- a/src/Strings/Multibyte/StringUtilities.php
+++ b/src/Strings/Multibyte/StringUtilities.php
@@ -1,60 +1,88 @@
<?php
declare(strict_types=1);
namespace Keruald\OmniTools\Strings\Multibyte;
class StringUtilities {
/**
* Pads a multibyte string to a certain length with another string
*
* @param string $input the input string
* @param int $padLength the target string size
* @param string $padString the padding characters (optional, default is space)
* @param int $padType STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH (optional, default is STR_PAD_RIGHT)
* @param string $encoding the character encoding (optional)
*
* @return string the padded string
*
*/
public static function pad (
string $input,
int $padLength,
string $padString = ' ',
int $padType = STR_PAD_RIGHT,
string $encoding = ''
) : string {
return (new StringPad)
->setInput($input)
->setPadLength($padLength)
->setPadString($padString)
->setPadType($padType)
->setEncoding($encoding ?: mb_internal_encoding())
->pad();
}
public static function isSupportedEncoding (string $encoding) : bool {
foreach (mb_list_encodings() as $supportedEncoding) {
if ($encoding === $supportedEncoding) {
return true;
}
}
return false;
}
public static function startsWith (string $string, string $start) {
$length = mb_strlen($start);
return mb_substr($string, 0, $length) === $start;
}
public static function endsWith (string $string, string $end) {
$length = mb_strlen($end);
return $length === 0 || mb_substr($string, -$length) === $end;
}
public static function contains (string $string, string $needle) : bool {
return strpos($string, $needle) !== false;
}
+ /**
+ * Encode a string using a variant of the MIME base64 compatible with URLs.
+ *
+ * The + and / characters used in base64 are replaced by - and _.
+ * The = padding is removed.
+ *
+ * @param string $string The string to encode
+ * @return string The encoded string
+ */
+ public static function encodeInBase64 (string $string) : string {
+ return str_replace(
+ ['+', '/', '='],
+ ['-', '_', ''],
+ base64_encode($string)
+ );
+ }
+
+ /**
+ * Decode a string encoded with StringUtilities::encodeInBase64
+ *
+ * @param string $string The string to decode
+ * @return string The decoded string
+ */
+ public static function decodeFromBase64 (string $string) : string {
+ $toDecode = str_replace(['-', '_'], ['+', '/'], $string);
+ return base64_decode($toDecode);
+ }
+
}
diff --git a/tests/Strings/Multibyte/StringUtilitiesTest.php b/tests/Strings/Multibyte/StringUtilitiesTest.php
index 8b848e3..1bbdffe 100644
--- a/tests/Strings/Multibyte/StringUtilitiesTest.php
+++ b/tests/Strings/Multibyte/StringUtilitiesTest.php
@@ -1,92 +1,122 @@
<?php
declare(strict_types=1);
namespace Keruald\OmniTools\Tests\Strings\Multibyte;
use Keruald\OmniTools\Strings\Multibyte\StringUtilities;
use PHPUnit\Framework\TestCase;
class StringUtilitiesTest extends TestCase {
///
/// Tests
///
/**
* @dataProvider providePadding
*/
public function testPad (
string $expected,
string $input, int $padLength, string $padString, int $padType
) : void {
$paddedString = StringUtilities::pad(
$input, $padLength, $padString, $padType, "UTF-8"
);
$this->assertEquals($expected, $paddedString);
}
public function testPadWithDefaultArguments () : void {
$this->assertEquals("foo ", StringUtilities::pad("foo", 4));
$this->assertEquals("foo_", StringUtilities::pad("foo", 4, '_'));
$this->assertEquals("_foo", StringUtilities::pad("foo", 4, '_', STR_PAD_LEFT));
}
public function testSupportedEncoding () : void {
$this->assertTrue(StringUtilities::isSupportedEncoding("UTF-8"));
$this->assertFalse(StringUtilities::isSupportedEncoding("notexisting"));
}
public function testStartsWith () : void {
$this->assertTrue(StringUtilities::startsWith("foo", "fo"));
$this->assertTrue(StringUtilities::startsWith("foo", ""));
$this->assertTrue(StringUtilities::startsWith("foo", "foo"));
$this->assertFalse(StringUtilities::startsWith("foo", "bar"));
}
public function testEndsWith () : void {
$this->assertTrue(StringUtilities::endsWith("foo", "oo"));
$this->assertTrue(StringUtilities::endsWith("foo", ""));
$this->assertTrue(StringUtilities::endsWith("foo", "foo"));
$this->assertFalse(StringUtilities::endsWith("foo", "oO"));
$this->assertFalse(StringUtilities::endsWith("foo", "bar"));
}
+ /**
+ * @dataProvider provideBase64
+ */
+ public function testEncodeInBase64 (string $decoded, string $encoded) : void {
+ $actual = StringUtilities::encodeInBase64($decoded);
+ $this->assertEquals($encoded, $actual);
+ }
+
+ /**
+ * @dataProvider provideBase64
+ */
+ public function testDecodeFromBase64 (string $decoded, string $encoded) : void {
+ $actual = StringUtilities::decodeFromBase64($encoded);
+ $this->assertEquals($decoded, $actual);
+ }
+
///
/// Data providers
///
public function providePadding () : iterable {
// Tests from http://3v4l.org/UnXTF
// http://web.archive.org/web/20150711100913/http://3v4l.org/UnXTF
yield ['àèòàFOOàèòà', "FOO", 11, "àèò", STR_PAD_BOTH];
yield ['àèòFOOàèòà', "FOO", 10, "àèò", STR_PAD_BOTH];
yield ['àèòBAAZàèòà', "BAAZ", 11, "àèò", STR_PAD_BOTH];
yield ['àèòBAAZàèò', "BAAZ", 10, "àèò", STR_PAD_BOTH];
yield ['FOOBAR', "FOOBAR", 6, "àèò", STR_PAD_BOTH];
yield ['FOOBAR', "FOOBAR", 1, "àèò", STR_PAD_BOTH];
yield ['FOOBAR', "FOOBAR", 0, "àèò", STR_PAD_BOTH];
yield ['FOOBAR', "FOOBAR", -10, "àèò", STR_PAD_BOTH];
yield ['àèòàèòàèFOO', "FOO", 11, "àèò", STR_PAD_LEFT];
yield ['àèòàèòàFOO', "FOO", 10, "àèò", STR_PAD_LEFT];
yield ['àèòàèòàBAAZ', "BAAZ", 11, "àèò", STR_PAD_LEFT];
yield ['àèòàèòBAAZ', "BAAZ", 10, "àèò", STR_PAD_LEFT];
yield ['FOOBAR', "FOOBAR", 6, "àèò", STR_PAD_LEFT];
yield ['FOOBAR', "FOOBAR", 1, "àèò", STR_PAD_LEFT];
yield ['FOOBAR', "FOOBAR", 0, "àèò", STR_PAD_LEFT];
yield ['FOOBAR', "FOOBAR", -10, "àèò", STR_PAD_LEFT];
yield ['FOOàèòàèòàè', "FOO", 11, "àèò", STR_PAD_RIGHT];
yield ['FOOàèòàèòà', "FOO", 10, "àèò", STR_PAD_RIGHT];
yield ['BAAZàèòàèòà', "BAAZ", 11, "àèò", STR_PAD_RIGHT];
yield ['BAAZàèòàèò', "BAAZ", 10, "àèò", STR_PAD_RIGHT];
yield ['FOOBAR', "FOOBAR", 6, "àèò", STR_PAD_RIGHT];
yield ['FOOBAR', "FOOBAR", 1, "àèò", STR_PAD_RIGHT];
yield ['FOOBAR', "FOOBAR", 0, "àèò", STR_PAD_RIGHT];
yield ['FOOBAR', "FOOBAR", -10, "àèò", STR_PAD_RIGHT];
}
+
+ public function provideBase64 () : iterable {
+ yield ['foo', 'Zm9v', "This is the regular base test without any exception."];
+ yield ['', '', "An empty string should remain an empty string."];
+ yield [
+ "J'ai fait mes 60 prières par terre dans la poudrière.",
+ 'SidhaSBmYWl0IG1lcyA2MCBwcmnDqHJlcyBwYXIgdGVycmUgZGFucyBsYSBwb3VkcmnDqHJlLg',
+ "No padding should be used."
+ ];
+ yield [
+ "àèòàFOOàèòà", "w6DDqMOyw6BGT0_DoMOow7LDoA",
+ "Slashes / should be replaced by underscores _."
+ ];
+ }
}

File Metadata

Mime Type
text/x-diff
Expires
Tue, Jan 28, 08:28 (6 h, 4 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2380184
Default Alt Text
(8 KB)

Event Timeline