Skip to content

Commit

Permalink
add content-type matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Vaughn committed Jul 9, 2018
1 parent 9d528b4 commit ef96679
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/Content/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public static function newFromString($string) {
if(count($typeParts) !== 2) {
return null;
}
$mainType = strtolower($typeParts[0]);
$subType = strtolower($typeParts[1]);
$mainType = $typeParts[0];
$subType = $typeParts[1];
$parameters = [];
array_shift($parts);
foreach($parts as $part) {
Expand Down Expand Up @@ -87,9 +87,12 @@ public static function newFromString($string) {
* @param string[] $parameters - key value pairs of parameters (ex: ['charset' => 'utf-8']
*/
public function __construct($mainType, $subType, array $parameters = []) {
$this->mainType = $mainType;
$this->subType = $subType;
$this->parameters = $parameters;
$this->mainType = strtolower($mainType);
$this->subType = strtolower($subType);
$this->parameters = [];
foreach($parameters as $parameter => $value) {
$this->parameters[strtolower($parameter)] = strtolower($value);
}
}

/**
Expand All @@ -99,6 +102,17 @@ public function __toString() {
return $this->toString();
}

/**
* @param ContentType $contentType
* @param bool $includeParameters - include parameters when matching content-type string (default: false)
* @return bool
*/
public function is(ContentType $contentType, $includeParameters = false) {
return $includeParameters
? $this->toString() === $contentType->toString()
: $this->mainType === $contentType->mainType && $this->subType === $contentType->subType;
}

/**
* @return bool
*/
Expand Down
97 changes: 97 additions & 0 deletions tests/Content/ContentType/is_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* MindTouch HTTP
* Copyright (C) 2006-2018 MindTouch, Inc.
* www.mindtouch.com [email protected]
*
* 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 MindTouch\Http\tests\Content\SerializedPhpArrayContent;

use MindTouch\Http\Content\ContentType;
use MindTouch\Http\tests\MindTouchHttpUnitTestCase;

class is_Test extends MindTouchHttpUnitTestCase {

/**
* @test
*/
public function Can_match_content_type_with_parameters() {

// arrange
$contentType1 = ContentType::newFromString(ContentType::XML);
$contentType2 = ContentType::newFromString(ContentType::XML);

// act
$result1 = $contentType1->is($contentType2, true);
$result2 = $contentType2->is($contentType1, true);

// assert
$this->assertTrue($result1);
$this->assertTrue($result2);
}

/**
* @test
*/
public function Can_match_content_type_without_parameters() {

// arrange
$contentType1 = ContentType::newFromString('text/html; charset=latin');
$contentType2 = ContentType::newFromString('text/html; charset=utf-8');

// act
$result1 = $contentType1->is($contentType2);
$result2 = $contentType2->is($contentType1);

// assert
$this->assertTrue($result1);
$this->assertTrue($result2);
}

/**
* @test
*/
public function Can_fail_if_content_type_not_matched_with_parameters() {

// arrange
$contentType1 = ContentType::newFromString('text/html; charset=utf-8');
$contentType2 = ContentType::newFromString('text/css; charset=utf-8');

// act
$result1 = $contentType1->is($contentType2);
$result2 = $contentType2->is($contentType1);

// assert
$this->assertFalse($result1);
$this->assertFalse($result2);
}

/**
* @test
*/
public function Can_fail_if_content_type_not_matched_without_parameters() {

// arrange
$contentType1 = ContentType::newFromString('text/html; charset=utf-8');
$contentType2 = ContentType::newFromString('text/html; charset=latin');

// act
$result1 = $contentType1->is($contentType2, true);
$result2 = $contentType2->is($contentType1, true);

// assert
$this->assertFalse($result1);
$this->assertFalse($result2);
}
}

0 comments on commit ef96679

Please sign in to comment.