1: <?php
2: namespace Worldline\Connect\Sdk\Communication;
3:
4: /**
5: * Class ResponseHeaderBuilder
6: *
7: * @package Worldline\Connect\Sdk\Communication
8: */
9: class ResponseHeaderBuilder
10: {
11: /**
12: * @var string
13: */
14: private string $headerString = '';
15:
16: /**
17: * @var array|null
18: */
19: private ?array $headers = null;
20:
21: /**
22: * @var string|null
23: */
24: private ?string $contentType = null;
25:
26: /**
27: * @param string $data
28: *
29: * @return void
30: */
31: public function append(string $data): void
32: {
33: $this->headerString .= $data;
34: $this->headers = null;
35: }
36:
37: /**
38: * @return array
39: */
40: public function getHeaders(): array
41: {
42: if (is_null($this->headers)) {
43: $this->headers = HttpHeaderHelper::parseRawHeaders(explode("\r\n", $this->headerString));
44: }
45: return $this->headers;
46: }
47:
48: /**
49: * @return string|null
50: */
51: public function getContentType(): ?string
52: {
53: if (is_null($this->contentType)) {
54: $headers = $this->getHeaders();
55: foreach ($headers as $headerKey => $headerValue) {
56: if (strtolower($headerKey) === 'content-type') {
57: $this->contentType = $headerValue;
58: break;
59: }
60: }
61: }
62: return $this->contentType;
63: }
64: }
65: