Skip to content

Commit bbd345e

Browse files
authored
Implement against InternetMediaTypeInterface (#4)
1 parent 1a5bbe3 commit bbd345e

File tree

5 files changed

+108
-142
lines changed

5 files changed

+108
-142
lines changed

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
],
1414
"autoload": {
1515
"psr-4": {
16-
"webignition\\InternetMediaType\\": "src/",
16+
"webignition\\InternetMediaType\\": "src/"
17+
}
18+
},
19+
"autoload-dev": {
20+
"psr-4": {
1721
"webignition\\Tests\\InternetMediaType\\": "tests/"
1822
}
1923
},
@@ -27,13 +31,15 @@
2731
},
2832
"require": {
2933
"php": ">=5.6.0",
34+
"webignition/internet-media-type-interface":">=0.1,<1.0",
3035
"webignition/quoted-string":">=0.2.1,<1.0",
3136
"webignition/string-parser":">=0.2.3,<1.0"
37+
3238
},
3339
"require-dev": {
3440
"phpunit/phpunit": "~5.0",
3541
"squizlabs/php_codesniffer": "3.*"
3642
},
37-
"minimum-stability":"dev",
43+
"minimum-stability":"stable",
3844
"prefer-stable":true
3945
}

composer.lock

Lines changed: 46 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/InternetMediaType.php

Lines changed: 22 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,11 @@
33
namespace webignition\InternetMediaType;
44

55
use webignition\InternetMediaType\Parameter\Parameter;
6+
use webignition\InternetMediaTypeInterface\InternetMediaTypeInterface;
7+
use webignition\InternetMediaTypeInterface\ParameterInterface;
68

7-
/**
8-
* Models an Internet Media Type as defined as:
9-
*
10-
* HTTP uses Internet Media Types [17] in the Content-Type (section 14.17) and
11-
* Accept (section 14.1) header fields in order to provide open and extensible data
12-
* typing and type negotiation.
13-
*
14-
* media-type = type "/" subtype *( ";" parameter )
15-
* type = token
16-
* subtype = token
17-
*
18-
* Parameters MAY follow the type/subtype in the form of attribute/value pairs
19-
*
20-
* parameter = attribute "=" value
21-
* attribute = token
22-
* value = token | quoted-string
23-
*
24-
* The type, subtype, and parameter attribute names are case-insensitive
25-
*
26-
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
27-
*
28-
* Note: may have multiple parameters as per Content-Type example
29-
* in http://www.w3.org/Protocols/rfc1341/rfc1341.html section 7.3.2:
30-
*
31-
* Content-Type: Message/Partial;
32-
* number=2; total=3;
33-
34-
*
35-
*/
36-
class InternetMediaType
9+
class InternetMediaType implements InternetMediaTypeInterface
3710
{
38-
const TYPE_SUBTYPE_SEPARATOR = '/';
39-
const PARAMETER_ATTRIBUTE_VALUE_SEPARATOR = '=';
40-
const ATTRIBUTE_PARAMETER_SEPARATOR = ';';
41-
4211
/**
4312
* Main media type.
4413
*
@@ -62,74 +31,62 @@ class InternetMediaType
6231
*
6332
* @var Parameter[]
6433
*/
65-
private $parameters = array();
34+
private $parameters = [];
6635

6736
/**
68-
* @param string $type
69-
*
70-
* @return self
37+
* {@inheritdoc}
7138
*/
7239
public function setType($type)
7340
{
7441
$this->type = strtolower($type);
75-
return $this;
7642
}
7743

7844
/**
79-
* @return string
45+
* {@inheritdoc}
8046
*/
8147
public function getType()
8248
{
8349
return $this->type;
8450
}
8551

8652
/**
87-
* @param string $subtype
88-
*
89-
* @return self
53+
* {@inheritdoc}
9054
*/
9155
public function setSubtype($subtype)
9256
{
9357
$this->subtype = strtolower($subtype);
94-
return $this;
9558
}
9659

9760
/**
98-
* @return string
61+
* {@inheritdoc}
9962
*/
10063
public function getSubtype()
10164
{
10265
return $this->subtype;
10366
}
10467

10568
/**
106-
* @param Parameter $parameter
107-
*
108-
* @return self
69+
* {@inheritdoc}
10970
*/
110-
public function addParameter(Parameter $parameter)
71+
public function addParameter(ParameterInterface $parameter)
11172
{
11273
$this->parameters[$parameter->getAttribute()] = $parameter;
11374

11475
return $this;
11576
}
11677

11778
/**
118-
* @param string $attribute
119-
*
120-
* @return bool
79+
* {@inheritdoc}
12180
*/
12281
public function hasParameter($attribute)
12382
{
12483
return !is_null($this->getParameter($attribute));
12584
}
12685

12786
/**
128-
* @param Parameter $parameter
129-
*
130-
* @return self
87+
* {@inheritdoc}
13188
*/
132-
public function removeParameter(Parameter $parameter)
89+
public function removeParameter(ParameterInterface $parameter)
13390
{
13491
if ($this->hasParameter($parameter->getAttribute())) {
13592
unset($this->parameters[$parameter->getAttribute()]);
@@ -139,9 +96,7 @@ public function removeParameter(Parameter $parameter)
13996
}
14097

14198
/**
142-
* @param string $attribute
143-
*
144-
* @return Parameter|null
99+
* {@inheritdoc}
145100
*/
146101
public function getParameter($attribute)
147102
{
@@ -151,37 +106,29 @@ public function getParameter($attribute)
151106
}
152107

153108
/**
154-
* Get collection of Parameter objects
155-
*
156-
* @return Parameter[]
109+
* {@inheritdoc}
157110
*/
158111
public function getParameters()
159112
{
160113
return $this->parameters;
161114
}
162115

163116
/**
164-
* @return bool
117+
* @return string
165118
*/
166-
public function hasType()
119+
public function getTypeSubtypeString()
167120
{
121+
$string = '';
122+
168123
if (empty($this->type)) {
169-
return false;
124+
return $string;
170125
}
171126

172-
return true;
173-
}
174-
175-
/**
176-
* @return bool
177-
*/
178-
public function hasSubtype()
179-
{
180127
if (empty($this->subtype)) {
181-
return false;
128+
return $string;
182129
}
183130

184-
return true;
131+
return $this->type . self::TYPE_SUBTYPE_SEPARATOR . $this->subtype;
185132
}
186133

187134
/**
@@ -225,24 +172,4 @@ private function isEmptyParameterStringCollection($parameterStringCollection)
225172

226173
return true;
227174
}
228-
229-
/**
230-
* Get a string of the form {type}/{subtype}
231-
*
232-
* @return string
233-
*/
234-
public function getTypeSubtypeString()
235-
{
236-
$string = '';
237-
238-
if (!$this->hasType()) {
239-
return $string;
240-
}
241-
242-
if (!$this->hasSubtype()) {
243-
return $string;
244-
}
245-
246-
return $this->getType() . self::TYPE_SUBTYPE_SEPARATOR . $this->getSubtype();
247-
}
248175
}

0 commit comments

Comments
 (0)