PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Header.php

https://github.com/lslucas/105fm
PHP | 178 lines | 115 code | 30 blank | 33 comment | 4 complexity | 0ce77dc630d1ec45fb73aea238d578ca MD5 | raw file
  1. <?php
  2. namespace Guzzle\Http\Message;
  3. use Guzzle\Common\Version;
  4. use Guzzle\Http\Message\Header\HeaderInterface;
  5. /**
  6. * Represents a header and all of the values stored by that header
  7. */
  8. class Header implements HeaderInterface
  9. {
  10. protected $values = array();
  11. protected $header;
  12. protected $glue;
  13. /**
  14. * @param string $header Name of the header
  15. * @param array|string $values Values of the header as an array or a scalar
  16. * @param string $glue Glue used to combine multiple values into a string
  17. */
  18. public function __construct($header, $values = array(), $glue = ',')
  19. {
  20. $this->header = trim($header);
  21. $this->glue = $glue;
  22. foreach ((array) $values as $value) {
  23. foreach ((array) $value as $v) {
  24. $this->values[] = $v;
  25. }
  26. }
  27. }
  28. public function __toString()
  29. {
  30. return implode($this->glue . ' ', $this->toArray());
  31. }
  32. public function add($value)
  33. {
  34. $this->values[] = $value;
  35. return $this;
  36. }
  37. public function getName()
  38. {
  39. return $this->header;
  40. }
  41. public function setName($name)
  42. {
  43. $this->header = $name;
  44. return $this;
  45. }
  46. public function setGlue($glue)
  47. {
  48. $this->glue = $glue;
  49. return $this;
  50. }
  51. public function getGlue()
  52. {
  53. return $this->glue;
  54. }
  55. /**
  56. * Normalize the header to be a single header with an array of values.
  57. *
  58. * If any values of the header contains the glue string value (e.g. ","), then the value will be exploded into
  59. * multiple entries in the header.
  60. *
  61. * @return self
  62. */
  63. public function normalize()
  64. {
  65. $values = $this->toArray();
  66. for ($i = 0, $total = count($values); $i < $total; $i++) {
  67. if (strpos($values[$i], $this->glue) !== false) {
  68. // Explode on glue when the glue is not inside of a comma
  69. foreach (preg_split('/' . preg_quote($this->glue) . '(?=([^"]*"[^"]*")*[^"]*$)/', $values[$i]) as $v) {
  70. $values[] = trim($v);
  71. }
  72. unset($values[$i]);
  73. }
  74. }
  75. $this->values = array_values($values);
  76. return $this;
  77. }
  78. public function hasValue($searchValue)
  79. {
  80. return in_array($searchValue, $this->toArray());
  81. }
  82. public function removeValue($searchValue)
  83. {
  84. $this->values = array_values(array_filter($this->values, function ($value) use ($searchValue) {
  85. return $value != $searchValue;
  86. }));
  87. return $this;
  88. }
  89. public function toArray()
  90. {
  91. return $this->values;
  92. }
  93. public function count()
  94. {
  95. return count($this->toArray());
  96. }
  97. public function getIterator()
  98. {
  99. return new \ArrayIterator($this->toArray());
  100. }
  101. public function parseParams()
  102. {
  103. $params = $matches = array();
  104. $callback = array($this, 'trimHeader');
  105. // Normalize the header into a single array and iterate over all values
  106. foreach ($this->normalize()->toArray() as $val) {
  107. $part = array();
  108. foreach (preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) as $kvp) {
  109. preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches);
  110. $pieces = array_map($callback, $matches[0]);
  111. $part[$pieces[0]] = isset($pieces[1]) ? $pieces[1] : '';
  112. }
  113. $params[] = $part;
  114. }
  115. return $params;
  116. }
  117. /**
  118. * @deprecated
  119. * @codeCoverageIgnore
  120. */
  121. public function hasExactHeader($header)
  122. {
  123. Version::warn(__METHOD__ . ' is deprecated');
  124. return $this->header == $header;
  125. }
  126. /**
  127. * @deprecated
  128. * @codeCoverageIgnore
  129. */
  130. public function raw()
  131. {
  132. Version::warn(__METHOD__ . ' is deprecated. Use toArray()');
  133. return $this->toArray();
  134. }
  135. /**
  136. * Trim a header by removing excess spaces and wrapping quotes
  137. *
  138. * @param $str
  139. *
  140. * @return string
  141. */
  142. protected function trimHeader($str)
  143. {
  144. static $trimmed = "\"' \n\t";
  145. return trim($str, $trimmed);
  146. }
  147. }