Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
IcapRequest
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 withHeader
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Ndrstmr\Icap\DTO;
6
7/**
8 * Immutable data object representing an ICAP request.
9 */
10final readonly class IcapRequest
11{
12    /** @var array<string, string[]> */
13    public array $headers;
14
15    /**
16     * @param string $method HTTP method, e.g. 'GET', 'POST', 'OPTIONS'
17     * @param string $uri Request URI, e.g. 'icap://example.com/service'
18     * @param array<string, string[]> $headers Request headers
19     * @param resource|string $body Request body content, can be a string or stream resource
20     */
21    public function __construct(
22        public string $method,
23        public string $uri = '/',
24        array $headers = [],
25        public mixed $body = ''
26    ) {
27        $this->headers = array_map(fn ($v) => (array) $v, $headers);
28    }
29
30    /**
31     * @param string|string[] $value
32     */
33    public function withHeader(string $name, string|array $value): self
34    {
35        $headers = $this->headers;
36        $headers[$name] = (array) $value;
37
38        return new self(
39            $this->method,
40            $this->uri,
41            $headers,
42            $this->body,
43        );
44    }
45}