Overview

Namespaces

  • Composer
    • Autoload
  • Guzzle
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
        • Header
      • QueryAggregator
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Mock
    • Stream
  • Mockery
    • Adapter
      • Phpunit
    • CountValidator
    • Exception
    • Generator
      • StringManipulation
        • Pass
    • Loader
    • Matcher
  • None
  • Omnipay
    • Common
      • Exception
      • Message
    • Dummy
      • Message
    • Fatzebra
      • Message
  • PHP
  • Symfony
    • Component
      • EventDispatcher
        • Debug
        • DependencyInjection
        • Tests
          • Debug
          • DependencyInjection
      • HttpFoundation
        • File
          • Exception
          • MimeType
        • Session
          • Attribute
          • Flash
          • Storage
            • Handler
            • Proxy
        • Tests
          • File
            • MimeType
          • Session
            • Attribute
            • Flash
            • Storage
              • Handler
              • Proxy
      • Yaml
        • Exception
        • Tests

Classes

  • Symfony\Component\Yaml\Tests\A
  • Symfony\Component\Yaml\Tests\B
  • Symfony\Component\Yaml\Tests\DumperTest
  • Symfony\Component\Yaml\Tests\InlineTest
  • Symfony\Component\Yaml\Tests\ParseExceptionTest
  • Symfony\Component\Yaml\Tests\ParserTest
  • Symfony\Component\Yaml\Tests\YamlTest
  • Overview
  • Namespace
  • Function
  • Tree
  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: 
 12: namespace Symfony\Component\HttpFoundation;
 13: 
 14: /**
 15:  * HeaderBag is a container for HTTP headers.
 16:  *
 17:  * @author Fabien Potencier <fabien@symfony.com>
 18:  *
 19:  * @api
 20:  */
 21: class HeaderBag implements \IteratorAggregate, \Countable
 22: {
 23:     protected $headers = array();
 24:     protected $cacheControl = array();
 25: 
 26:     /**
 27:      * Constructor.
 28:      *
 29:      * @param array $headers An array of HTTP headers
 30:      *
 31:      * @api
 32:      */
 33:     public function __construct(array $headers = array())
 34:     {
 35:         foreach ($headers as $key => $values) {
 36:             $this->set($key, $values);
 37:         }
 38:     }
 39: 
 40:     /**
 41:      * Returns the headers as a string.
 42:      *
 43:      * @return string The headers
 44:      */
 45:     public function __toString()
 46:     {
 47:         if (!$this->headers) {
 48:             return '';
 49:         }
 50: 
 51:         $max = max(array_map('strlen', array_keys($this->headers))) + 1;
 52:         $content = '';
 53:         ksort($this->headers);
 54:         foreach ($this->headers as $name => $values) {
 55:             $name = implode('-', array_map('ucfirst', explode('-', $name)));
 56:             foreach ($values as $value) {
 57:                 $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value);
 58:             }
 59:         }
 60: 
 61:         return $content;
 62:     }
 63: 
 64:     /**
 65:      * Returns the headers.
 66:      *
 67:      * @return array An array of headers
 68:      *
 69:      * @api
 70:      */
 71:     public function all()
 72:     {
 73:         return $this->headers;
 74:     }
 75: 
 76:     /**
 77:      * Returns the parameter keys.
 78:      *
 79:      * @return array An array of parameter keys
 80:      *
 81:      * @api
 82:      */
 83:     public function keys()
 84:     {
 85:         return array_keys($this->headers);
 86:     }
 87: 
 88:     /**
 89:      * Replaces the current HTTP headers by a new set.
 90:      *
 91:      * @param array $headers An array of HTTP headers
 92:      *
 93:      * @api
 94:      */
 95:     public function replace(array $headers = array())
 96:     {
 97:         $this->headers = array();
 98:         $this->add($headers);
 99:     }
