Open Journal Systems  3.3.0
lib/vendor/symfony/http-foundation/HeaderBag.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
19 class HeaderBag implements \IteratorAggregate, \Countable
20 {
21  protected $headers = array();
22  protected $cacheControl = array();
23 
29  public function __construct(array $headers = array())
30  {
31  foreach ($headers as $key => $values) {
32  $this->set($key, $values);
33  }
34  }
35 
41  public function __toString()
42  {
43  if (!$headers = $this->all()) {
44  return '';
45  }
46 
47  ksort($headers);
48  $max = max(array_map('strlen', array_keys($headers))) + 1;
49  $content = '';
50  foreach ($headers as $name => $values) {
51  $name = implode('-', array_map('ucfirst', explode('-', $name)));
52  foreach ($values as $value) {
53  $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value);
54  }
55  }
56 
57  return $content;
58  }
59 
65  public function all()
66  {
67  return $this->headers;
68  }
69 
75  public function keys()
76  {
77  return array_keys($this->all());
78  }
79 
85  public function replace(array $headers = array())
86  {
87  $this->headers = array();
88  $this->add($headers);
89  }
90 
96  public function add(array $headers)
97  {
98  foreach ($headers as $key => $values) {
99  $this->set($key, $values);
100  }
101  }
102 
112  public function get($key, $default = null, $first = true)
113  {
114  $key = str_replace('_', '-', strtolower($key));
115  $headers = $this->all();
116 
117  if (!array_key_exists($key, $headers)) {
118  if (null === $default) {
119  return $first ? null : array();
120  }
121 
122  return $first ? $default : array($default);
123  }
124 
125  if ($first) {
126  return count($headers[$key]) ? $headers[$key][0] : $default;
127  }
128 
129  return $headers[$key];
130  }
131 
139  public function set($key, $values, $replace = true)
140  {
141  $key = str_replace('_', '-', strtolower($key));
142 
143  $values = array_values((array) $values);
144 
145  if (true === $replace || !isset($this->headers[$key])) {
146  $this->headers[$key] = $values;
147  } else {
148  $this->headers[$key] = array_merge($this->headers[$key], $values);
149  }
150 
151  if ('cache-control' === $key) {
152  $this->cacheControl = $this->parseCacheControl($values[0]);
153  }
154  }
155 
163  public function has($key)
164  {
165  return array_key_exists(str_replace('_', '-', strtolower($key)), $this->all());
166  }
167 
176  public function contains($key, $value)
177  {
178  return in_array($value, $this->get($key, null, false));
179  }
180 
186  public function remove($key)
187  {
188  $key = str_replace('_', '-', strtolower($key));
189 
190  unset($this->headers[$key]);
191 
192  if ('cache-control' === $key) {
193  $this->cacheControl = array();
194  }
195  }
196 
207  public function getDate($key, \DateTime $default = null)
208  {
209  if (null === $value = $this->get($key)) {
210  return $default;
211  }
212 
213  if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) {
214  throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value));
215  }
216 
217  return $date;
218  }
219 
226  public function addCacheControlDirective($key, $value = true)
227  {
228  $this->cacheControl[$key] = $value;
229 
230  $this->set('Cache-Control', $this->getCacheControlHeader());
231  }
232 
240  public function hasCacheControlDirective($key)
241  {
242  return array_key_exists($key, $this->cacheControl);
243  }
244 
252  public function getCacheControlDirective($key)
253  {
254  return array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
255  }
256 
262  public function removeCacheControlDirective($key)
263  {
264  unset($this->cacheControl[$key]);
265 
266  $this->set('Cache-Control', $this->getCacheControlHeader());
267  }
268 
274  public function getIterator()
275  {
276  return new \ArrayIterator($this->headers);
277  }
278 
284  public function count()
285  {
286  return count($this->headers);
287  }
288 
289  protected function getCacheControlHeader()
290  {
291  $parts = array();
292  ksort($this->cacheControl);
293  foreach ($this->cacheControl as $key => $value) {
294  if (true === $value) {
295  $parts[] = $key;
296  } else {
297  if (preg_match('#[^a-zA-Z0-9._-]#', $value)) {
298  $value = '"'.$value.'"';
299  }
300 
301  $parts[] = "$key=$value";
302  }
303  }
304 
305  return implode(', ', $parts);
306  }
307 
315  protected function parseCacheControl($header)
316  {
317  $cacheControl = array();
318  preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER);
319  foreach ($matches as $match) {
320  $cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true);
321  }
322 
323  return $cacheControl;
324  }
325 }
Symfony\Component\HttpFoundation\HeaderBag\getIterator
getIterator()
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:274
Symfony\Component\HttpFoundation\HeaderBag\getCacheControlHeader
getCacheControlHeader()
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:289
Symfony\Component\HttpFoundation\HeaderBag\addCacheControlDirective
addCacheControlDirective($key, $value=true)
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:226
Symfony\Component\HttpFoundation\HeaderBag\removeCacheControlDirective
removeCacheControlDirective($key)
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:262
Symfony\Component\HttpFoundation\HeaderBag\getDate
getDate($key, \DateTime $default=null)
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:207
Symfony\Component\HttpFoundation\HeaderBag\add
add(array $headers)
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:96
Symfony\Component\HttpFoundation\HeaderBag\__toString
__toString()
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:41
Symfony\Component\HttpFoundation\HeaderBag\contains
contains($key, $value)
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:176
Symfony\Component\HttpFoundation\HeaderBag\$headers
$headers
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:21
Symfony\Component\HttpFoundation\HeaderBag\all
all()
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:65
Symfony\Component\HttpFoundation\HeaderBag\replace
replace(array $headers=array())
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:85
Symfony\Component\HttpFoundation\HeaderBag\$cacheControl
$cacheControl
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:22
Symfony\Component\HttpFoundation\HeaderBag\parseCacheControl
parseCacheControl($header)
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:315
Symfony\Component\HttpFoundation
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:12
Symfony\Component\HttpFoundation\HeaderBag
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:19
Symfony\Component\HttpFoundation\HeaderBag\getCacheControlDirective
getCacheControlDirective($key)
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:252
Symfony\Component\HttpFoundation\HeaderBag\__construct
__construct(array $headers=array())
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:29
Symfony\Component\HttpFoundation\HeaderBag\count
count()
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:284
Symfony\Component\HttpFoundation\HeaderBag\has
has($key)
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:163
Symfony\Component\HttpFoundation\HeaderBag\hasCacheControlDirective
hasCacheControlDirective($key)
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:240
Symfony\Component\HttpFoundation\HeaderBag\keys
keys()
Definition: lib/vendor/symfony/http-foundation/HeaderBag.php:75