balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

Headers.php (2135B)


      1 <?php
      2 /**
      3  * Case-insensitive dictionary, suitable for HTTP headers
      4  *
      5  * @package Requests
      6  */
      7 
      8 /**
      9  * Case-insensitive dictionary, suitable for HTTP headers
     10  *
     11  * @package Requests
     12  */
     13 class Requests_Response_Headers extends Requests_Utility_CaseInsensitiveDictionary {
     14 	/**
     15 	 * Get the given header
     16 	 *
     17 	 * Unlike {@see self::getValues()}, this returns a string. If there are
     18 	 * multiple values, it concatenates them with a comma as per RFC2616.
     19 	 *
     20 	 * Avoid using this where commas may be used unquoted in values, such as
     21 	 * Set-Cookie headers.
     22 	 *
     23 	 * @param string $key
     24 	 * @return string|null Header value
     25 	 */
     26 	public function offsetGet($key) {
     27 		$key = strtolower($key);
     28 		if (!isset($this->data[$key])) {
     29 			return null;
     30 		}
     31 
     32 		return $this->flatten($this->data[$key]);
     33 	}
     34 
     35 	/**
     36 	 * Set the given item
     37 	 *
     38 	 * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
     39 	 *
     40 	 * @param string $key Item name
     41 	 * @param string $value Item value
     42 	 */
     43 	public function offsetSet($key, $value) {
     44 		if ($key === null) {
     45 			throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
     46 		}
     47 
     48 		$key = strtolower($key);
     49 
     50 		if (!isset($this->data[$key])) {
     51 			$this->data[$key] = array();
     52 		}
     53 
     54 		$this->data[$key][] = $value;
     55 	}
     56 
     57 	/**
     58 	 * Get all values for a given header
     59 	 *
     60 	 * @param string $key
     61 	 * @return array|null Header values
     62 	 */
     63 	public function getValues($key) {
     64 		$key = strtolower($key);
     65 		if (!isset($this->data[$key])) {
     66 			return null;
     67 		}
     68 
     69 		return $this->data[$key];
     70 	}
     71 
     72 	/**
     73 	 * Flattens a value into a string
     74 	 *
     75 	 * Converts an array into a string by imploding values with a comma, as per
     76 	 * RFC2616's rules for folding headers.
     77 	 *
     78 	 * @param string|array $value Value to flatten
     79 	 * @return string Flattened value
     80 	 */
     81 	public function flatten($value) {
     82 		if (is_array($value)) {
     83 			$value = implode(',', $value);
     84 		}
     85 
     86 		return $value;
     87 	}
     88 
     89 	/**
     90 	 * Get an iterator for the data
     91 	 *
     92 	 * Converts the internal
     93 	 * @return ArrayIterator
     94 	 */
     95 	public function getIterator() {
     96 		return new Requests_Utility_FilteredIterator($this->data, array($this, 'flatten'));
     97 	}
     98 }