100: 
101:     /**
102:      * Adds new headers the current HTTP headers set.
103:      *
104:      * @param array $headers An array of HTTP headers
105:      *
106:      * @api
107:      */
108:     public function add(array $headers)
109:     {
110:         foreach ($headers as $key => $values) {
111:             $this->set($key, $values);
112:         }
113:     }
114: 
115:     /**
116:      * Returns a header value by name.
117:      *
118:      * @param string $key     The header name
119:      * @param mixed  $default The default value
120:      * @param bool   $first   Whether to return the first value or all header values
121:      *
122:      * @return string|array The first header value if $first is true, an array of values otherwise
123:      *
124:      * @api
125:      */
126:     public function get($key, $default = null, $first = true)
127:     {
128:         $key = strtr(strtolower($key), '_', '-');
129: 
130:         if (!array_key_exists($key, $this->headers)) {
131:             if (null === $default) {
132:                 return $first ? null : array();
133:             }
134: 
135:             return $first ? $default : array($default);
136:         }
137: 
138:         if ($first) {
139:             return count($this->headers[$key]) ? $this->headers[$key][0] : $default;
140:         }
141: 
142:         return $this->headers[$key];
143:     }
144: 
145:     /**
146:      * Sets a header by name.
147:      *
148:      * @param string       $key     The key
149:      * @param string|array $values  The value or an array of values
150:      * @param bool         $replace Whether to replace the actual value or not (true by default)
151:      *
152:      * @api
153:      */
154:     public function set($key, $values, $replace = true)
155:     {
156:         $key = strtr(strtolower($key), '_', '-');
157: 
158:         $values = array_values((array) $values);
159: 
160:         if (true === $replace || !isset($this->headers[$key])) {
161:             $this->headers[$key] = $values;
162:         } else {
163:             $this->headers[$key] = array_merge($this->headers[$key], $values);
164:         }
165: 
166:         if ('cache-control' === $key) {
167:             $this->cacheControl = $this->parseCacheControl($values[0]);
168:         }
169:     }
170: 
171:     /**
172:      * Returns true if the HTTP header is defined.
173:      *
174:      * @param string $key The HTTP header
175:      *
176:      * @return bool true if the parameter exists, false otherwise
177:      *
178:      * @api
179:      */
180:     public function has($key)
181:     {
182:         return array_key_exists(strtr(strtolower($key), '_', '-'), $this->headers);
183:     }
184: 
185:     /**
186:      * Returns true if the given HTTP header contains the given value.
187:      *
188:      * @param string $key   The HTTP header name
189:      * @param string $value The HTTP value
190:      *
191:      * @return bool true if the value is contained in the header, false otherwise
192:      *
193:      * @api
194:      */
195:     public function contains($key, $value)
196:     {
197:         return in_array($value, $this->get($key, null, false));
198:     }
199: 
200:     /**
201:      * Removes a header.
202:      *
203:      * @param string $key The HTTP header name
204:      *
205:      * @api
206:      */
207:     public function remove($key)
208:     {
209:         $key = strtr(strtolower($key), '_', '-');
210: 
211:         unset($this->headers[$key]);
212: 
213:         if ('cache-control' === $key) {
214:             $this->cacheControl = array();
215:         }
216:     }
217: 
218:     /**
219:      * Returns the HTTP header value converted to a date.
220:      *
221:      * @param string    $key     The parameter key
222:      * @param \DateTime $default The default value
223:      *
224:      * @return null|\DateTime The parsed DateTime or the default value if the header does not exist
225:      *
226:      * @throws \RuntimeException When the HTTP header is not parseable
227:      *
228:      * @api
229:      */
230:     public function getDate($key, \DateTime $default = null)
231:     {
232:         if (null === $value = $this->get($key)) {
233:             return $default;
234:         }
235: 
236:         if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) {
237:             throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value));
238:         }
239: 
240:         return $date;
241:     }
242: 
243:     /**
244:      * Adds a custom Cache-Control directive.
245:      *
246:      * @param string $key   The Cache-Control directive name
247:      * @param mixed  $value The Cache-Control directive value
248:      */
249:     public function addCacheControlDirective($key, $value = true)
250:     {
251:         $this->cacheControl[$key] = $value;
252: 
253:         $this->set('Cache-Control', $this->getCacheControlHeader());
254:     }
255: 
256:     /**
257:      * Returns true if the Cache-Control directive is defined.
258:      *
259:      * @param string $key The Cache-Control directive
260:      *
261:      * @return bool true if the directive exists, false otherwise
262:      */
263:     public function hasCacheControlDirective($key)
264:     {
265:         return array_key_exists($key, $this->cacheControl);
266:     }
267: 
268:     /**
269:      * Returns a Cache-Control directive value by name.
270:      *
271:      * @param string $key The directive name
272:      *
273:      * @return mixed|null The directive value if defined, null otherwise
274:      */
275:     public function getCacheControlDirective($key)
276:     {
277:         return array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
278:     }
279: 
280:     /**
281:      * Removes a Cache-Control directive.
282:      *
283:      * @param string $key The Cache-Control directive
284:      */
285:     public function removeCacheControlDirective($key)
286:     {
287:         unset($this->cacheControl[$key]);
288: 
289:         $this->set('Cache-Control', $this->getCacheControlHeader());
290:     }
291: 
292:     /**
293:      * Returns an iterator for headers.
294:      *
295:      * @return \ArrayIterator An \ArrayIterator instance
296:      */
297:     public function getIterator()
298:     {
299:         return new \ArrayIterator($this->headers);
300:     }
301: 
302:     /**
303:      * Returns the number of headers.
304:      *
305:      * @return int The number of headers
306:      */
307:     public function count()
308:     {
309:         return count($this->headers);
310:     }
311: 
312:     protected function getCacheControlHeader()
313:     {
314:         $parts = array();
315:         ksort($this->cacheControl);
316:         foreach ($this->cacheControl as $key => $value) {
317:             if (true === $value) {
318:                 $parts[] = $key;
319:             } else {
320:                 if (preg_match('#[^a-zA-Z0-9._-]#', $value)) {
321:                     $value = '"'.$value.'"';
322:                 }
323: 
324:                 $parts[] = "$key=$value";
325:             }
326:         }
327: 
328:         return implode(', ', $parts);
329:     }
330: 
331:     /**
332:      * Parses a Cache-Control HTTP header.
333:      *
334:      * @param string $header The value of the Cache-Control HTTP header
335:      *
336:      * @return array An array representing the attribute values
337:      */
338:     protected function parseCacheControl($header)
339:     {
340:         $cacheControl = array();
341:         preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER);
342:         foreach ($matches as $match) {
343:             $cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true);
344:         }
345: 
346:         return $cacheControl;
347:     }
348: }
349: 
Omnipay Fat Zebra / Paystream Gateway Module API Documentation API documentation generated by ApiGen