/system/classes/kohana/http/header/value.php

https://github.com/azhai2012/calf · PHP · 219 lines · 113 code · 23 blank · 83 comment · 11 complexity · 0dd2a01a4d003fb8f0a68943fc5e8b85 MD5 · raw file

  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Kohana_Http_Header_Value represents a value assigned to an HTTP header, i.e.
  4. *
  5. * Accept: [key=]value[; property[=property_value][; ...]]
  6. *
  7. * Values are either single values,
  8. *
  9. * @package Kohana
  10. * @category Http
  11. * @author Kohana Team
  12. * @since 3.1.0
  13. * @copyright (c) 2008-2011 Kohana Team
  14. * @license http://kohanaphp.com/license
  15. */
  16. class Kohana_Http_Header_Value {
  17. /**
  18. * @var float The default quality header property value
  19. */
  20. public static $default_quality = 1.0;
  21. /**
  22. * Detects and returns key/value pairs
  23. *
  24. * @param string $string String to parse
  25. * @param string $separator
  26. * @return array
  27. */
  28. public static function parse_key_value($string, $separator = '=')
  29. {
  30. $parts = explode($separator, trim($string), 2);
  31. if (count($parts) == 1)
  32. {
  33. return $parts;
  34. }
  35. else
  36. {
  37. return array($parts[0] => $parts[1]);
  38. }
  39. }
  40. /**
  41. * @var array
  42. */
  43. public $properties = array();
  44. /**
  45. * @var void|string
  46. */
  47. public $key;
  48. /**
  49. * @var array
  50. */
  51. public $value = array();
  52. /**
  53. * Builds the header field
  54. *
  55. * @param mixed value configuration array passed
  56. * @param boolean no_parse skip parsing of the string (i.e. user-agent)
  57. * @throws Kohana_Http_Exception
  58. */
  59. public function __construct($value, $no_parse = FALSE)
  60. {
  61. // If no parse is set, set the value and get out of here (user-agent)
  62. if ($no_parse)
  63. {
  64. $this->key = NULL;
  65. $this->value = $value;
  66. return;
  67. }
  68. // If configuration array passed
  69. if (is_array($value))
  70. {
  71. // Parse each value
  72. foreach ($value as $k => $v)
  73. {
  74. // If the key is a property
  75. if (property_exists($this, $k))
  76. {
  77. // Map values
  78. $this->$k = $v;
  79. }
  80. }
  81. }
  82. // If value is a string
  83. elseif (is_string($value))
  84. {
  85. // Detect properties
  86. if (strpos($value, ';') !== FALSE)
  87. {
  88. // Remove properties from the string
  89. $parts = explode(';', $value);
  90. $value = array_shift($parts);
  91. // Parse the properties
  92. $properties = array();
  93. // Foreach part
  94. foreach ($parts as $part)
  95. {
  96. // Merge the parsed values
  97. $properties = array_merge(Http_Header_Value::parse_key_value($part), $properties);
  98. }
  99. // Apply the parsed values
  100. $this->properties = $properties;
  101. }
  102. // Parse the value and get key
  103. $value = Http_Header_Value::parse_key_value($value);
  104. $key = key($value);
  105. // If the key is a string
  106. if (is_string($key))
  107. {
  108. // Apply the key as a property
  109. $this->key = $key;
  110. }
  111. // Apply the value
  112. $this->value = current($value);
  113. }
  114. // Unrecognised value type
  115. else
  116. {
  117. throw new Http_Exception(__METHOD__.' unknown header value type: :type. array or string allowed.', array(':type' => gettype($value)));
  118. }
  119. }
  120. /**
  121. * Provides direct access to the key of this header value
  122. *
  123. * @param string $key Key value to set
  124. * @return mixed
  125. */
  126. public function key($key = NULL)
  127. {
  128. if ($key === NULL)
  129. {
  130. return $this->key;
  131. }
  132. else
  133. {
  134. $this->key = $key;
  135. return $this;
  136. }
  137. }
  138. /**
  139. * Provides direct access to the value of this header value
  140. *
  141. * @param string $value Value to set
  142. * @return mixed
  143. */
  144. public function value($value = NULL)
  145. {
  146. if ($value === NULL)
  147. {
  148. return $this->value;
  149. }
  150. else
  151. {
  152. $this->value = $value;
  153. return $this;
  154. }
  155. }
  156. /**
  157. * Provides direct access to the properties of this header value
  158. *
  159. * @param array $properties Properties to set to this value
  160. * @return mixed
  161. */
  162. public function properties(array $properties = array())
  163. {
  164. if ( ! $properties)
  165. {
  166. return $this->properties;
  167. }
  168. else
  169. {
  170. $this->properties = $properties;
  171. return $this;
  172. }
  173. }
  174. /**
  175. * Magic method to handle object being cast to
  176. * string. Produces the following header value syntax
  177. *
  178. * [key=]value[; property[=property_value][; ... ]]
  179. *
  180. * @return string
  181. */
  182. public function __toString()
  183. {
  184. $string = ($this->key !== NULL) ? ($this->key.'='.$this->value) : $this->value;
  185. if ($this->properties)
  186. {
  187. $props = array($string);
  188. foreach ($this->properties as $k => $v)
  189. {
  190. $props[] = is_int($k) ? $v : ($k.'='.$v);
  191. }
  192. $string = implode('; ', $props);
  193. }
  194. return $string;
  195. }
  196. } // End Http_Header_Field