Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: mark properties as public+readonly in MediaType #31

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,10 @@ use Neoncitylights\MediaType\MediaTypeParser;
$parser = new MediaTypeParser();
$mediaType = $parser->parseOrNull( 'text/plain;charset=UTF-8' );

print( $mediaType->getType() );
// 'text'

print( $mediaType->getSubType() );
// 'plain'

print( $mediaType->getEssence() );
// 'text/plain'

print( $mediaType->getParameterValue( 'charset' ) );
// 'UTF-8'
print( $mediaType->type ); // 'text'
print( $mediaType->subType ); // 'plain'
print( $mediaType->getEssence() ); // 'text/plain'
print( $mediaType->getParameterValue( 'charset' ) ); // 'UTF-8'
```

## License
Expand Down
82 changes: 29 additions & 53 deletions src/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,31 @@
*/
class MediaType implements Stringable {
/**
* Gives the first portion of a media type's essence.
* e.g, if given 'text/plain', it will return 'text'.
*
* @see https://mimesniff.spec.whatwg.org/#type
* @var string
*/
private $type;
public readonly string $type;

/**
* Gives the second portion of a media type's essence.
* e.g, if given 'text/plain', it will return 'plain'.
*
* @see https://mimesniff.spec.whatwg.org/#subtype
* @var string
*/
private $subType;
public readonly string $subType;

/**
* Gives an array of parameters of a media type,
* if any parameters exist. Otherwise, it will return an empty array.
*
* @see https://mimesniff.spec.whatwg.org/#parameters
* @var string[]
*/
private $parameters;
public readonly array $parameters;

/**
* @param string $type
Expand All @@ -39,28 +48,6 @@ public function __construct( string $type, string $subType, array $parameters )
$this->parameters = $parameters;
}

/**
* Gives the first portion of a media type's essence.
* e.g, if given 'text/plain', it will return 'text'.
*
* @see https://mimesniff.spec.whatwg.org/#type
* @return string
*/
public function getType(): string {
return $this->type;
}

/**
* Gives the second portion of a media type's essence.
* e.g, if given 'text/plain', it will return 'plain'.
*
* @see https://mimesniff.spec.whatwg.org/#subtype
* @return string
*/
public function getSubType(): string {
return $this->subType;
}

/**
* Gives the type and subtype of a media type.
* e.g, if given 'text/plain;charset=UTF-8', it will return 'text/plain'.
Expand All @@ -72,17 +59,6 @@ public function getEssence(): string {
return "{$this->type}/{$this->subType}";
}

/**
* Gives an array of parameters of a media type,
* if any parameters exist. Otherwise, it will return an empty array.
*
* @see https://mimesniff.spec.whatwg.org/#parameters
* @return array
*/
public function getParameters(): array {
return $this->parameters;
}

/**
* Gives the value of a specified parameter of a media type,
* if that parameter exists; otherwise, it will return a null value.
Expand All @@ -104,25 +80,25 @@ public function getParameterValue( string $parameterName ): ?string {
* @see https://mimesniff.spec.whatwg.org/#image-mime-type
*/
public function isImage(): bool {
return $this->getType() === 'image';
return $this->type === 'image';
}

/**
* https://mimesniff.spec.whatwg.org/#audio-or-video-mime-type
*/
public function isAudioOrVideo(): bool {
return $this->getType() === 'video'
|| $this->getType() === 'audio'
return $this->type === 'video'
|| $this->type === 'audio'
|| $this->getEssence() === 'application/ogg';
}

/**
* @see https://mimesniff.spec.whatwg.org/#font-mime-type
*/
public function isFont(): bool {
return $this->getType() === 'font'
|| ( $this->getType() === 'application'
&& \in_array( $this->getSubType(), [
return $this->type === 'font'
|| ( $this->type === 'application'
&& \in_array( $this->subType, [
'font-cff',
'font-off',
'font-sfnt',
Expand All @@ -137,23 +113,23 @@ public function isFont(): bool {
* @see https://mimesniff.spec.whatwg.org/#zip-based-mime-type
*/
public function isZipBased(): bool {
return \str_ends_with( $this->getSubType(), '+zip' )
return \str_ends_with( $this->subType, '+zip' )
|| $this->getEssence() === 'application/zip';
}

/**
* @see https://mimesniff.spec.whatwg.org/#archive-mime-type
*/
public function isArchive(): bool {
return $this->getType() === 'application'
&& \in_array( $this->getSubType(), [ 'x-rar-compressed', 'zip', 'x-gzip' ] );
return $this->type === 'application'
&& \in_array( $this->subType, [ 'x-rar-compressed', 'zip', 'x-gzip' ] );
}

/**
* @see https://mimesniff.spec.whatwg.org/#xml-mime-type
*/
public function isXml(): bool {
return \str_ends_with( $this->getSubType(), '+xml' )
return \str_ends_with( $this->subType, '+xml' )
|| $this->getEssence() === 'text/xml'
|| $this->getEssence() === 'application/xml';
}
Expand Down Expand Up @@ -181,16 +157,16 @@ public function isScriptable(): bool {
*/
public function isJavaScript(): bool {
return (
$this->getType() === 'application'
&& \in_array( $this->getSubType(), [
$this->type === 'application'
&& \in_array( $this->subType, [
'ecmascript',
'javascript',
'x-ecmascript',
'x-javascript'
] ) )
|| (
$this->getType() === 'text'
&& \in_array( $this->getSubType(), [
$this->type === 'text'
&& \in_array( $this->subType, [
'ecmascript',
'javascript',
'javascript1.0',
Expand All @@ -210,7 +186,7 @@ public function isJavaScript(): bool {
* @see https://mimesniff.spec.whatwg.org/#json-mime-type
*/
public function isJson(): bool {
return \str_ends_with( $this->getSubType(), '+json' )
return \str_ends_with( $this->subType, '+json' )
|| $this->getEssence() === 'application/json'
|| $this->getEssence() === 'text/json';
}
Expand All @@ -221,12 +197,12 @@ public function isJson(): bool {
*/
public function __toString(): string {
$essence = $this->getEssence();
if ( $this->getParameters() === [] ) {
if ( $this->parameters === [] ) {
return $essence;
}

$serializedParameters = '';
foreach ( $this->getParameters() as $parameter => $value ) {
foreach ( $this->parameters as $parameter => $value ) {
$serializedParameters .= ";{$parameter}=";
$serializedValue = $value;

Expand Down
6 changes: 3 additions & 3 deletions tests/MediaTypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function testValidMediaTypes(
$parsedValue = $this->parser->parse( $validMediaType );

$this->assertInstanceOf( MediaType::class, $parsedValue );
$this->assertEquals( $expectedType, $parsedValue->getType() );
$this->assertEquals( $expectedSubType, $parsedValue->getSubType() );
$this->assertEquals( $expectedParameters, $parsedValue->getParameters() );
$this->assertEquals( $expectedType, $parsedValue->type );
$this->assertEquals( $expectedSubType, $parsedValue->subType );
$this->assertEquals( $expectedParameters, $parsedValue->parameters );
}

/**
Expand Down
72 changes: 0 additions & 72 deletions tests/MediaTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,6 @@ public function testConstructor(): void {
);
}

/**
* @covers ::getType
* @dataProvider provideTypes
*/
public function testGetType( string $subType, string $type ): void {
$this->assertEquals(
$type,
( new MediaType( $type, $subType, [] ) )->getType()
);
}

/**
* @covers ::getSubType
* @dataProvider provideSubTypes
*/
public function testGetSubType( string $subType, string $type ): void {
$this->assertEquals(
$subType,
( new MediaType( $type, $subType, [] ) )->getSubType()
);
}

/**
* @covers ::getEssence
* @dataProvider provideEssences
Expand All @@ -52,22 +30,6 @@ public function testGetEssence( string $type, string $subType, string $expectedE
);
}

/**
* @covers ::getParameters
* @dataProvider provideParameters
*/
public function testGetParameters(
string $type,
string $subType,
array $givenParameters,
array $expectedParameters
): void {
$this->assertEquals(
$expectedParameters,
( new MediaType( $type, $subType, $givenParameters ) )->getParameters()
);
}

/**
* @covers ::getParameterValue
* @dataProvider provideParameterValues
Expand Down Expand Up @@ -107,40 +69,6 @@ public function provideInvalidMediaTypes() {
];
}

public function provideTypes() {
return [
[
'text',
'text/plain',
],
[
'application',
'application/xhtml+xml',
],
[
'application',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
],
];
}

public function provideSubTypes() {
return [
[
'plain',
'text/plain',
],
[
'xhtml+xml',
'application/xhtml+xml',
],
[
'vnd.openxmlformats-officedocument.presentationml.presentation',
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
],
];
}

public function provideEssences() {
return [
[
Expand Down