Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 459f1f9

Browse files
committed
Merge remote-tracking branch 'Freeaqingme/acceptHandling'
PR zendframework/zendframework#1887
5 parents e0c8424 + 5ef6a39 + 58fd018 + 65e3b0b + d8deffa commit 459f1f9

15 files changed

+996
-159
lines changed

src/Header/AbstractAccept.php

Lines changed: 327 additions & 128 deletions
Large diffs are not rendered by default.

src/Header/Accept.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010

1111
namespace Zend\Http\Header;
12+
use Zend\Http\Header\Accept\FieldValuePart;
13+
1214

1315
/**
1416
* Accept Header
@@ -49,9 +51,9 @@ public function toString()
4951
* @param int $level
5052
* @return Accept
5153
*/
52-
public function addMediaType($type, $priority = 1, $level = null)
54+
public function addMediaType($type, $priority = 1, array $params = array())
5355
{
54-
return $this->addType($type, $priority, $level);
56+
return $this->addType($type, $priority, $params);
5557
}
5658

5759
/**
@@ -64,4 +66,54 @@ public function hasMediaType($type)
6466
{
6567
return $this->hasType($type);
6668
}
69+
70+
/**
71+
* Parse the keys contained in the header line
72+
*
73+
* @param string mediaType
74+
* @return \Zend\Http\Header\Accept\FieldValuePart\CharsetFieldValuePart
75+
* @see \Zend\Http\Header\AbstractAccept::parseFieldValuePart()
76+
*/
77+
protected function parseFieldValuePart($fieldValuePart)
78+
{
79+
$raw = $fieldValuePart;
80+
if ($pos = strpos($fieldValuePart, '/')) {
81+
$type = trim(substr($fieldValuePart, 0, $pos));
82+
} else {
83+
$type = trim(substr($fieldValuePart, 0));
84+
}
85+
86+
$params = $this->getParametersFromFieldValuePart($fieldValuePart);
87+
88+
if ($pos = strpos($fieldValuePart, ';')) {
89+
$fieldValuePart = trim(substr($fieldValuePart, 0, $pos));
90+
}
91+
92+
if ($pos = strpos($fieldValuePart, '/')) {
93+
$subtypeWhole = $format = $subtype = trim(substr($fieldValuePart, strpos($fieldValuePart, '/')+1));
94+
} else {
95+
$subtypeWhole = '';
96+
$format = '*';
97+
$subtype = '*';
98+
}
99+
100+
$pos = strpos($subtype, '+');
101+
if (false !== $pos) {
102+
$format = trim(substr($subtype, $pos+1));
103+
$subtype = trim(substr($subtype, 0, $pos));
104+
}
105+
106+
$aggregated = array(
107+
'typeString' => trim($fieldValuePart),
108+
'type' => $type,
109+
'subtype' => $subtype,
110+
'subtypeRaw' => $subtypeWhole,
111+
'format' => $format,
112+
'priority' => isset($params['q']) ? $params['q'] : 1,
113+
'params' => $params,
114+
'raw' => trim($raw)
115+
);
116+
117+
return new FieldValuePart\AcceptFieldValuePart((object) $aggregated);
118+
}
67119
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
* @package Zend_Http
9+
*/
10+
11+
namespace Zend\Http\Header\Accept\FieldValuePart;
12+
13+
/**
14+
* Field Value Part
15+
*
16+
*
17+
* @category Zend
18+
* @package Zend\Http\Header
19+
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
20+
*/
21+
abstract class AbstractFieldValuePart
22+
{
23+
24+
/**
25+
* Internal object used for value retrieval
26+
* @var object
27+
*/
28+
private $internalValues;
29+
30+
/**
31+
*
32+
* @param object $internalValues
33+
*/
34+
public function __construct($internalValues)
35+
{
36+
$this->internalValues = $internalValues;
37+
}
38+
39+
/**
40+
*
41+
* @return object
42+
*/
43+
protected function getInternalValues()
44+
{
45+
return $this->internalValues;
46+
}
47+
48+
/**
49+
* @return string $typeString
50+
*/
51+
public function getTypeString()
52+
{
53+
return $this->getInternalValues()->typeString;
54+
}
55+
56+
/**
57+
* @return float $priority
58+
*/
59+
public function getPriority()
60+
{
61+
return (float) $this->getInternalValues()->priority;
62+
}
63+
64+
/**
65+
* @return StdClass $params
66+
*/
67+
public function getParams()
68+
{
69+
return (object) $this->getInternalValues()->params;
70+
}
71+
72+
/**
73+
* @return raw $raw
74+
*/
75+
public function getRaw()
76+
{
77+
return $this->getInternalValues()->raw;
78+
}
79+
80+
/**
81+
*
82+
* @param mixed
83+
* @return mixed
84+
*/
85+
public function __get($key)
86+
{
87+
return $this->getInternalValues()->$key;
88+
}
89+
90+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
* @package Zend_Http
9+
*/
10+
11+
namespace Zend\Http\Header\Accept\FieldValuePart;
12+
13+
/**
14+
* Field Value Part
15+
*
16+
*
17+
* @category Zend
18+
* @package Zend\Http\Header
19+
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
20+
*/
21+
class AcceptFieldValuePart extends AbstractFieldValuePart
22+
{
23+
24+
/**
25+
* @return string
26+
*/
27+
public function getSubtype()
28+
{
29+
return $this->getInternalValues()->subtype;
30+
}
31+
32+
/**
33+
* @return string
34+
*/
35+
public function getSubtypeRaw()
36+
{
37+
return $this->getInternalValues()->subtypeRaw;
38+
}
39+
40+
/**
41+
* @return string
42+
*/
43+
public function getFormat()
44+
{
45+
return $this->getInternalValues()->format;
46+
}
47+
48+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
* @package Zend_Http
9+
*/
10+
11+
namespace Zend\Http\Header\Accept\FieldValuePart;
12+
13+
/**
14+
* Field Value Part
15+
*
16+
*
17+
* @category Zend
18+
* @package Zend\Http\Header
19+
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
20+
*/
21+
class CharsetFieldValuePart extends AbstractFieldValuePart
22+
{
23+
24+
/**
25+
*
26+
* @return string
27+
*/
28+
public function getCharset()
29+
{
30+
return $this->getInternalValues()->type;
31+
}
32+
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
* @package Zend_Http
9+
*/
10+
11+
namespace Zend\Http\Header\Accept\FieldValuePart;
12+
13+
/**
14+
* Field Value Part
15+
*
16+
*
17+
* @category Zend
18+
* @package Zend\Http\Header
19+
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
20+
*/
21+
class EncodingFieldValuePart extends AbstractFieldValuePart
22+
{
23+
24+
/**
25+
*
26+
* @return string
27+
*/
28+
public function getEncoding()
29+
{
30+
return $this->type;
31+
}
32+
33+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
* @package Zend_Http
9+
*/
10+
11+
namespace Zend\Http\Header\Accept\FieldValuePart;
12+
13+
/**
14+
* Field Value Part
15+
*
16+
*
17+
* @category Zend
18+
* @package Zend\Http\Header
19+
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
20+
*/
21+
class LanguageFieldValuePart extends AbstractFieldValuePart
22+
{
23+
24+
public function getLanguage()
25+
{
26+
return $this->getInternalValues()->typeString;
27+
}
28+
29+
public function getPrimaryTag()
30+
{
31+
return $this->getInternalValues()->type;
32+
}
33+
34+
public function getSubTag()
35+
{
36+
return $this->getInternalValues()->subtype;
37+
}
38+
39+
}

src/Header/AcceptCharset.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
namespace Zend\Http\Header;
12+
use Zend\Http\Header\Accept\FieldValuePart;
1213

1314
/**
1415
* Accept Charset Header
@@ -63,4 +64,18 @@ public function hasCharset($type)
6364
{
6465
return $this->hasType($type);
6566
}
67+
68+
/**
69+
* Parse the keys contained in the header line
70+
*
71+
* @param string mediaType
72+
* @return \Zend\Http\Header\Accept\FieldValuePart\CharsetFieldValuePart
73+
* @see \Zend\Http\Header\AbstractAccept::parseFieldValuePart()
74+
*/
75+
protected function parseFieldValuePart($fieldValuePart)
76+
{
77+
$internalValues = parent::parseFieldValuePart($fieldValuePart);
78+
79+
return new FieldValuePart\CharsetFieldValuePart($internalValues);
80+
}
6681
}

src/Header/AcceptEncoding.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
namespace Zend\Http\Header;
12+
use Zend\Http\Header\Accept\FieldValuePart;
1213

1314
/**
1415
* Accept Encoding Header
@@ -64,4 +65,19 @@ public function hasEncoding($type)
6465
{
6566
return $this->hasType($type);
6667
}
68+
69+
/**
70+
* Parse the keys contained in the header line
71+
*
72+
* @param string mediaType
73+
* @return \Zend\Http\Header\Accept\FieldValuePart\EncodingFieldValuePart
74+
* @see \Zend\Http\Header\AbstractAccept::parseFieldValuePart()
75+
*/
76+
protected function parseFieldValuePart($fieldValuePart)
77+
{
78+
$internalValues = parent::parseFieldValuePart($fieldValuePart);
79+
80+
return new FieldValuePart\EncodingFieldValuePart($internalValues);
81+
}
6782
}
83+

0 commit comments

Comments
 (0)