Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
yudhatamaaditiyara committed Jul 11, 2019
1 parent 9358ab5 commit d1f97d4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Wayang/Stdlib/Base64Url/Base64Url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Copyright (C) 2019 Yudha Tama Aditiyara
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Wayang\Stdlib\Base64Url;

use Wayang\Exception\Spl\InvalidArgumentException;

/**
*/
class Base64Url implements Base64UrlInterface
{
/**
* @param string $data
* @return string
*/
public function encode(string $data): string{
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

/**
* @param string $data
* @throws InvalidArgumentException
* @return string
*/
public function decode(string $data): string{
$data.= str_repeat('=', 4 - strlen($data) % 4);
$data = base64_decode(strtr($data, '-_', '+/'), true);
if ($data === false) {
throw new InvalidArgumentException('Invalid data');
}
return $data;
}
}
34 changes: 34 additions & 0 deletions src/Wayang/Stdlib/Base64Url/Base64UrlInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright (C) 2019 Yudha Tama Aditiyara
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Wayang\Stdlib\Base64Url;

/**
*/
interface Base64UrlInterface
{
/**
* @param string $data
* @return string
*/
public function encode(string $data): string;

/**
* @param string $data
* @return string
*/
public function decode(string $data): string;
}

0 comments on commit d1f97d4

Please sign in to comment.