@@ -10,58 +10,58 @@ |
||
10 | 10 | */ |
11 | 11 | return [ |
12 | 12 | |
13 | - 'ok' => 'OK', |
|
14 | - 'no_error_message' => 'Fehler #:api_code', |
|
13 | + 'ok' => 'OK', |
|
14 | + 'no_error_message' => 'Fehler #:api_code', |
|
15 | 15 | |
16 | - // Used by Exception Handler Helper (when used) |
|
17 | - 'uncaught_exception' => 'Ungefangene Ausnahme: :message', |
|
18 | - 'http_exception' => 'HTTP Ausnahme: :message', |
|
16 | + // Used by Exception Handler Helper (when used) |
|
17 | + 'uncaught_exception' => 'Ungefangene Ausnahme: :message', |
|
18 | + 'http_exception' => 'HTTP Ausnahme: :message', |
|
19 | 19 | |
20 | - // HttpException handler (added in 6.4.0) |
|
21 | - // Error messages for HttpException caught w/o custom messages |
|
22 | - // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
23 | - // |
|
24 | - // German translation based on https://wiki.selfhtml.org/wiki/HTTP/Statuscodes |
|
25 | - 'http_400' => 'Ungültige Anfrage', |
|
26 | - 'http_401' => 'Unautorisiert', |
|
27 | - 'http_402' => 'Bezahlung benötigt', |
|
28 | - 'http_403' => 'Verboten', |
|
29 | - 'http_404' => 'Nicht gefunden', |
|
30 | - 'http_405' => 'Methode nicht erlaubt', |
|
31 | - 'http_406' => 'Nicht akzeptabel', |
|
32 | - 'http_407' => 'Proxy-Authentifizierung benötigt', |
|
33 | - 'http_408' => 'Anfrage-Zeitüberschreitung', |
|
34 | - 'http_409' => 'Konflikt', |
|
35 | - 'http_410' => 'Verschwunden', |
|
36 | - 'http_411' => 'Länge benötigt', |
|
37 | - 'http_412' => 'Vorbedingung missglückt', |
|
38 | - 'http_413' => 'Anfrage-Entität zu groß', |
|
39 | - 'http_414' => 'Anfrage-URI zu lang', |
|
40 | - 'http_415' => 'Nicht unterstützter Medientyp', |
|
41 | - 'http_416' => 'Anfrage-Bereich nicht erfüllbar', |
|
42 | - 'http_417' => 'Erwartung missglückt', |
|
43 | - 'http_421' => 'Fehlgeleitete Anforderung', |
|
44 | - 'http_422' => 'Kann nicht verarbeitet werden', |
|
45 | - 'http_423' => 'Gesperrt', |
|
46 | - 'http_424' => 'Vorhergehende Bedingung nicht erfüllt', |
|
47 | - 'http_425' => 'Too Early', // FIXME |
|
48 | - 'http_426' => 'Update benötigt', |
|
49 | - 'http_428' => ' Vorbedingung benötigt', |
|
50 | - 'http_429' => 'Zu viele Anfragen', |
|
51 | - 'http_431' => 'Headerfelds zu groß', |
|
52 | - 'http_451' => 'Ressource aus rechtlichen Gründen nicht verfügbar', |
|
20 | + // HttpException handler (added in 6.4.0) |
|
21 | + // Error messages for HttpException caught w/o custom messages |
|
22 | + // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
23 | + // |
|
24 | + // German translation based on https://wiki.selfhtml.org/wiki/HTTP/Statuscodes |
|
25 | + 'http_400' => 'Ungültige Anfrage', |
|
26 | + 'http_401' => 'Unautorisiert', |
|
27 | + 'http_402' => 'Bezahlung benötigt', |
|
28 | + 'http_403' => 'Verboten', |
|
29 | + 'http_404' => 'Nicht gefunden', |
|
30 | + 'http_405' => 'Methode nicht erlaubt', |
|
31 | + 'http_406' => 'Nicht akzeptabel', |
|
32 | + 'http_407' => 'Proxy-Authentifizierung benötigt', |
|
33 | + 'http_408' => 'Anfrage-Zeitüberschreitung', |
|
34 | + 'http_409' => 'Konflikt', |
|
35 | + 'http_410' => 'Verschwunden', |
|
36 | + 'http_411' => 'Länge benötigt', |
|
37 | + 'http_412' => 'Vorbedingung missglückt', |
|
38 | + 'http_413' => 'Anfrage-Entität zu groß', |
|
39 | + 'http_414' => 'Anfrage-URI zu lang', |
|
40 | + 'http_415' => 'Nicht unterstützter Medientyp', |
|
41 | + 'http_416' => 'Anfrage-Bereich nicht erfüllbar', |
|
42 | + 'http_417' => 'Erwartung missglückt', |
|
43 | + 'http_421' => 'Fehlgeleitete Anforderung', |
|
44 | + 'http_422' => 'Kann nicht verarbeitet werden', |
|
45 | + 'http_423' => 'Gesperrt', |
|
46 | + 'http_424' => 'Vorhergehende Bedingung nicht erfüllt', |
|
47 | + 'http_425' => 'Too Early', // FIXME |
|
48 | + 'http_426' => 'Update benötigt', |
|
49 | + 'http_428' => ' Vorbedingung benötigt', |
|
50 | + 'http_429' => 'Zu viele Anfragen', |
|
51 | + 'http_431' => 'Headerfelds zu groß', |
|
52 | + 'http_451' => 'Ressource aus rechtlichen Gründen nicht verfügbar', |
|
53 | 53 | |
54 | - 'http_500' => 'Interner Server-Fehler', |
|
55 | - 'http_501' => 'Nicht implementiert', |
|
56 | - 'http_502' => 'Schlechtes Portal', |
|
57 | - 'http_503' => 'Dienst nicht verfügbar', |
|
58 | - 'http_504' => 'Portal-Auszeit', |
|
59 | - 'http_505' => 'HTTP-Version nicht unterstützt', |
|
60 | - 'http_506' => 'Variant Also Negotiates', // FIXME |
|
61 | - 'http_507' => 'Speicher des Servers reicht nicht aus', |
|
62 | - 'http_508' => 'Endlosschleife', |
|
63 | - 'http_509' => 'Unassigned', // FIXME |
|
64 | - 'http_510' => 'Zu wenig Informationen', |
|
65 | - 'http_511' => 'Identizifierung benötigt', |
|
54 | + 'http_500' => 'Interner Server-Fehler', |
|
55 | + 'http_501' => 'Nicht implementiert', |
|
56 | + 'http_502' => 'Schlechtes Portal', |
|
57 | + 'http_503' => 'Dienst nicht verfügbar', |
|
58 | + 'http_504' => 'Portal-Auszeit', |
|
59 | + 'http_505' => 'HTTP-Version nicht unterstützt', |
|
60 | + 'http_506' => 'Variant Also Negotiates', // FIXME |
|
61 | + 'http_507' => 'Speicher des Servers reicht nicht aus', |
|
62 | + 'http_508' => 'Endlosschleife', |
|
63 | + 'http_509' => 'Unassigned', // FIXME |
|
64 | + 'http_510' => 'Zu wenig Informationen', |
|
65 | + 'http_511' => 'Identizifierung benötigt', |
|
66 | 66 | ]; |
67 | 67 |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | 'http_422' => 'Kann nicht verarbeitet werden', |
45 | 45 | 'http_423' => 'Gesperrt', |
46 | 46 | 'http_424' => 'Vorhergehende Bedingung nicht erfüllt', |
47 | - 'http_425' => 'Too Early', // FIXME |
|
47 | + 'http_425' => 'Too Early', // FIXME |
|
48 | 48 | 'http_426' => 'Update benötigt', |
49 | 49 | 'http_428' => ' Vorbedingung benötigt', |
50 | 50 | 'http_429' => 'Zu viele Anfragen', |
@@ -57,10 +57,10 @@ discard block |
||
57 | 57 | 'http_503' => 'Dienst nicht verfügbar', |
58 | 58 | 'http_504' => 'Portal-Auszeit', |
59 | 59 | 'http_505' => 'HTTP-Version nicht unterstützt', |
60 | - 'http_506' => 'Variant Also Negotiates', // FIXME |
|
60 | + 'http_506' => 'Variant Also Negotiates', // FIXME |
|
61 | 61 | 'http_507' => 'Speicher des Servers reicht nicht aus', |
62 | 62 | 'http_508' => 'Endlosschleife', |
63 | - 'http_509' => 'Unassigned', // FIXME |
|
63 | + 'http_509' => 'Unassigned', // FIXME |
|
64 | 64 | 'http_510' => 'Zu wenig Informationen', |
65 | 65 | 'http_511' => 'Identizifierung benötigt', |
66 | 66 | ]; |
@@ -10,56 +10,56 @@ |
||
10 | 10 | */ |
11 | 11 | return [ |
12 | 12 | |
13 | - 'ok' => 'OK', |
|
14 | - 'no_error_message' => 'Error #:api_code', |
|
13 | + 'ok' => 'OK', |
|
14 | + 'no_error_message' => 'Error #:api_code', |
|
15 | 15 | |
16 | - // Used by Exception Handler Helper (when used) |
|
17 | - 'uncaught_exception' => 'Uncaught exception: :message', |
|
18 | - 'http_exception' => 'HTTP exception: :message', |
|
16 | + // Used by Exception Handler Helper (when used) |
|
17 | + 'uncaught_exception' => 'Uncaught exception: :message', |
|
18 | + 'http_exception' => 'HTTP exception: :message', |
|
19 | 19 | |
20 | - // HttpException handler (added in 6.4.0) |
|
21 | - // Error messages for HttpException caught w/o custom messages |
|
22 | - // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
23 | - 'http_400' => 'Bad Request', |
|
24 | - 'http_401' => 'Unauthorized', |
|
25 | - 'http_402' => 'Payment Required', |
|
26 | - 'http_403' => 'Forbidden', |
|
27 | - 'http_404' => 'Not Found', |
|
28 | - 'http_405' => 'Method Not Allowed', |
|
29 | - 'http_406' => 'Not Acceptable', |
|
30 | - 'http_407' => 'Proxy Authentication Required', |
|
31 | - 'http_408' => 'Request Timeout', |
|
32 | - 'http_409' => 'Conflict', |
|
33 | - 'http_410' => 'Gone', |
|
34 | - 'http_411' => 'Length Required', |
|
35 | - 'http_412' => 'Precondition Failed', |
|
36 | - 'http_413' => 'Payload Too Large', |
|
37 | - 'http_414' => 'URI Too Long', |
|
38 | - 'http_415' => 'Unsupported Media Type', |
|
39 | - 'http_416' => 'Range Not Satisfiable', |
|
40 | - 'http_417' => 'Expectation Failed', |
|
41 | - 'http_421' => 'Misdirected Request', |
|
42 | - 'http_422' => 'Unprocessable Entity', |
|
43 | - 'http_423' => 'Locked', |
|
44 | - 'http_424' => 'Failed Dependency', |
|
45 | - 'http_425' => 'Too Early', |
|
46 | - 'http_426' => 'Upgrade Required', |
|
47 | - 'http_428' => 'Precondition Required', |
|
48 | - 'http_429' => 'Too Many Requests', |
|
49 | - 'http_431' => 'Request Header Fields Too Large', |
|
50 | - 'http_451' => 'Unavailable For Legal Reasons', |
|
20 | + // HttpException handler (added in 6.4.0) |
|
21 | + // Error messages for HttpException caught w/o custom messages |
|
22 | + // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
23 | + 'http_400' => 'Bad Request', |
|
24 | + 'http_401' => 'Unauthorized', |
|
25 | + 'http_402' => 'Payment Required', |
|
26 | + 'http_403' => 'Forbidden', |
|
27 | + 'http_404' => 'Not Found', |
|
28 | + 'http_405' => 'Method Not Allowed', |
|
29 | + 'http_406' => 'Not Acceptable', |
|
30 | + 'http_407' => 'Proxy Authentication Required', |
|
31 | + 'http_408' => 'Request Timeout', |
|
32 | + 'http_409' => 'Conflict', |
|
33 | + 'http_410' => 'Gone', |
|
34 | + 'http_411' => 'Length Required', |
|
35 | + 'http_412' => 'Precondition Failed', |
|
36 | + 'http_413' => 'Payload Too Large', |
|
37 | + 'http_414' => 'URI Too Long', |
|
38 | + 'http_415' => 'Unsupported Media Type', |
|
39 | + 'http_416' => 'Range Not Satisfiable', |
|
40 | + 'http_417' => 'Expectation Failed', |
|
41 | + 'http_421' => 'Misdirected Request', |
|
42 | + 'http_422' => 'Unprocessable Entity', |
|
43 | + 'http_423' => 'Locked', |
|
44 | + 'http_424' => 'Failed Dependency', |
|
45 | + 'http_425' => 'Too Early', |
|
46 | + 'http_426' => 'Upgrade Required', |
|
47 | + 'http_428' => 'Precondition Required', |
|
48 | + 'http_429' => 'Too Many Requests', |
|
49 | + 'http_431' => 'Request Header Fields Too Large', |
|
50 | + 'http_451' => 'Unavailable For Legal Reasons', |
|
51 | 51 | |
52 | - 'http_500' => 'Internal Server Error', |
|
53 | - 'http_501' => 'Not Implemented', |
|
54 | - 'http_502' => 'Bad Gateway', |
|
55 | - 'http_503' => 'Service Unavailable', |
|
56 | - 'http_504' => 'Gateway Timeout', |
|
57 | - 'http_505' => 'HTTP Version Not Supported', |
|
58 | - 'http_506' => 'Variant Also Negotiates', |
|
59 | - 'http_507' => 'Insufficient Storage', |
|
60 | - 'http_508' => 'Loop Detected', |
|
61 | - 'http_509' => 'Unassigned', |
|
62 | - 'http_510' => 'Not Extended', |
|
63 | - 'http_511' => 'Network Authentication Required', |
|
52 | + 'http_500' => 'Internal Server Error', |
|
53 | + 'http_501' => 'Not Implemented', |
|
54 | + 'http_502' => 'Bad Gateway', |
|
55 | + 'http_503' => 'Service Unavailable', |
|
56 | + 'http_504' => 'Gateway Timeout', |
|
57 | + 'http_505' => 'HTTP Version Not Supported', |
|
58 | + 'http_506' => 'Variant Also Negotiates', |
|
59 | + 'http_507' => 'Insufficient Storage', |
|
60 | + 'http_508' => 'Loop Detected', |
|
61 | + 'http_509' => 'Unassigned', |
|
62 | + 'http_510' => 'Not Extended', |
|
63 | + 'http_511' => 'Network Authentication Required', |
|
64 | 64 | ]; |
65 | 65 |
@@ -10,55 +10,55 @@ |
||
10 | 10 | */ |
11 | 11 | return [ |
12 | 12 | |
13 | - 'ok' => 'OK', |
|
14 | - 'no_error_message' => 'Błąd #:api_code', |
|
13 | + 'ok' => 'OK', |
|
14 | + 'no_error_message' => 'Błąd #:api_code', |
|
15 | 15 | |
16 | - // Used by Exception Handler Helper (when used) |
|
17 | - 'uncaught_exception' => 'Nieprzechwycony wyjątek: :message', |
|
18 | - 'http_exception' => 'Wyjątek HTTP: :message', |
|
16 | + // Used by Exception Handler Helper (when used) |
|
17 | + 'uncaught_exception' => 'Nieprzechwycony wyjątek: :message', |
|
18 | + 'http_exception' => 'Wyjątek HTTP: :message', |
|
19 | 19 | |
20 | - // HttpException handler (added in 6.4.0) |
|
21 | - // Error messages for HttpException caught w/o custom messages |
|
22 | - // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
23 | - 'http_400' => 'Bad Request', |
|
24 | - 'http_401' => 'Unauthorized', |
|
25 | - 'http_402' => 'Payment Required', |
|
26 | - 'http_403' => 'Forbidden', |
|
27 | - 'http_404' => 'Not Found', |
|
28 | - 'http_405' => 'Method Not Allowed', |
|
29 | - 'http_406' => 'Not Acceptable', |
|
30 | - 'http_407' => 'Proxy Authentication Required', |
|
31 | - 'http_408' => 'Request Timeout', |
|
32 | - 'http_409' => 'Conflict', |
|
33 | - 'http_410' => 'Gone', |
|
34 | - 'http_411' => 'Length Required', |
|
35 | - 'http_412' => 'Precondition Failed', |
|
36 | - 'http_413' => 'Payload Too Large', |
|
37 | - 'http_414' => 'URI Too Long', |
|
38 | - 'http_415' => 'Unsupported Media Type', |
|
39 | - 'http_416' => 'Range Not Satisfiable', |
|
40 | - 'http_417' => 'Expectation Failed', |
|
41 | - 'http_421' => 'Misdirected Request', |
|
42 | - 'http_422' => 'Unprocessable Entity', |
|
43 | - 'http_423' => 'Locked', |
|
44 | - 'http_424' => 'Failed Dependency', |
|
45 | - 'http_425' => 'Too Early', |
|
46 | - 'http_426' => 'Upgrade Required', |
|
47 | - 'http_428' => 'Precondition Required', |
|
48 | - 'http_429' => 'Too Many Requests', |
|
49 | - 'http_431' => 'Request Header Fields Too Large', |
|
50 | - 'http_451' => 'Unavailable For Legal Reasons', |
|
20 | + // HttpException handler (added in 6.4.0) |
|
21 | + // Error messages for HttpException caught w/o custom messages |
|
22 | + // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
23 | + 'http_400' => 'Bad Request', |
|
24 | + 'http_401' => 'Unauthorized', |
|
25 | + 'http_402' => 'Payment Required', |
|
26 | + 'http_403' => 'Forbidden', |
|
27 | + 'http_404' => 'Not Found', |
|
28 | + 'http_405' => 'Method Not Allowed', |
|
29 | + 'http_406' => 'Not Acceptable', |
|
30 | + 'http_407' => 'Proxy Authentication Required', |
|
31 | + 'http_408' => 'Request Timeout', |
|
32 | + 'http_409' => 'Conflict', |
|
33 | + 'http_410' => 'Gone', |
|
34 | + 'http_411' => 'Length Required', |
|
35 | + 'http_412' => 'Precondition Failed', |
|
36 | + 'http_413' => 'Payload Too Large', |
|
37 | + 'http_414' => 'URI Too Long', |
|
38 | + 'http_415' => 'Unsupported Media Type', |
|
39 | + 'http_416' => 'Range Not Satisfiable', |
|
40 | + 'http_417' => 'Expectation Failed', |
|
41 | + 'http_421' => 'Misdirected Request', |
|
42 | + 'http_422' => 'Unprocessable Entity', |
|
43 | + 'http_423' => 'Locked', |
|
44 | + 'http_424' => 'Failed Dependency', |
|
45 | + 'http_425' => 'Too Early', |
|
46 | + 'http_426' => 'Upgrade Required', |
|
47 | + 'http_428' => 'Precondition Required', |
|
48 | + 'http_429' => 'Too Many Requests', |
|
49 | + 'http_431' => 'Request Header Fields Too Large', |
|
50 | + 'http_451' => 'Unavailable For Legal Reasons', |
|
51 | 51 | |
52 | - 'http_500' => 'Internal Server Error', |
|
53 | - 'http_501' => 'Not Implemented', |
|
54 | - 'http_502' => 'Bad Gateway', |
|
55 | - 'http_503' => 'Service Unavailable', |
|
56 | - 'http_504' => 'Gateway Timeout', |
|
57 | - 'http_505' => 'HTTP Version Not Supported', |
|
58 | - 'http_506' => 'Variant Also Negotiates', |
|
59 | - 'http_507' => 'Insufficient Storage', |
|
60 | - 'http_508' => 'Loop Detected', |
|
61 | - 'http_509' => 'Unassigned', |
|
62 | - 'http_510' => 'Not Extended', |
|
63 | - 'http_511' => 'Network Authentication Required', |
|
52 | + 'http_500' => 'Internal Server Error', |
|
53 | + 'http_501' => 'Not Implemented', |
|
54 | + 'http_502' => 'Bad Gateway', |
|
55 | + 'http_503' => 'Service Unavailable', |
|
56 | + 'http_504' => 'Gateway Timeout', |
|
57 | + 'http_505' => 'HTTP Version Not Supported', |
|
58 | + 'http_506' => 'Variant Also Negotiates', |
|
59 | + 'http_507' => 'Insufficient Storage', |
|
60 | + 'http_508' => 'Loop Detected', |
|
61 | + 'http_509' => 'Unassigned', |
|
62 | + 'http_510' => 'Not Extended', |
|
63 | + 'http_511' => 'Network Authentication Required', |
|
64 | 64 | ]; |
@@ -22,175 +22,175 @@ |
||
22 | 22 | */ |
23 | 23 | class Converter |
24 | 24 | { |
25 | - /** |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - protected $classes = []; |
|
29 | - |
|
30 | - /** |
|
31 | - * Converter constructor. |
|
32 | - * |
|
33 | - * @throws \RuntimeException |
|
34 | - */ |
|
35 | - public function __construct() |
|
36 | - { |
|
37 | - $this->classes = static::getClassesMapping() ?? []; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * Returns local copy of configuration mapping for the classes. |
|
42 | - * |
|
43 | - * @return array |
|
44 | - */ |
|
45 | - public function getClasses(): array |
|
46 | - { |
|
47 | - return $this->classes; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * Checks if we have "classes" mapping configured for $data object class. |
|
52 | - * Returns @true if there's valid config for this class. |
|
53 | - * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
54 | - * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
55 | - * |
|
56 | - * @param object $data Object to check mapping for. |
|
57 | - * |
|
58 | - * @return array |
|
59 | - * |
|
60 | - * @throws \InvalidArgumentException |
|
61 | - */ |
|
62 | - protected function getClassMappingConfigOrThrow(object $data): array |
|
63 | - { |
|
64 | - $result = null; |
|
65 | - |
|
66 | - // check for exact class name match... |
|
67 | - $cls = \get_class($data); |
|
68 | - if (\is_string($cls)) { |
|
69 | - if (\array_key_exists($cls, $this->classes)) { |
|
70 | - $result = $this->classes[ $cls ]; |
|
71 | - } else { |
|
72 | - // no exact match, then lets try with `instanceof` |
|
73 | - foreach (\array_keys($this->getClasses()) as $class_name) { |
|
74 | - if ($data instanceof $class_name) { |
|
75 | - $result = $this->classes[ $class_name ]; |
|
76 | - break; |
|
77 | - } |
|
78 | - } |
|
79 | - } |
|
80 | - } |
|
81 | - |
|
82 | - if ($result === null) { |
|
83 | - throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
84 | - } |
|
85 | - |
|
86 | - return $result; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * We need to prepare source data |
|
91 | - * |
|
92 | - * @param object|array|null $data |
|
93 | - * |
|
94 | - * @return array|null |
|
95 | - * |
|
96 | - * @throws \InvalidArgumentException |
|
97 | - */ |
|
98 | - public function convert($data = null): ?array |
|
99 | - { |
|
100 | - if ($data === null) { |
|
101 | - return null; |
|
102 | - } |
|
103 | - |
|
104 | - Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY, |
|
105 | - Validator::TYPE_OBJECT]); |
|
106 | - |
|
107 | - if (\is_object($data)) { |
|
108 | - $cfg = $this->getClassMappingConfigOrThrow($data); |
|
109 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
110 | - $data = $worker->convert($data, $cfg); |
|
111 | - } else { |
|
112 | - $data = $this->convertArray($data); |
|
113 | - } |
|
114 | - |
|
115 | - return $data; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Recursively walks $data array and converts all known objects if found. Note |
|
120 | - * $data array is passed by reference so source $data array may be modified. |
|
121 | - * |
|
122 | - * @param array $data array to recursively convert known elements of |
|
123 | - * |
|
124 | - * @return array |
|
125 | - * |
|
126 | - * @throws \RuntimeException |
|
127 | - */ |
|
128 | - protected function convertArray(array $data): array |
|
129 | - { |
|
130 | - // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
131 | - // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
132 | - // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
133 | - $string_keys_cnt = 0; |
|
134 | - $int_keys_cnt = 0; |
|
135 | - foreach ($data as $key => $val) { |
|
136 | - if (\is_int($key)) { |
|
137 | - $int_keys_cnt++; |
|
138 | - } else { |
|
139 | - $string_keys_cnt++; |
|
140 | - } |
|
141 | - |
|
142 | - if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
143 | - throw new \RuntimeException( |
|
144 | - 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
145 | - 'Arrays with mixed keys are not supported by design.'); |
|
146 | - } |
|
147 | - } |
|
148 | - |
|
149 | - foreach ($data as $key => $val) { |
|
150 | - if (\is_array($val)) { |
|
151 | - $data[ $key ] = $this->convertArray($val); |
|
152 | - } elseif (\is_object($val)) { |
|
153 | - $cfg = $this->getClassMappingConfigOrThrow($val); |
|
154 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
155 | - $converted_data = $worker->convert($val, $cfg); |
|
156 | - $data[ $key ] = $converted_data; |
|
157 | - } |
|
158 | - } |
|
159 | - |
|
160 | - return $data; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Reads and validates "classes" config mapping |
|
165 | - * |
|
166 | - * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
167 | - * |
|
168 | - * @throws \RuntimeException if "classes" mapping is technically invalid (i.e. not array etc). |
|
169 | - */ |
|
170 | - protected static function getClassesMapping(): array |
|
171 | - { |
|
172 | - $classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER); |
|
173 | - |
|
174 | - if ($classes !== null) { |
|
175 | - if (!\is_array($classes)) { |
|
176 | - throw new \RuntimeException( |
|
177 | - \sprintf('CONFIG: "classes" mapping must be an array (%s given)', \gettype($classes))); |
|
178 | - } |
|
179 | - |
|
180 | - $mandatory_keys = [ |
|
181 | - ResponseBuilder::KEY_HANDLER, |
|
182 | - ]; |
|
183 | - foreach ($classes as $class_name => $class_config) { |
|
184 | - foreach ($mandatory_keys as $key_name) { |
|
185 | - if (!\array_key_exists($key_name, $class_config)) { |
|
186 | - throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
187 | - } |
|
188 | - } |
|
189 | - } |
|
190 | - } else { |
|
191 | - $classes = []; |
|
192 | - } |
|
193 | - |
|
194 | - return $classes; |
|
195 | - } |
|
25 | + /** |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + protected $classes = []; |
|
29 | + |
|
30 | + /** |
|
31 | + * Converter constructor. |
|
32 | + * |
|
33 | + * @throws \RuntimeException |
|
34 | + */ |
|
35 | + public function __construct() |
|
36 | + { |
|
37 | + $this->classes = static::getClassesMapping() ?? []; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * Returns local copy of configuration mapping for the classes. |
|
42 | + * |
|
43 | + * @return array |
|
44 | + */ |
|
45 | + public function getClasses(): array |
|
46 | + { |
|
47 | + return $this->classes; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * Checks if we have "classes" mapping configured for $data object class. |
|
52 | + * Returns @true if there's valid config for this class. |
|
53 | + * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
54 | + * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
55 | + * |
|
56 | + * @param object $data Object to check mapping for. |
|
57 | + * |
|
58 | + * @return array |
|
59 | + * |
|
60 | + * @throws \InvalidArgumentException |
|
61 | + */ |
|
62 | + protected function getClassMappingConfigOrThrow(object $data): array |
|
63 | + { |
|
64 | + $result = null; |
|
65 | + |
|
66 | + // check for exact class name match... |
|
67 | + $cls = \get_class($data); |
|
68 | + if (\is_string($cls)) { |
|
69 | + if (\array_key_exists($cls, $this->classes)) { |
|
70 | + $result = $this->classes[ $cls ]; |
|
71 | + } else { |
|
72 | + // no exact match, then lets try with `instanceof` |
|
73 | + foreach (\array_keys($this->getClasses()) as $class_name) { |
|
74 | + if ($data instanceof $class_name) { |
|
75 | + $result = $this->classes[ $class_name ]; |
|
76 | + break; |
|
77 | + } |
|
78 | + } |
|
79 | + } |
|
80 | + } |
|
81 | + |
|
82 | + if ($result === null) { |
|
83 | + throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
84 | + } |
|
85 | + |
|
86 | + return $result; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * We need to prepare source data |
|
91 | + * |
|
92 | + * @param object|array|null $data |
|
93 | + * |
|
94 | + * @return array|null |
|
95 | + * |
|
96 | + * @throws \InvalidArgumentException |
|
97 | + */ |
|
98 | + public function convert($data = null): ?array |
|
99 | + { |
|
100 | + if ($data === null) { |
|
101 | + return null; |
|
102 | + } |
|
103 | + |
|
104 | + Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY, |
|
105 | + Validator::TYPE_OBJECT]); |
|
106 | + |
|
107 | + if (\is_object($data)) { |
|
108 | + $cfg = $this->getClassMappingConfigOrThrow($data); |
|
109 | + $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
110 | + $data = $worker->convert($data, $cfg); |
|
111 | + } else { |
|
112 | + $data = $this->convertArray($data); |
|
113 | + } |
|
114 | + |
|
115 | + return $data; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Recursively walks $data array and converts all known objects if found. Note |
|
120 | + * $data array is passed by reference so source $data array may be modified. |
|
121 | + * |
|
122 | + * @param array $data array to recursively convert known elements of |
|
123 | + * |
|
124 | + * @return array |
|
125 | + * |
|
126 | + * @throws \RuntimeException |
|
127 | + */ |
|
128 | + protected function convertArray(array $data): array |
|
129 | + { |
|
130 | + // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
131 | + // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
132 | + // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
133 | + $string_keys_cnt = 0; |
|
134 | + $int_keys_cnt = 0; |
|
135 | + foreach ($data as $key => $val) { |
|
136 | + if (\is_int($key)) { |
|
137 | + $int_keys_cnt++; |
|
138 | + } else { |
|
139 | + $string_keys_cnt++; |
|
140 | + } |
|
141 | + |
|
142 | + if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
143 | + throw new \RuntimeException( |
|
144 | + 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
145 | + 'Arrays with mixed keys are not supported by design.'); |
|
146 | + } |
|
147 | + } |
|
148 | + |
|
149 | + foreach ($data as $key => $val) { |
|
150 | + if (\is_array($val)) { |
|
151 | + $data[ $key ] = $this->convertArray($val); |
|
152 | + } elseif (\is_object($val)) { |
|
153 | + $cfg = $this->getClassMappingConfigOrThrow($val); |
|
154 | + $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
155 | + $converted_data = $worker->convert($val, $cfg); |
|
156 | + $data[ $key ] = $converted_data; |
|
157 | + } |
|
158 | + } |
|
159 | + |
|
160 | + return $data; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Reads and validates "classes" config mapping |
|
165 | + * |
|
166 | + * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
167 | + * |
|
168 | + * @throws \RuntimeException if "classes" mapping is technically invalid (i.e. not array etc). |
|
169 | + */ |
|
170 | + protected static function getClassesMapping(): array |
|
171 | + { |
|
172 | + $classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER); |
|
173 | + |
|
174 | + if ($classes !== null) { |
|
175 | + if (!\is_array($classes)) { |
|
176 | + throw new \RuntimeException( |
|
177 | + \sprintf('CONFIG: "classes" mapping must be an array (%s given)', \gettype($classes))); |
|
178 | + } |
|
179 | + |
|
180 | + $mandatory_keys = [ |
|
181 | + ResponseBuilder::KEY_HANDLER, |
|
182 | + ]; |
|
183 | + foreach ($classes as $class_name => $class_config) { |
|
184 | + foreach ($mandatory_keys as $key_name) { |
|
185 | + if (!\array_key_exists($key_name, $class_config)) { |
|
186 | + throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
187 | + } |
|
188 | + } |
|
189 | + } |
|
190 | + } else { |
|
191 | + $classes = []; |
|
192 | + } |
|
193 | + |
|
194 | + return $classes; |
|
195 | + } |
|
196 | 196 | } |
@@ -67,12 +67,12 @@ discard block |
||
67 | 67 | $cls = \get_class($data); |
68 | 68 | if (\is_string($cls)) { |
69 | 69 | if (\array_key_exists($cls, $this->classes)) { |
70 | - $result = $this->classes[ $cls ]; |
|
70 | + $result = $this->classes[$cls]; |
|
71 | 71 | } else { |
72 | 72 | // no exact match, then lets try with `instanceof` |
73 | 73 | foreach (\array_keys($this->getClasses()) as $class_name) { |
74 | 74 | if ($data instanceof $class_name) { |
75 | - $result = $this->classes[ $class_name ]; |
|
75 | + $result = $this->classes[$class_name]; |
|
76 | 76 | break; |
77 | 77 | } |
78 | 78 | } |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | |
107 | 107 | if (\is_object($data)) { |
108 | 108 | $cfg = $this->getClassMappingConfigOrThrow($data); |
109 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
109 | + $worker = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
110 | 110 | $data = $worker->convert($data, $cfg); |
111 | 111 | } else { |
112 | 112 | $data = $this->convertArray($data); |
@@ -148,12 +148,12 @@ discard block |
||
148 | 148 | |
149 | 149 | foreach ($data as $key => $val) { |
150 | 150 | if (\is_array($val)) { |
151 | - $data[ $key ] = $this->convertArray($val); |
|
151 | + $data[$key] = $this->convertArray($val); |
|
152 | 152 | } elseif (\is_object($val)) { |
153 | 153 | $cfg = $this->getClassMappingConfigOrThrow($val); |
154 | - $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ](); |
|
154 | + $worker = new $cfg[ResponseBuilder::KEY_HANDLER](); |
|
155 | 155 | $converted_data = $worker->convert($val, $cfg); |
156 | - $data[ $key ] = $converted_data; |
|
156 | + $data[$key] = $converted_data; |
|
157 | 157 | } |
158 | 158 | } |
159 | 159 |
@@ -21,124 +21,124 @@ |
||
21 | 21 | */ |
22 | 22 | trait ApiCodesHelpers |
23 | 23 | { |
24 | - /** |
|
25 | - * Returns lowest allowed error code for this module |
|
26 | - * |
|
27 | - * @return integer |
|
28 | - * |
|
29 | - * @throws \RuntimeException Throws exception if no min_code set up |
|
30 | - */ |
|
31 | - public static function getMinCode(): int |
|
32 | - { |
|
33 | - $key = ResponseBuilder::CONF_KEY_MIN_CODE; |
|
34 | - $min_code = Config::get($key, null); |
|
35 | - |
|
36 | - if ($min_code === null) { |
|
37 | - throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
38 | - } |
|
39 | - |
|
40 | - return $min_code; |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Returns highest allowed error code for this module |
|
45 | - * |
|
46 | - * @return integer |
|
47 | - * |
|
48 | - * @throws \RuntimeException Throws exception if no max_code set up |
|
49 | - */ |
|
50 | - public static function getMaxCode(): int |
|
51 | - { |
|
52 | - $key = ResponseBuilder::CONF_KEY_MAX_CODE; |
|
53 | - $max_code = Config::get($key, null); |
|
54 | - |
|
55 | - if ($max_code === null) { |
|
56 | - throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
57 | - } |
|
58 | - |
|
59 | - return $max_code; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Returns array of error code constants defined in this class. Used mainly for debugging/tests |
|
64 | - * |
|
65 | - * @return array |
|
66 | - */ |
|
67 | - public static function getApiCodeConstants(): array |
|
68 | - { |
|
69 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
70 | - return (new \ReflectionClass(static::class))->getConstants(); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Returns complete error code to locale string mapping array |
|
75 | - * |
|
76 | - * @return array |
|
77 | - * |
|
78 | - * @throws \RuntimeException Thrown when builder map is not configured. |
|
79 | - */ |
|
80 | - public static function getMap(): array |
|
81 | - { |
|
82 | - $user_map = Config::get(ResponseBuilder::CONF_KEY_MAP, null); |
|
83 | - if ($user_map === null) { |
|
84 | - throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', ResponseBuilder::CONF_KEY_MAP)); |
|
85 | - } |
|
86 | - if (!\is_array($user_map)) { |
|
87 | - throw new \RuntimeException(sprintf('CONFIG: "%s" must be an array', ResponseBuilder::CONF_KEY_MAP)); |
|
88 | - } |
|
89 | - return Util::mergeConfig(BaseApiCodes::getBaseMap(), $user_map); |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Returns locale mappings key for given api code or @null if there's no mapping |
|
94 | - * |
|
95 | - * @param integer $api_code Api code to look for mapped message for. |
|
96 | - * |
|
97 | - * @return string|null |
|
98 | - * |
|
99 | - * @throws \InvalidArgumentException If $code is not in allowed range. |
|
100 | - */ |
|
101 | - public static function getCodeMessageKey(int $api_code): ?string |
|
102 | - { |
|
103 | - if (!static::isCodeValid($api_code)) { |
|
104 | - $min = static::getMinCode(); |
|
105 | - $max = static::getMaxCode(); |
|
106 | - throw new \InvalidArgumentException("API code value ({$api_code}) is out of allowed range {$min}-{$max}"); |
|
107 | - } |
|
108 | - |
|
109 | - $map = static::getMap(); |
|
110 | - |
|
111 | - return $map[ $api_code ] ?? null; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Checks if given API $code can be used in current configuration. |
|
116 | - * |
|
117 | - * @param int $code API code to validate |
|
118 | - * |
|
119 | - * @return bool |
|
120 | - */ |
|
121 | - public static function isCodeValid(int $code): bool |
|
122 | - { |
|
123 | - return ($code === 0) || (($code >= static::getMinCode()) && ($code <= static::getMaxCode())); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Returns final API code for internal code, remapped to configured code range |
|
128 | - * |
|
129 | - * @param int $internal_code |
|
130 | - * |
|
131 | - * @return int |
|
132 | - * |
|
133 | - * @throws \InvalidArgumentException |
|
134 | - */ |
|
135 | - public static function getCodeForInternalOffset(int $internal_code): int |
|
136 | - { |
|
137 | - $min = static::RESERVED_MIN_API_CODE_OFFSET; |
|
138 | - $max = static::RESERVED_MAX_API_CODE_OFFSET; |
|
139 | - Validator::assertIsIntRange('internal_code', $internal_code, $min, $max); |
|
140 | - |
|
141 | - return ($internal_code === 0) ? 0 : $internal_code + static::getMinCode(); |
|
142 | - } |
|
24 | + /** |
|
25 | + * Returns lowest allowed error code for this module |
|
26 | + * |
|
27 | + * @return integer |
|
28 | + * |
|
29 | + * @throws \RuntimeException Throws exception if no min_code set up |
|
30 | + */ |
|
31 | + public static function getMinCode(): int |
|
32 | + { |
|
33 | + $key = ResponseBuilder::CONF_KEY_MIN_CODE; |
|
34 | + $min_code = Config::get($key, null); |
|
35 | + |
|
36 | + if ($min_code === null) { |
|
37 | + throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
38 | + } |
|
39 | + |
|
40 | + return $min_code; |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Returns highest allowed error code for this module |
|
45 | + * |
|
46 | + * @return integer |
|
47 | + * |
|
48 | + * @throws \RuntimeException Throws exception if no max_code set up |
|
49 | + */ |
|
50 | + public static function getMaxCode(): int |
|
51 | + { |
|
52 | + $key = ResponseBuilder::CONF_KEY_MAX_CODE; |
|
53 | + $max_code = Config::get($key, null); |
|
54 | + |
|
55 | + if ($max_code === null) { |
|
56 | + throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', $key)); |
|
57 | + } |
|
58 | + |
|
59 | + return $max_code; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Returns array of error code constants defined in this class. Used mainly for debugging/tests |
|
64 | + * |
|
65 | + * @return array |
|
66 | + */ |
|
67 | + public static function getApiCodeConstants(): array |
|
68 | + { |
|
69 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
70 | + return (new \ReflectionClass(static::class))->getConstants(); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Returns complete error code to locale string mapping array |
|
75 | + * |
|
76 | + * @return array |
|
77 | + * |
|
78 | + * @throws \RuntimeException Thrown when builder map is not configured. |
|
79 | + */ |
|
80 | + public static function getMap(): array |
|
81 | + { |
|
82 | + $user_map = Config::get(ResponseBuilder::CONF_KEY_MAP, null); |
|
83 | + if ($user_map === null) { |
|
84 | + throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', ResponseBuilder::CONF_KEY_MAP)); |
|
85 | + } |
|
86 | + if (!\is_array($user_map)) { |
|
87 | + throw new \RuntimeException(sprintf('CONFIG: "%s" must be an array', ResponseBuilder::CONF_KEY_MAP)); |
|
88 | + } |
|
89 | + return Util::mergeConfig(BaseApiCodes::getBaseMap(), $user_map); |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Returns locale mappings key for given api code or @null if there's no mapping |
|
94 | + * |
|
95 | + * @param integer $api_code Api code to look for mapped message for. |
|
96 | + * |
|
97 | + * @return string|null |
|
98 | + * |
|
99 | + * @throws \InvalidArgumentException If $code is not in allowed range. |
|
100 | + */ |
|
101 | + public static function getCodeMessageKey(int $api_code): ?string |
|
102 | + { |
|
103 | + if (!static::isCodeValid($api_code)) { |
|
104 | + $min = static::getMinCode(); |
|
105 | + $max = static::getMaxCode(); |
|
106 | + throw new \InvalidArgumentException("API code value ({$api_code}) is out of allowed range {$min}-{$max}"); |
|
107 | + } |
|
108 | + |
|
109 | + $map = static::getMap(); |
|
110 | + |
|
111 | + return $map[ $api_code ] ?? null; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Checks if given API $code can be used in current configuration. |
|
116 | + * |
|
117 | + * @param int $code API code to validate |
|
118 | + * |
|
119 | + * @return bool |
|
120 | + */ |
|
121 | + public static function isCodeValid(int $code): bool |
|
122 | + { |
|
123 | + return ($code === 0) || (($code >= static::getMinCode()) && ($code <= static::getMaxCode())); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Returns final API code for internal code, remapped to configured code range |
|
128 | + * |
|
129 | + * @param int $internal_code |
|
130 | + * |
|
131 | + * @return int |
|
132 | + * |
|
133 | + * @throws \InvalidArgumentException |
|
134 | + */ |
|
135 | + public static function getCodeForInternalOffset(int $internal_code): int |
|
136 | + { |
|
137 | + $min = static::RESERVED_MIN_API_CODE_OFFSET; |
|
138 | + $max = static::RESERVED_MAX_API_CODE_OFFSET; |
|
139 | + Validator::assertIsIntRange('internal_code', $internal_code, $min, $max); |
|
140 | + |
|
141 | + return ($internal_code === 0) ? 0 : $internal_code + static::getMinCode(); |
|
142 | + } |
|
143 | 143 | |
144 | 144 | } |