• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sirn-se / websocket-php / 4771910143

pending completion
4771910143

push

github

Sören Jensen
Connect, handshake, HTTP, etc

212 of 212 new or added lines in 5 files covered. (100.0%)

591 of 670 relevant lines covered (88.21%)

14.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.57
/lib/Http/Message.php
1
<?php
2

3
/**
4
 * File for Phrity\WebSocket\Http\Message class
5
 * @package Phrity > WebSocket > Http
6
 */
7

8
namespace WebSocket\Http;
9

10
use BadMethodCallException;
11
use InvalidArgumentException;
12
use Psr\Http\Message\MessageInterface;
13
use Psr\Http\Message\StreamInterface;
14

15
/**
16
 * Phrity\WebSocket\Http\Message class.
17
 */
18
class Message implements MessageInterface
19
{
20
    protected $version = '1.1';
21
    protected $headers = [];
22

23
    /**
24
     * Retrieves the HTTP protocol version as a string.
25
     * @return string HTTP protocol version.
26
     */
27
    public function getProtocolVersion(): string
28
    {
29
        return $this->version;
31✔
30
    }
31

32
    /**
33
     * Return an instance with the specified HTTP protocol version.
34
     * @param string $version HTTP protocol version
35
     * @return static
36
     */
37
    public function withProtocolVersion($version): self
38
    {
39
        $new = clone $this;
29✔
40
        $new->version = $version;
29✔
41
        return $new;
29✔
42
    }
43

44
    /**
45
     * Retrieves all message header values.
46
     * @return string[][] Returns an associative array of the message's headers.
47
     */
48
    public function getHeaders(): array
49
    {
50
        return array_merge(...array_values($this->headers));
31✔
51
    }
52

53
    /**
54
     * Checks if a header exists by the given case-insensitive name.
55
     * @param string $name Case-insensitive header field name.
56
     * @return bool Returns true if any header names match the given header.
57
     */
58
    public function hasHeader($name): bool
59
    {
60
        return array_key_exists(strtolower($name), $this->headers);
31✔
61
    }
62

63
    /**
64
     * Retrieves a message header value by the given case-insensitive name.
65
     * @param string $name Case-insensitive header field name.
66
     * @return string[] An array of string values as provided for the given header.
67
     */
68
    public function getHeader($name): array
69
    {
70
        return $this->hasHeader($name)
29✔
71
            ? array_merge(...array_values($this->headers[strtolower($name)] ?: []))
28✔
72
            : [];
29✔
73
    }
74

75
    /**
76
     * Retrieves a comma-separated string of the values for a single header.
77
     * @param string $name Case-insensitive header field name.
78
     * @return string A string of values as provided for the given header.
79
     */
80
    public function getHeaderLine($name): string
81
    {
82
        return implode(',', $this->getHeader($name));
29✔
83
    }
84

85
    /**
86
     * Return an instance with the provided value replacing the specified header.
87
     * @param string $name Case-insensitive header field name.
88
     * @param string|string[] $value Header value(s).
89
     * @return static
90
     * @throws \InvalidArgumentException for invalid header names or values.
91
     */
92
    public function withHeader($name, $value): self
93
    {
94
        $new = clone $this;
31✔
95
        if ($this->hasHeader($name)) {
31✔
96
            unset($new->headers[strtolower($name)]);
×
97
        }
98
        $new->handleHeader($name, $value);
31✔
99
        return $new;
31✔
100
    }
101

102

103
    /**
104
     * Return an instance with the specified header appended with the given value.
105
     * @param string $name Case-insensitive header field name to add.
106
     * @param string|string[] $value Header value(s).
107
     * @return static
108
     * @throws \InvalidArgumentException for invalid header names.
109
     * @throws \InvalidArgumentException for invalid header values.
110
     */
111
    public function withAddedHeader($name, $value): self
112
    {
113
        $new = clone $this;
×
114
        $new->handleHeader($name, $value);
×
115
        return $new;
×
116
    }
117

118
    /**
119
     * Return an instance without the specified header.
120
     * @param string $name Case-insensitive header field name to remove.
121
     * @return static
122
     */
123
    public function withoutHeader($name): self
124
    {
125
        $new = clone $this;
×
126
        if ($this->hasHeader($name)) {
×
127
            unset($new->headers[strtolower($name)]);
×
128
        }
129
        return $new;
×
130
    }
131

132
    /**
133
     * Not implemented, WebSocket only use headers.
134
     */
135
    public function getBody(): StreamInterface
136
    {
137
        throw new BadMethodCallException("Not implemented.");
×
138
    }
139

140
    /**
141
     * Not implemented, WebSocket only use headers.
142
     */
143
    public function withBody(StreamInterface $body): self
144
    {
145
        throw new BadMethodCallException("Not implemented.");
×
146
    }
147

148
    public function render(): string
149
    {
150
        $data = '';
31✔
151
        foreach ($this->getHeaders() as $name => $values) {
31✔
152
            foreach ($values as $value) {
31✔
153
                $data .= "{$name}: {$value}\r\n";
31✔
154
            }
155
        }
156
        $data .= "\r\n";
31✔
157
        return $data;
31✔
158
    }
159

160
    private function handleHeader($name, $value): void
161
    {
162
        // @todo: Add all available characters, these are just some of them.
163
        if (!preg_match('|^[0-9a-zA-Z#_-]+$|', $name)) {
31✔
164
            throw new InvalidArgumentException("'{$name}' is not a valid header field name.");
×
165
        }
166
        $value = is_array($value) ? $value : [$value];
31✔
167
        foreach ($value as $content) {
31✔
168
            if (!(is_string($content) || is_numeric($content)) || empty($content = trim($content))) {
31✔
169
                throw new InvalidArgumentException("Invalid header value(s) provided.");
×
170
            }
171
            $this->headers[strtolower($name)][$name][] = $content;
31✔
172
        }
173
    }
174
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc