Skip to content

Commit

Permalink
refactor: use enums for representing tokens (PHP 8.1)
Browse files Browse the repository at this point in the history
  • Loading branch information
neoncitylights committed Apr 17, 2024
1 parent 74e82c0 commit f9544ee
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
}
},
"require": {
"neoncitylights/media-type": "^1.0"
"neoncitylights/media-type": "^1.0",
"php": ">= 8.1"
},
"require-dev": {
"mediawiki/mediawiki-codesniffer": "43.0.0",
Expand Down
8 changes: 4 additions & 4 deletions src/DataUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @see https://tools.ietf.org/html/rfc2397
* @license MIT
*/
class DataUrl implements DataUrlToken {
class DataUrl {
/** @var MediaType */
private $mediaType;

Expand Down Expand Up @@ -61,10 +61,10 @@ public function getDecodedValue(): string {
public function __toString(): string {
return sprintf(
'%s%s%s%s%s',
self::TOKEN_DATA_SCHEME,
Token::UriScheme->value,
(string)$this->mediaType,
self::TOKEN_BASE64_EXT,
self::TOKEN_COMMA,
Token::Base64Ext->value,
Token::Comma->value,
$this->data
);
}
Expand Down
10 changes: 5 additions & 5 deletions src/DataUrlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @see https://tools.ietf.org/html/rfc2397
* @license MIT
*/
class DataUrlParser implements DataUrlToken {
class DataUrlParser {
/**
* @param string $dataUrl
* @return DataUrl
Expand All @@ -36,15 +36,15 @@ public function parse( string $dataUrl ): DataUrl {
);
}

if ( strpos( $trimmedDataUrl, self::TOKEN_DATA_SCHEME ) === false ) {
if ( strpos( $trimmedDataUrl, Token::UriScheme->value ) === false ) {
throw new InvalidDataUrlSyntaxException(
"A valid data URL requires including a \"data:\" scheme.",
$dataUrl
);
}

$urlPath = substr( $trimmedDataUrl, strlen( self::TOKEN_DATA_SCHEME ) );
$lastCommaIndex = strrpos( $urlPath, self::TOKEN_COMMA );
$urlPath = substr( $trimmedDataUrl, strlen( Token::UriScheme->value ) );
$lastCommaIndex = strrpos( $urlPath, Token::Comma->value );
if ( $lastCommaIndex === false ) {
throw new InvalidDataUrlSyntaxException(
"A valid data URL requires including a comma character.",
Expand All @@ -66,7 +66,7 @@ public function parse( string $dataUrl ): DataUrl {
* @return MediaType
*/
private function parseMediaTypeAndBase64( string $content ): MediaType {
$base64ExtIndex = strrpos( $content, self::TOKEN_BASE64_EXT );
$base64ExtIndex = strrpos( $content, Token::Base64Ext->value );
if ( is_int( $base64ExtIndex ) ) {
$mediaTypeString = substr( $content, 0, $base64ExtIndex );
return $this->getMediaType( $mediaTypeString );
Expand Down
13 changes: 0 additions & 13 deletions src/DataUrlToken.php

This file was deleted.

13 changes: 13 additions & 0 deletions src/Token.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Neoncitylights\DataUrl;

/**
* @internal
* @license MIT
*/
enum Token: string {
case Base64Ext = ';base64';
case UriScheme = 'data:';
case Comma = ',';
}

0 comments on commit f9544ee

Please sign in to comment.