Skip to content

Commit cd797df

Browse files
authored
Include content type in parse failure exception (#14)
1 parent d9fa880 commit cd797df

17 files changed

+194
-289
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
sudo: false
21
language: php
32
php:
43
- 7.2

src/Parser/ParseException.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22

33
namespace webignition\InternetMediaType\Parser;
44

5+
use Throwable;
6+
57
class ParseException extends \Exception
68
{
9+
/**
10+
* @var string
11+
*/
12+
private $contentTypeString;
13+
14+
public function __construct(
15+
string $message = '',
16+
int $code = 0,
17+
string $contentTypeString = '',
18+
Throwable $previous = null
19+
) {
20+
parent::__construct($message, $code, $previous);
21+
22+
$this->contentTypeString = $contentTypeString;
23+
}
24+
25+
public function getContentTypeString(): string
26+
{
27+
return $this->contentTypeString;
28+
}
729
}

src/Parser/Parser.php

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,34 +52,56 @@ public function __construct()
5252
/**
5353
* @param string $internetMediaTypeString
5454
*
55-
* @return InternetMediaTypeInterface
55+
* @return InternetMediaTypeInterface|null
5656
*
57-
* @throws SubtypeParserException
58-
* @throws TypeParserException
59-
* @throws AttributeParserException
57+
* @throws ParseException
6058
*/
6159
public function parse(string $internetMediaTypeString): ?InternetMediaTypeInterface
6260
{
6361
$inputString = trim($internetMediaTypeString);
6462

6563
$internetMediaType = new InternetMediaType();
66-
$internetMediaType->setType($this->typeParser->parse($inputString));
67-
$internetMediaType->setSubtype($this->subtypeParser->parse($inputString));
6864

69-
$parameterString = $this->createParameterString(
70-
$inputString,
71-
$internetMediaType->getType(),
72-
$internetMediaType->getSubtype()
73-
);
74-
$parameterStrings = $this->getParameterStrings($parameterString);
65+
try {
66+
$internetMediaType->setType($this->typeParser->parse($inputString));
67+
$internetMediaType->setSubtype($this->subtypeParser->parse($inputString));
7568

76-
$parameters = $this->getParameters($parameterStrings);
69+
$parameterString = $this->createParameterString(
70+
$inputString,
71+
$internetMediaType->getType(),
72+
$internetMediaType->getSubtype()
73+
);
74+
$parameterStrings = $this->getParameterStrings($parameterString);
7775

78-
foreach ($parameters as $parameter) {
79-
$internetMediaType->addParameter($parameter);
80-
}
76+
$parameters = $this->getParameters($parameterStrings);
8177

82-
return $internetMediaType;
78+
foreach ($parameters as $parameter) {
79+
$internetMediaType->addParameter($parameter);
80+
}
81+
82+
return $internetMediaType;
83+
} catch (TypeParserException $typeParserException) {
84+
throw new ParseException(
85+
$typeParserException->getMessage(),
86+
$typeParserException->getCode(),
87+
$inputString,
88+
$typeParserException
89+
);
90+
} catch (SubtypeParserException $subtypeParserException) {
91+
throw new ParseException(
92+
$subtypeParserException->getMessage(),
93+
$subtypeParserException->getCode(),
94+
$inputString,
95+
$subtypeParserException
96+
);
97+
} catch (AttributeParserException $attributeParserException) {
98+
throw new ParseException(
99+
$attributeParserException->getMessage(),
100+
$attributeParserException->getCode(),
101+
$inputString,
102+
$attributeParserException
103+
);
104+
}
83105
}
84106

85107
/**

tests/InternetMediaTypeTest.php

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
/** @noinspection PhpDocSignatureInspection */
23

34
namespace webignition\Tests\InternetMediaType;
45

@@ -10,13 +11,6 @@ class InternetMediaTypeTest extends \PHPUnit\Framework\TestCase
1011
{
1112
/**
1213
* @dataProvider createDataProvider
13-
*
14-
* @param null|string $type
15-
* @param null|string $subtype
16-
* @param array $parameters
17-
* @param null|string $expectedType
18-
* @param null|string $expectedSubtype
19-
* @param array $expectedParameterStrings
2014
*/
2115
public function testCreate(
2216
?string $type,
@@ -103,11 +97,6 @@ public function createDataProvider(): array
10397

10498
/**
10599
* @dataProvider typeAndCastToStringDataProvider
106-
*
107-
* @param InternetMediaType $internetMediaType
108-
* @param string $expectedType
109-
* @param string $expectedSubtype
110-
* @param string $expectedString
111100
*/
112101
public function testTypeAndCastToString(
113102
InternetMediaType $internetMediaType,
@@ -176,10 +165,6 @@ public function typeAndCastToStringDataProvider(): array
176165

177166
/**
178167
* @dataProvider addParameterDataProvider
179-
*
180-
* @param InternetMediaType $internetMediaType
181-
* @param ParameterInterface $parameterToAdd
182-
* @param string[] $expectedParameters
183168
*/
184169
public function testAddParameter(
185170
InternetMediaType $internetMediaType,
@@ -227,10 +212,6 @@ public function addParameterDataProvider(): array
227212

228213
/**
229214
* @dataProvider hasParameterDataProvider
230-
*
231-
* @param InternetMediaType $internetMediaType
232-
* @param string $attribute
233-
* @param bool $expectedHasParameter
234215
*/
235216
public function testHasParameter(
236217
InternetMediaType $internetMediaType,
@@ -267,10 +248,6 @@ public function hasParameterDataProvider(): array
267248

268249
/**
269250
* @dataProvider removeParameterDataProvider
270-
*
271-
* @param InternetMediaType $internetMediaType
272-
* @param ParameterInterface $parameterToRemove
273-
* @param string[] $expectedParametersAsStrings
274251
*/
275252
public function testRemoveParameter(
276253
InternetMediaType $internetMediaType,
@@ -317,10 +294,6 @@ public function removeParameterDataProvider(): array
317294

318295
/**
319296
* @dataProvider getParameterDataProvider
320-
*
321-
* @param InternetMediaType $internetMediaType
322-
* @param string $attribute
323-
* @param ParameterInterface|null $expectedParameter
324297
*/
325298
public function testGetParameter(
326299
InternetMediaType $internetMediaType,
@@ -360,9 +333,6 @@ public function getParameterDataProvider(): array
360333

361334
/**
362335
* @dataProvider hasTypeHasSubtypeGetSubtypeStringDataProvider
363-
*
364-
* @param InternetMediaType $internetMediaType
365-
* @param string $expectedTypeSubtypeString
366336
*/
367337
public function testGetTypeSubtypeString(InternetMediaType $internetMediaType, string $expectedTypeSubtypeString)
368338
{

tests/Parameter/ParameterTest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
/** @noinspection PhpDocSignatureInspection */
23

34
namespace webignition\Tests\InternetMediaType\Parameter;
45

@@ -8,22 +9,15 @@ class ParameterTest extends \PHPUnit\Framework\TestCase
89
{
910
/**
1011
* @dataProvider castToStringDataProvider
11-
*
12-
* @param string $attribute
13-
* @param string $value
14-
* @param string $expectedParameterString
1512
*/
16-
public function testCastToString($attribute, $value, $expectedParameterString)
13+
public function testCastToString(string $attribute, ?string $value, string $expectedParameterString)
1714
{
1815
$parameter = new Parameter($attribute, $value);
1916

2017
$this->assertEquals($expectedParameterString, (string)$parameter);
2118
}
2219

23-
/**
24-
* @return array
25-
*/
26-
public function castToStringDataProvider()
20+
public function castToStringDataProvider(): array
2721
{
2822
return [
2923
'all lowercase' => [

tests/Parameter/Parser/AttributeFixerTest.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
/** @noinspection PhpDocSignatureInspection */
23

34
namespace webignition\Tests\InternetMediaType\Parameter\Parser;
45

@@ -8,22 +9,16 @@ class AttributeFixerTest extends \PHPUnit\Framework\TestCase
89
{
910
/**
1011
* @dataProvider fixValidAttributeDataProvider
11-
*
12-
* @param $attribute
13-
* @param $expectedFixedAttribute
1412
*/
15-
public function testFixValidAttribute($attribute, $expectedFixedAttribute)
13+
public function testFixValidAttribute(string $attribute, string $expectedFixedAttribute)
1614
{
1715
$attributeFixer = new AttributeFixer();
1816
$attributeFixer->setInputString($attribute);
1917

2018
$this->assertEquals($expectedFixedAttribute, $attributeFixer->fix());
2119
}
2220

23-
/**
24-
* @return array
25-
*/
26-
public function fixValidAttributeDataProvider()
21+
public function fixValidAttributeDataProvider(): array
2722
{
2823
return [
2924
'valid empty attribute' => [

tests/Parameter/Parser/AttributeParserExceptionTest.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
/** @noinspection PhpDocSignatureInspection */
23

34
namespace webignition\Tests\InternetMediaType\Parameter\Parser;
45

@@ -8,25 +9,21 @@ class AttributeParserExceptionTest extends \PHPUnit\Framework\TestCase
89
{
910
/**
1011
* @dataProvider createDataProvider
11-
*
12-
* @param string $message
13-
* @param int $code
14-
* @param int $position
15-
* @param bool $expectedIsInternalCharacterException
16-
* @param int $expectedPosition
1712
*/
18-
public function testCreate($message, $code, $position, $expectedIsInternalCharacterException, $expectedPosition)
19-
{
13+
public function testCreate(
14+
string $message,
15+
int $code,
16+
int $position,
17+
bool $expectedIsInternalCharacterException,
18+
int $expectedPosition
19+
) {
2020
$exception = new AttributeParserException($message, $code, $position);
2121

2222
$this->assertEquals($expectedIsInternalCharacterException, $exception->isInvalidInternalCharacterException());
2323
$this->assertEquals($expectedPosition, $exception->getPosition());
2424
}
2525

26-
/**
27-
* @return array
28-
*/
29-
public function createDataProvider()
26+
public function createDataProvider(): array
3027
{
3128
return [
3229
'is internal character exception' => [

0 commit comments

Comments
 (0)