Issues (125)

src/Exception/HttpException.php (3 issues)

1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Http\Router\Exception;
15
16
use Fig\Http\Message\StatusCodeInterface;
17
use RuntimeException;
18
use Stringable;
19
use Sunrise\Http\Router\Dictionary\TranslationDomain;
20
use Sunrise\Http\Router\Validation\ConstraintViolationInterface;
21
use Throwable;
22
23
use function implode;
24
use function strtr;
25
26
/**
27
 * @since 3.0.0
28
 */
29
class HttpException extends RuntimeException implements StatusCodeInterface
30
{
31
    /**
32
     * The exception's non-interpolated message.
33
     */
34
    private string $messageTemplate;
35
36
    /**
37
     * @var array<string, mixed>
38
     */
39
    private array $messagePlaceholders = [];
40
41
    /**
42
     * @var list<array{0: string, 1: string}>
0 ignored issues
show
The type Sunrise\Http\Router\Exception\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
     */
44
    private array $headerFields = [];
45
46
    /**
47
     * @var list<ConstraintViolationInterface>
48
     */
49
    private array $constraintViolations = [];
50
51
    private string $translationDomain = TranslationDomain::ROUTER;
52
53 62
    public function __construct(string $message, int $code, ?Throwable $previous = null)
54
    {
55 62
        parent::__construct($message, $code, $previous);
56
57 62
        $this->messageTemplate = $message;
58
    }
59
60
    /**
61
     * Returns the exception's <b>non-interpolated</b> message.
62
     */
63 17
    final public function getMessageTemplate(): string
64
    {
65 17
        return $this->messageTemplate;
66
    }
67
68
    /**
69
     * @return array<string, mixed>
70
     */
71 6
    final public function getMessagePlaceholders(): array
72
    {
73 6
        return $this->messagePlaceholders;
74
    }
75
76
    /**
77
     * @return list<array{0: string, 1: string}>
78
     */
79 8
    final public function getHeaderFields(): array
80
    {
81 8
        return $this->headerFields;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->headerFields returns the type array which is incompatible with the documented return type Sunrise\Http\Router\Exception\list.
Loading history...
82
    }
83
84
    /**
85
     * @return list<ConstraintViolationInterface>
86
     */
87 19
    final public function getConstraintViolations(): array
88
    {
89 19
        return $this->constraintViolations;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->constraintViolations returns the type array which is incompatible with the documented return type Sunrise\Http\Router\Exception\list.
Loading history...
90
    }
91
92 6
    final public function getTranslationDomain(): string
93
    {
94 6
        return $this->translationDomain;
95
    }
96
97 27
    final public function addMessagePlaceholder(string $placeholder, mixed $replacement): static
98
    {
99 27
        $this->message = strtr($this->message, [$placeholder => $replacement]);
100
101 27
        $this->messagePlaceholders[$placeholder] = $replacement;
102
103 27
        return $this;
104
    }
105
106 4
    final public function addHeaderField(string $fieldName, string|Stringable ...$fieldValues): static
107
    {
108
        // https://datatracker.ietf.org/doc/html/rfc7230#section-7
109 4
        $fieldValue = implode(', ', $fieldValues);
110
111 4
        $this->headerFields[] = [$fieldName, $fieldValue];
112
113 4
        return $this;
114
    }
115
116 35
    final public function addConstraintViolation(ConstraintViolationInterface ...$constraintViolations): static
117
    {
118 35
        foreach ($constraintViolations as $constraintViolation) {
119 27
            $this->constraintViolations[] = $constraintViolation;
120
        }
121
122 35
        return $this;
123
    }
124
125 2
    final public function setTranslationDomain(string $translationDomain): static
126
    {
127 2
        $this->translationDomain = $translationDomain;
128
129 2
        return $this;
130
    }
131
}
132