SchematorException::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Schemator\Exceptions;
6
7
use Smoren\Schemator\Interfaces\FilterContextInterface;
8
use Throwable;
9
10
/**
11
 * Class SchematorException.
12
 */
13
class SchematorException extends \Exception
14
{
15
    public const UNSUPPORTED_SOURCE_TYPE = 1;
16
    public const UNSUPPORTED_KEY_TYPE = 2;
17
    public const UNSUPPORTED_FILTER_CONFIG_TYPE = 3;
18
    public const FILTER_NOT_FOUND = 4;
19
    public const FILTER_ERROR = 5;
20
    public const CANNOT_GET_VALUE = 6;
21
    public const BAD_FILTER_CONFIG = 7;
22
    public const BAD_FILTER_SOURCE = 8;
23
24
    /**
25
     * @var array<mixed>|null
26
     */
27
    protected ?array $data;
28
29
    /**
30
     * @param string $message
31
     * @param int $code
32
     * @param Throwable|null $previous
33
     * @param array<mixed>|null $data
34
     */
35
    public function __construct(
36
        string $message,
37
        int $code,
38
        Throwable $previous = null,
39
        ?array $data = null
40
    ) {
41
        parent::__construct($message, $code, $previous);
42
        $this->data = $data;
43
    }
44
45
    /**
46
     * @return array<mixed>
47
     */
48
    public function getData(): array
49
    {
50
        return $this->data ?? [];
51
    }
52
53
    /**
54
     * Creates a new exception instance for filter not found error.
55
     *
56
     * @param string $filterName name of filter.
57
     */
58
    public static function createAsFilterNotFound(string $filterName): SchematorException
59
    {
60
        return new SchematorException(
61
            "filter '{$filterName}' not found",
62
            SchematorException::FILTER_NOT_FOUND,
63
            null,
64
            [
65
                'filter_name' => $filterName,
66
            ]
67
        );
68
    }
69
70
    /**
71
     * Creates a new exception instance for "cannot get value" error
72
     * @param mixed $source data to get value from
73
     * @param string $key path key to get value by
74
     * @param Throwable|null $previous previous exception
75
     * @return SchematorException
76
     */
77
    public static function createAsCannotGetValue(
78
        $source,
79
        string $key,
80
        ?Throwable $previous = null
81
    ): SchematorException {
82
        return new SchematorException(
83
            "cannot get value by key '{$key}'",
84
            SchematorException::CANNOT_GET_VALUE,
85
            $previous,
86
            [
87
                'key' => $key,
88
                'source' => $source,
89
            ]
90
        );
91
    }
92
93
    /**
94
     * Creates a new exception instance for "unsupported source type" error
95
     * @param mixed $source source
96
     * @param mixed $key path key to get value by
97
     * @param Throwable|null $previous previous exception
98
     * @return SchematorException
99
     */
100
    public static function createAsUnsupportedSourceType(
101
        $source,
102
        $key,
103
        ?Throwable $previous = null
104
    ): SchematorException {
105
        $sourceType = gettype($source);
106
        return new SchematorException(
107
            "unsupported source type '{$sourceType}'",
108
            SchematorException::UNSUPPORTED_SOURCE_TYPE,
109
            $previous,
110
            [
111
                'key' => $key,
112
                'source' => $source,
113
                'source_type' => $sourceType,
114
            ]
115
        );
116
    }
117
118
    /**
119
     * Creates a new exception instance for "unsupported key type" error
120
     * @param mixed $source source
121
     * @param mixed $key path key to get value by
122
     * @param Throwable|null $previous previous exception
123
     * @return SchematorException
124
     */
125
    public static function createAsUnsupportedKeyType(
126
        $source,
127
        $key,
128
        ?Throwable $previous = null
129
    ): SchematorException {
130
        $keyType = gettype($key);
131
        return new SchematorException(
132
            "unsupported key type '{$keyType}'",
133
            SchematorException::UNSUPPORTED_KEY_TYPE,
134
            $previous,
135
            [
136
                'key' => $key,
137
                'source' => $source,
138
                'key_type' => $keyType,
139
            ]
140
        );
141
    }
142
143
    /**
144
     * Creates a new exception instance for "unsupported filter config type" error
145
     * @param mixed $filterConfig filter config
146
     * @param Throwable|null $previous previous exception
147
     * @return SchematorException
148
     */
149
    public static function createAsUnsupportedFilterConfigType(
150
        $filterConfig,
151
        ?Throwable $previous = null
152
    ): SchematorException {
153
        $filterConfigType = gettype($filterConfig);
154
        return new SchematorException(
155
            "unsupported filter config type '{$filterConfigType}'",
156
            SchematorException::UNSUPPORTED_FILTER_CONFIG_TYPE,
157
            $previous,
158
            [
159
                'filter_config' => $filterConfig,
160
                'filter_config_type' => $filterConfigType,
161
            ]
162
        );
163
    }
164
165
    /**
166
     * Creates a new exception instance for filter execution error
167
     * @param FilterContextInterface $filterContext name of the filter
168
     * @param ?Throwable $previous exception thrown in the filter body
169
     * @return SchematorException
170
     */
171
    public static function createAsFilterError(
172
        FilterContextInterface $filterContext,
173
        ?Throwable $previous = null
174
    ): SchematorException {
175
        return new SchematorException(
176
            "filter error: '{$filterContext->getFilterName()}'",
177
            SchematorException::FILTER_ERROR,
178
            $previous,
179
            [
180
                'error' => $previous ? $previous->getMessage() : "filter error: '{$filterContext->getFilterName()}'",
181
                'filter_name' => $filterContext->getFilterName(),
182
                'config' => $filterContext->getConfig(),
183
                'source' => $filterContext->getSource(),
184
            ]
185
        );
186
    }
187
188
    /**
189
     * Creates a new exception instance for filter config error
190
     * @param FilterContextInterface $filterContext name of the filter
191
     * @param ?Throwable $previous exception thrown in the filter body
192
     * @return SchematorException
193
     */
194
    public static function createAsBadFilterConfig(
195
        FilterContextInterface $filterContext,
196
        ?Throwable $previous = null
197
    ): SchematorException {
198
        return new SchematorException(
199
            "bad config for filter '{$filterContext->getFilterName()}'",
200
            SchematorException::BAD_FILTER_CONFIG,
201
            $previous,
202
            [
203
                'filter_name' => $filterContext->getFilterName(),
204
                'config' => $filterContext->getConfig(),
205
                'source' => $filterContext->getSource(),
206
            ]
207
        );
208
    }
209
210
    /**
211
     * Creates a new exception instance for filter source error
212
     * @param FilterContextInterface $filterContext name of the filter
213
     * @param ?Throwable $previous exception thrown in the filter body
214
     * @return SchematorException
215
     */
216
    public static function createAsBadFilterSource(
217
        FilterContextInterface $filterContext,
218
        ?Throwable $previous = null
219
    ): SchematorException {
220
        return new SchematorException(
221
            "bad source for filter '{$filterContext->getFilterName()}'",
222
            SchematorException::BAD_FILTER_SOURCE,
223
            $previous,
224
            [
225
                'filter_name' => $filterContext->getFilterName(),
226
                'config' => $filterContext->getConfig(),
227
                'source' => $filterContext->getSource(),
228
            ]
229
        );
230
    }
231
}
232