Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
IcapResponse
100.00% covered (success)
100.00%
8 / 8
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%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * SPDX-License-Identifier: EUPL-1.2
5 *
6 * This file is part of icap-flow.
7 *
8 * Licensed under the EUPL, Version 1.2 only (the "Licence");
9 * you may not use this work except in compliance with the Licence.
10 * You may obtain a copy of the Licence at:
11 *
12 *     https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the Licence is distributed on an "AS IS" basis,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 */
18
19declare(strict_types=1);
20
21namespace Ndrstmr\Icap\DTO;
22
23/**
24 * Immutable DTO representing an ICAP response.
25 */
26final readonly class IcapResponse
27{
28    /** @var array<string, string[]> */
29    public array $headers;
30
31    /**
32     * @param int $statusCode HTTP status code, e.g. 200, 204, 500
33     * @param array<string, string[]> $headers Response headers
34     * @param string $body Response body content
35     */
36    public function __construct(
37        public int $statusCode,
38        array $headers = [],
39        public string $body = ''
40    ) {
41        $this->headers = array_map(fn ($v) => (array) $v, $headers);
42    }
43
44    /**
45     * @param string|string[] $value
46     */
47    public function withHeader(string $name, string|array $value): self
48    {
49        $headers = $this->headers;
50        $headers[$name] = (array) $value;
51
52        return new self(
53            $this->statusCode,
54            $headers,
55            $this->body,
56        );
57    }
58}