aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-26 15:21:51 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2015-05-26 15:21:51 +0200
commit1dbc518199e35d7da9dcbcc9004aef045aedfc86 (patch)
treeda7a3f829dbef2d1c5b158f15769a54fb5744c73
parent98e9170670f3463cfc0b90ce087cd4114408dd33 (diff)
downloadMail-1dbc518199e35d7da9dcbcc9004aef045aedfc86.zip
Mail-1dbc518199e35d7da9dcbcc9004aef045aedfc86.tar.gz
Mail-1dbc518199e35d7da9dcbcc9004aef045aedfc86.tar.bz2
Add the Base64 encoder.
Please, see RFC4648.
-rw-r--r--Content/Encoder/Base64.php83
-rw-r--r--Test/Unit/Content/Encoder/Base64.php90
2 files changed, 173 insertions, 0 deletions
diff --git a/Content/Encoder/Base64.php b/Content/Encoder/Base64.php
new file mode 100644
index 0000000..303204a
--- /dev/null
+++ b/Content/Encoder/Base64.php
@@ -0,0 +1,83 @@
+<?php
+
+/**
+ * Hoa
+ *
+ *
+ * @license
+ *
+ * New BSD License
+ *
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Hoa nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+namespace Hoa\Mail\Content\Encoder;
+
+/**
+ * Class \Hoa\Mail\Content\Encoder\Base64.
+ *
+ * Encode and decode a string as described in the RFC4648. Also respect the
+ * column limitation.
+ *
+ * @copyright Copyright © 2007-2015 Hoa community
+ * @license New BSD License
+ */
+class Base64 implements Encoder
+{
+ /**
+ * Encode into base64.
+ *
+ * @param string $string String to encode.
+ * @return string
+ */
+ public static function encode($string)
+ {
+ return trim(
+ chunk_split(
+ base64_encode($string),
+ 76,
+ CRLF
+ )
+ );
+ }
+
+ /**
+ * Decode from base64.
+ *
+ * @param string $string String to decode.
+ * @return string
+ */
+ public static function decode($string)
+ {
+ return base64_decode(
+ str_replace(
+ CRLF,
+ '',
+ $string
+ )
+ );
+ }
+}
diff --git a/Test/Unit/Content/Encoder/Base64.php b/Test/Unit/Content/Encoder/Base64.php
new file mode 100644
index 0000000..55d50b8
--- /dev/null
+++ b/Test/Unit/Content/Encoder/Base64.php
@@ -0,0 +1,90 @@
+<?php
+
+/**
+ * Hoa
+ *
+ *
+ * @license
+ *
+ * New BSD License
+ *
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Hoa nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+namespace Hoa\Mail\Test\Unit\Content\Encoder;
+
+use Hoa\Mail\Content\Encoder\Base64 as SUT;
+use Hoa\Test;
+
+/**
+ * Class \Hoa\Mail\Test\Unit\Content\Encoder\Base64.
+ *
+ * Test suite of the base64 encoder.
+ *
+ * @copyright Copyright © 2007-2015 Hoa community
+ * @license New BSD License
+ */
+class Base64 extends Test\Unit\Suite
+{
+ public function case_encode_decode()
+ {
+ $this
+ ->given(
+ $decoded = 'foobar',
+ $encoded = 'Zm9vYmFy'
+ )
+ ->when($result = SUT::encode($decoded))
+ ->then
+ ->string($result)
+ ->isEqualTo($encoded)
+
+ ->when($result = SUT::decode($result))
+ ->then
+ ->string($result)
+ ->isEqualTo($decoded);
+ }
+
+ public function case_long_encode_decode()
+ {
+ $this
+ ->given(
+ $decoded = str_repeat('foobar', 15),
+ $encoded =
+ 'Zm9vYmFyZm9vYmFyZm9vYmFyZm9vYmFyZm9vYmFyZm9vYmFyZm9v' .
+ 'YmFyZm9vYmFyZm9vYmFyZm9v' . CRLF .
+ 'YmFyZm9vYmFyZm9vYmFyZm9vYmFyZm9vYmFyZm9vYmFy'
+ )
+ ->when($result = SUT::encode($decoded))
+ ->then
+ ->string($result)
+ ->isEqualTo($encoded)
+
+ ->when($result = SUT::decode($result))
+ ->then
+ ->string($result)
+ ->isEqualTo($decoded);
+ }
+}