Skip to content

Commit

Permalink
Cleanup library to be more compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
neoncitylights committed Oct 30, 2020
1 parent e42dec9 commit 6d3addd
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 227 deletions.
33 changes: 30 additions & 3 deletions README.md
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., &amp; X. (1998, August). The "data" URL scheme. Retrieved October 30, 2020, from https://tools.ietf.org/html/rfc2397
14 changes: 11 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
{
"name": "neoncitylights/base64-string",
"name": "neoncitylights/data-url",
"authors": [
{
"name": "Samantha Nguyen"
}
],
"license": "MIT",
"description": "Allows working with base 64 strings as entity objects in PHP",
"description": "Allows working with data URLs as entity objects in PHP",
"keywords": [
"data url",
"data uri",
"rfc 2397"
],
"autoload": {
"psr-4": {
"Neoncitylights\\Base64String\\": "src/"
"Neoncitylights\\DataUrl\\": "src/"
}
},
"require": {
"neoncitylights/media-type": "^1.0"
},
"require-dev": {
"mediawiki/mediawiki-codesniffer": "32.0.0",
"mediawiki/minus-x": "1.1.0",
Expand Down
107 changes: 0 additions & 107 deletions src/Base64String.php

This file was deleted.

102 changes: 102 additions & 0 deletions src/DataUrl.php
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
);
}
}
114 changes: 0 additions & 114 deletions tests/Base64StringTest.php

This file was deleted.

Loading

0 comments on commit 6d3addd

Please sign in to comment.