GuzzleHttpPsr7MessageTrait

Trait implementing functionality common to requests and responses.

Defined (1)

The class is defined in the following location(s).

/lib/Azure/GuzzleHttp/Psr7/MessageTrait.php  
  1. trait MessageTrait 
  2. /** @var array Map of all registered headers, as original name => array of values */ 
  3. private $headers = []; 
  4.  
  5. /** @var array Map of lowercase header name => original name at registration */ 
  6. private $headerNames = []; 
  7.  
  8. /** @var string */ 
  9. private $protocol = '1.1'; 
  10.  
  11. /** @var StreamInterface */ 
  12. private $stream; 
  13.  
  14. public function getProtocolVersion() 
  15. return $this->protocol; 
  16.  
  17. public function withProtocolVersion($version) 
  18. if ($this->protocol === $version) { 
  19. return $this; 
  20.  
  21. $new = clone $this; 
  22. $new->protocol = $version; 
  23. return $new; 
  24.  
  25. public function getHeaders() 
  26. return $this->headers; 
  27.  
  28. public function hasHeader($header) 
  29. return isset($this->headerNames[strtolower($header)]); 
  30.  
  31. public function getHeader($header) 
  32. $header = strtolower($header); 
  33.  
  34. if (!isset($this->headerNames[$header])) { 
  35. return []; 
  36.  
  37. $header = $this->headerNames[$header]; 
  38.  
  39. return $this->headers[$header]; 
  40.  
  41. public function getHeaderLine($header) 
  42. return implode(', ', $this->getHeader($header)); 
  43.  
  44. public function withHeader($header, $value) 
  45. if (!is_array($value)) { 
  46. $value = [$value]; 
  47.  
  48. $value = $this->trimHeaderValues($value); 
  49. $normalized = strtolower($header); 
  50.  
  51. $new = clone $this; 
  52. if (isset($new->headerNames[$normalized])) { 
  53. unset($new->headers[$new->headerNames[$normalized]]); 
  54. $new->headerNames[$normalized] = $header; 
  55. $new->headers[$header] = $value; 
  56.  
  57. return $new; 
  58.  
  59. public function withAddedHeader($header, $value) 
  60. if (!is_array($value)) { 
  61. $value = [$value]; 
  62.  
  63. $value = $this->trimHeaderValues($value); 
  64. $normalized = strtolower($header); 
  65.  
  66. $new = clone $this; 
  67. if (isset($new->headerNames[$normalized])) { 
  68. $header = $this->headerNames[$normalized]; 
  69. $new->headers[$header] = array_merge($this->headers[$header], $value); 
  70. } else { 
  71. $new->headerNames[$normalized] = $header; 
  72. $new->headers[$header] = $value; 
  73.  
  74. return $new; 
  75.  
  76. public function withoutHeader($header) 
  77. $normalized = strtolower($header); 
  78.  
  79. if (!isset($this->headerNames[$normalized])) { 
  80. return $this; 
  81.  
  82. $header = $this->headerNames[$normalized]; 
  83.  
  84. $new = clone $this; 
  85. unset($new->headers[$header], $new->headerNames[$normalized]); 
  86.  
  87. return $new; 
  88.  
  89. public function getBody() 
  90. if (!$this->stream) { 
  91. $this->stream = stream_for(''); 
  92.  
  93. return $this->stream; 
  94.  
  95. public function withBody(StreamInterface $body) 
  96. if ($body === $this->stream) { 
  97. return $this; 
  98.  
  99. $new = clone $this; 
  100. $new->stream = $body; 
  101. return $new; 
  102.  
  103. private function setHeaders(array $headers) 
  104. $this->headerNames = $this->headers = []; 
  105. foreach ($headers as $header => $value) { 
  106. if (!is_array($value)) { 
  107. $value = [$value]; 
  108.  
  109. $value = $this->trimHeaderValues($value); 
  110. $normalized = strtolower($header); 
  111. if (isset($this->headerNames[$normalized])) { 
  112. $header = $this->headerNames[$normalized]; 
  113. $this->headers[$header] = array_merge($this->headers[$header], $value); 
  114. } else { 
  115. $this->headerNames[$normalized] = $header; 
  116. $this->headers[$header] = $value; 
  117.  
  118. /** 
  119. * Trims whitespace from the header values. 
  120. * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field. 
  121. * header-field = field-name ":" OWS field-value OWS 
  122. * OWS = *( SP / HTAB ) 
  123. * @param string[] $values Header values 
  124. * @return string[] Trimmed header values 
  125. * @see https://tools.ietf.org/html/rfc7230#section-3.2.4 
  126. */ 
  127. private function trimHeaderValues(array $values) 
  128. return array_map(function ($value) { 
  129. return trim($value, " \t"); 
  130. }, $values);