-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup library to be more compliant
- Loading branch information
1 parent
e42dec9
commit 6d3addd
Showing
6 changed files
with
342 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,30 @@ | ||
# php-base64string | ||
A small PHP library for dealing with Base64 strings formatted as a data URI/URL. | ||
This library almost (but does not fully) conforms to RFC 2397. | ||
# DataUrl | ||
A small PHP library for dealing with data URLs, which contain a media type and an encoded base64 string. | ||
|
||
This library almost (but does not fully) conforms to RFC 2397. It currently does not yet allow `data:,`, which is a data URL with no defined media-type, and zero-length data. | ||
|
||
## Install | ||
``` | ||
composer require neoncitylights/data-url | ||
``` | ||
|
||
## Usage | ||
```php | ||
<?php | ||
|
||
use Neoncitylights\DataUrl\DataUrl; | ||
|
||
$dataUrl = DataUrl::newFromString( 'data:text/plain;base64,VGhlIGZpdmUgYm94aW5nIHdpemFyZHMganVtcCBxdWlja2x5Lg==' ); | ||
|
||
print( $dataUrl->getMediaType()->getEssence() ); | ||
// 'text/plain' | ||
|
||
print( $dataUrl->getData() ); | ||
// `VGhlIGZpdmUgYm94aW5nIHdpemFyZHMganVtcCBxdWlja2x5Lg==` | ||
|
||
print( $dataUrl->getDecodedValue() ); | ||
// 'The five boxing wizards jump quickly.' | ||
``` | ||
|
||
## References | ||
* Masinter, L., & X. (1998, August). The "data" URL scheme. Retrieved October 30, 2020, from https://tools.ietf.org/html/rfc2397 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace Neoncitylights\DataUrl; | ||
|
||
use Neoncitylights\MediaType\MediaType; | ||
use function base64_decode; | ||
use function sprintf; | ||
use function strlen; | ||
use function strrchr; | ||
use function strrpos; | ||
use function substr; | ||
|
||
/** | ||
* @see https://tools.ietf.org/html/rfc2397 | ||
* @license MIT | ||
*/ | ||
class DataUrl { | ||
private const TOKEN_COLON = ';'; | ||
private const TOKEN_DATA_SCHEME = 'data:'; | ||
private const TOKEN_BASE64_EXT = ';base64,'; | ||
|
||
/** @var MediaType */ | ||
private $mediaType; | ||
|
||
/** @var string */ | ||
private $data; | ||
|
||
/** @var string */ | ||
private $decodedValue; | ||
|
||
/** | ||
* @param MediaType $mediaType | ||
* @param string $data | ||
*/ | ||
public function __construct( MediaType $mediaType, string $data ) { | ||
$this->mediaType = $mediaType; | ||
$this->data = $data; | ||
} | ||
|
||
/** | ||
* Creates a Base64String object from a data URL. | ||
* | ||
* @param string $dataUrl | ||
* @return self|null | ||
*/ | ||
public static function newFromString( string $dataUrl ) : ?self { | ||
$stringFromLastColonToken = strrchr( $dataUrl, self::TOKEN_COLON ); | ||
$stringBeforeBase64 = substr( $dataUrl, 0, strrpos( $dataUrl, self::TOKEN_COLON ) ); | ||
|
||
$mediaType = substr( $stringBeforeBase64, strlen( self::TOKEN_DATA_SCHEME ) ); | ||
$data = substr( $stringFromLastColonToken, strlen( self::TOKEN_BASE64_EXT ) ); | ||
|
||
return new self( MediaType::newFromString( $mediaType ), $data ); | ||
} | ||
|
||
/** | ||
* Gets the media type of the encoded base64 string. | ||
* | ||
* @return MediaType | ||
*/ | ||
public function getMediaType() : MediaType { | ||
return $this->mediaType; | ||
} | ||
|
||
/** | ||
* Gets the original, encoded base64 string. | ||
* | ||
* @return string | ||
*/ | ||
public function getData() : string { | ||
return $this->data; | ||
} | ||
|
||
/** | ||
* Gets a decoded value of the base64 string. | ||
* | ||
* @return string | ||
*/ | ||
public function getDecodedValue() : string { | ||
if ( $this->decodedValue !== null ) { | ||
return $this->decodedValue; | ||
} | ||
|
||
$this->decodedValue = base64_decode( $this->data ); | ||
return $this->decodedValue; | ||
} | ||
|
||
/** | ||
* Returns a data URL compliant with RFC 2397. | ||
* | ||
* @return string | ||
*/ | ||
public function __toString() : string { | ||
return sprintf( | ||
'%s%s%s%s', | ||
self::TOKEN_DATA_SCHEME, | ||
(string)$this->mediaType, | ||
self::TOKEN_BASE64_EXT, | ||
$this->data | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.