/vendor/yiisoft/yii2-httpclient/Message.php

https://gitlab.com/itlboy/yii2-starter-installed · PHP · 338 lines · 182 code · 26 blank · 130 comment · 18 complexity · 1adbc6e2fca172c13336a7700d87987c MD5 · raw file

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\httpclient;
  8. use yii\base\Component;
  9. use yii\base\ErrorHandler;
  10. use yii\web\Cookie;
  11. use yii\web\CookieCollection;
  12. use yii\web\HeaderCollection;
  13. use Yii;
  14. /**
  15. * Message represents a base HTTP message.
  16. *
  17. * @property string $content Raw body.
  18. * @property CookieCollection|Cookie[] $cookies The cookie collection. Note that the type of this property
  19. * differs in getter and setter. See [[getCookies()]] and [[setCookies()]] for details.
  20. * @property mixed $data Content data fields.
  21. * @property string $format Body format name.
  22. * @property HeaderCollection $headers The header collection. Note that the type of this property differs in
  23. * getter and setter. See [[getHeaders()]] and [[setHeaders()]] for details.
  24. *
  25. * @author Paul Klimov <klimov.paul@gmail.com>
  26. * @since 2.0
  27. */
  28. class Message extends Component
  29. {
  30. /**
  31. * @var Client owner client instance.
  32. */
  33. public $client;
  34. /**
  35. * @var HeaderCollection headers.
  36. */
  37. private $_headers;
  38. /**
  39. * @var CookieCollection cookies.
  40. */
  41. private $_cookies;
  42. /**
  43. * @var string|null raw content
  44. */
  45. private $_content;
  46. /**
  47. * @var mixed content data
  48. */
  49. private $_data;
  50. /**
  51. * @var string content format name
  52. */
  53. private $_format;
  54. /**
  55. * Sets the HTTP headers associated with HTTP message.
  56. * @param array|HeaderCollection $headers headers collection or headers list in format: [headerName => headerValue]
  57. * @return $this self reference.
  58. */
  59. public function setHeaders($headers)
  60. {
  61. $this->_headers = $headers;
  62. return $this;
  63. }
  64. /**
  65. * Returns the header collection.
  66. * The header collection contains the HTTP headers associated with HTTP message.
  67. * @return HeaderCollection the header collection
  68. */
  69. public function getHeaders()
  70. {
  71. if (!is_object($this->_headers)) {
  72. $headerCollection = new HeaderCollection();
  73. if (is_array($this->_headers)) {
  74. foreach ($this->_headers as $name => $value) {
  75. if (is_int($name)) {
  76. // parse raw header :
  77. $rawHeader = $value;
  78. if (($separatorPos = strpos($rawHeader, ':')) !== false) {
  79. $name = strtolower(trim(substr($rawHeader, 0, $separatorPos)));
  80. $value = trim(substr($rawHeader, $separatorPos + 1));
  81. $headerCollection->add($name, $value);
  82. } elseif (strpos($rawHeader, 'HTTP/') === 0) {
  83. $parts = explode(' ', $rawHeader, 3);
  84. $headerCollection->add('http-code', $parts[1]);
  85. } else {
  86. $headerCollection->add('raw', $rawHeader);
  87. }
  88. } else {
  89. $headerCollection->set($name, $value);
  90. }
  91. }
  92. }
  93. $this->_headers = $headerCollection;
  94. }
  95. return $this->_headers;
  96. }
  97. /**
  98. * Adds more headers to the already defined ones.
  99. * @param array $headers additional headers in format: [headerName => headerValue]
  100. * @return $this self reference.
  101. */
  102. public function addHeaders(array $headers)
  103. {
  104. $headerCollection = $this->getHeaders();
  105. foreach ($headers as $name => $value) {
  106. $headerCollection->add($name, $value);
  107. }
  108. return $this;
  109. }
  110. /**
  111. * Checks of HTTP message contains any header.
  112. * Using this method you are able to check cookie presence without instantiating [[HeaderCollection]].
  113. * @return bool whether message contains any header.
  114. */
  115. public function hasHeaders()
  116. {
  117. if (is_object($this->_headers)) {
  118. return $this->_headers->getCount() > 0;
  119. }
  120. return !empty($this->_headers);
  121. }
  122. /**
  123. * Sets the cookies associated with HTTP message.
  124. * @param CookieCollection|Cookie[]|array $cookies cookie collection or cookies list.
  125. * @return $this self reference.
  126. */
  127. public function setCookies($cookies)
  128. {
  129. $this->_cookies = $cookies;
  130. return $this;
  131. }
  132. /**
  133. * Returns the cookie collection.
  134. * The cookie collection contains the cookies associated with HTTP message.
  135. * @return CookieCollection|Cookie[] the cookie collection.
  136. */
  137. public function getCookies()
  138. {
  139. if (!is_object($this->_cookies)) {
  140. $cookieCollection = new CookieCollection();
  141. if (is_array($this->_cookies)) {
  142. foreach ($this->_cookies as $cookie) {
  143. if (!is_object($cookie)) {
  144. $cookie = new Cookie($cookie);
  145. }
  146. $cookieCollection->add($cookie);
  147. }
  148. }
  149. $this->_cookies = $cookieCollection;
  150. }
  151. return $this->_cookies;
  152. }
  153. /**
  154. * Adds more cookies to the already defined ones.
  155. * @param Cookie[]|array $cookies additional cookies.
  156. * @return $this self reference.
  157. */
  158. public function addCookies(array $cookies)
  159. {
  160. $cookieCollection = $this->getCookies();
  161. foreach ($cookies as $cookie) {
  162. if (!is_object($cookie)) {
  163. $cookie = new Cookie($cookie);
  164. }
  165. $cookieCollection->add($cookie);
  166. }
  167. return $this;
  168. }
  169. /**
  170. * Checks of HTTP message contains any cookie.
  171. * Using this method you are able to check cookie presence without instantiating [[CookieCollection]].
  172. * @return bool whether message contains any cookie.
  173. */
  174. public function hasCookies()
  175. {
  176. if (is_object($this->_cookies)) {
  177. return $this->_cookies->getCount() > 0;
  178. }
  179. return !empty($this->_cookies);
  180. }
  181. /**
  182. * Sets the HTTP message raw content.
  183. * @param string $content raw content.
  184. * @return $this self reference.
  185. */
  186. public function setContent($content)
  187. {
  188. $this->_content = $content;
  189. return $this;
  190. }
  191. /**
  192. * Returns HTTP message raw content.
  193. * @return string raw body.
  194. */
  195. public function getContent()
  196. {
  197. return $this->_content;
  198. }
  199. /**
  200. * Sets the data fields, which composes message content.
  201. * @param mixed $data content data fields.
  202. * @return $this self reference.
  203. */
  204. public function setData($data)
  205. {
  206. $this->_data = $data;
  207. return $this;
  208. }
  209. /**
  210. * Returns the data fields, parsed from raw content.
  211. * @return mixed content data fields.
  212. */
  213. public function getData()
  214. {
  215. return $this->_data;
  216. }
  217. /**
  218. * Adds data fields to the existing ones.
  219. * @param array $data additional content data fields.
  220. * @return $this self reference.
  221. * @since 2.0.1
  222. */
  223. public function addData($data)
  224. {
  225. if (empty($this->_data)) {
  226. $this->_data = $data;
  227. } else {
  228. $this->_data = array_merge($this->_data, $data);
  229. }
  230. return $this;
  231. }
  232. /**
  233. * Sets body format.
  234. * @param string $format body format name.
  235. * @return $this self reference.
  236. */
  237. public function setFormat($format)
  238. {
  239. $this->_format = $format;
  240. return $this;
  241. }
  242. /**
  243. * Returns body format.
  244. * @return string body format name.
  245. */
  246. public function getFormat()
  247. {
  248. if ($this->_format === null) {
  249. $this->_format = $this->defaultFormat();
  250. }
  251. return $this->_format;
  252. }
  253. /**
  254. * Returns default format name.
  255. * @return string default format name.
  256. */
  257. protected function defaultFormat()
  258. {
  259. return Client::FORMAT_URLENCODED;
  260. }
  261. /**
  262. * Composes raw header lines from [[headers]].
  263. * Each line will be a string in format: 'header-name: value'.
  264. * @return array raw header lines.
  265. */
  266. public function composeHeaderLines()
  267. {
  268. if (!$this->hasHeaders()) {
  269. return [];
  270. }
  271. $headers = [];
  272. foreach ($this->getHeaders() as $name => $values) {
  273. $name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $name)));
  274. foreach ($values as $value) {
  275. $headers[] = "$name: $value";
  276. }
  277. }
  278. return $headers;
  279. }
  280. /**
  281. * Returns string representation of this HTTP message.
  282. * @return string the string representation of this HTTP message.
  283. */
  284. public function toString()
  285. {
  286. $result = '';
  287. if ($this->hasHeaders()) {
  288. $headers = $this->composeHeaderLines();
  289. $result .= implode("\n", $headers);
  290. }
  291. $content = $this->getContent();
  292. if ($content !== null) {
  293. $result .= "\n\n" . $content;
  294. }
  295. return $result;
  296. }
  297. /**
  298. * PHP magic method that returns the string representation of this object.
  299. * @return string the string representation of this object.
  300. */
  301. public function __toString()
  302. {
  303. // __toString cannot throw exception
  304. // use trigger_error to bypass this limitation
  305. try {
  306. return $this->toString();
  307. } catch (\Exception $e) {
  308. ErrorHandler::convertExceptionToError($e);
  309. return '';
  310. }
  311. }
  312. }