@@ -112,7 +112,7 @@ |
||
112 | 112 | |
113 | 113 | $map = static::getMap(); |
114 | 114 | |
115 | - return $map[ $api_code ] ?? null; |
|
115 | + return $map[$api_code] ?? null; |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | /** |
@@ -21,125 +21,125 @@ |
||
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 | - * @throws \ReflectionException |
|
67 | - */ |
|
68 | - public static function getApiCodeConstants(): array |
|
69 | - { |
|
70 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
71 | - return (new \ReflectionClass(static::class))->getConstants(); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Returns complete error code to locale string mapping array |
|
76 | - * |
|
77 | - * @return array |
|
78 | - * |
|
79 | - * @throws \RuntimeException Thrown when builder map is not configured. |
|
80 | - */ |
|
81 | - public static function getMap(): array |
|
82 | - { |
|
83 | - $user_map = Config::get(ResponseBuilder::CONF_KEY_MAP, null); |
|
84 | - if ($user_map === null) { |
|
85 | - throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', ResponseBuilder::CONF_KEY_MAP)); |
|
86 | - } |
|
87 | - if (!is_array($user_map)) { |
|
88 | - throw new \RuntimeException(sprintf('CONFIG: "%s" must be an array', ResponseBuilder::CONF_KEY_MAP)); |
|
89 | - } |
|
90 | - return Util::mergeConfig(BaseApiCodes::getBaseMap(), $user_map); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Returns locale mappings key for given api code or @null if there's no mapping |
|
95 | - * |
|
96 | - * @param integer $api_code Api code to look for mapped message for. |
|
97 | - * |
|
98 | - * @return string|null |
|
99 | - * |
|
100 | - * @throws \InvalidArgumentException If $code is not in allowed range. |
|
101 | - */ |
|
102 | - public static function getCodeMessageKey(int $api_code): ?string |
|
103 | - { |
|
104 | - if (!static::isCodeValid($api_code)) { |
|
105 | - $min = static::getMinCode(); |
|
106 | - $max = static::getMaxCode(); |
|
107 | - throw new \InvalidArgumentException("API code value ({$api_code}) is out of allowed range {$min}-{$max}"); |
|
108 | - } |
|
109 | - |
|
110 | - $map = static::getMap(); |
|
111 | - |
|
112 | - return $map[ $api_code ] ?? null; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Checks if given API $code can be used in current configuration. |
|
117 | - * |
|
118 | - * @param int $code API code to validate |
|
119 | - * |
|
120 | - * @return bool |
|
121 | - */ |
|
122 | - public static function isCodeValid(int $code): bool |
|
123 | - { |
|
124 | - return ($code === 0) || (($code >= static::getMinCode()) && ($code <= static::getMaxCode())); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Returns final API code for internal code, remapped to configured code range |
|
129 | - * |
|
130 | - * @param int $internal_code |
|
131 | - * |
|
132 | - * @return int |
|
133 | - * |
|
134 | - * @throws \InvalidArgumentException |
|
135 | - */ |
|
136 | - public static function getCodeForInternalOffset(int $internal_code): int |
|
137 | - { |
|
138 | - $min = static::RESERVED_MIN_API_CODE_OFFSET; |
|
139 | - $max = static::RESERVED_MAX_API_CODE_OFFSET; |
|
140 | - Validator::assertIsIntRange('internal_code', $internal_code, $min, $max); |
|
141 | - |
|
142 | - return ($internal_code === 0) ? 0 : $internal_code + static::getMinCode(); |
|
143 | - } |
|
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 | + * @throws \ReflectionException |
|
67 | + */ |
|
68 | + public static function getApiCodeConstants(): array |
|
69 | + { |
|
70 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
71 | + return (new \ReflectionClass(static::class))->getConstants(); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Returns complete error code to locale string mapping array |
|
76 | + * |
|
77 | + * @return array |
|
78 | + * |
|
79 | + * @throws \RuntimeException Thrown when builder map is not configured. |
|
80 | + */ |
|
81 | + public static function getMap(): array |
|
82 | + { |
|
83 | + $user_map = Config::get(ResponseBuilder::CONF_KEY_MAP, null); |
|
84 | + if ($user_map === null) { |
|
85 | + throw new \RuntimeException(sprintf('CONFIG: Missing "%s" key', ResponseBuilder::CONF_KEY_MAP)); |
|
86 | + } |
|
87 | + if (!is_array($user_map)) { |
|
88 | + throw new \RuntimeException(sprintf('CONFIG: "%s" must be an array', ResponseBuilder::CONF_KEY_MAP)); |
|
89 | + } |
|
90 | + return Util::mergeConfig(BaseApiCodes::getBaseMap(), $user_map); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Returns locale mappings key for given api code or @null if there's no mapping |
|
95 | + * |
|
96 | + * @param integer $api_code Api code to look for mapped message for. |
|
97 | + * |
|
98 | + * @return string|null |
|
99 | + * |
|
100 | + * @throws \InvalidArgumentException If $code is not in allowed range. |
|
101 | + */ |
|
102 | + public static function getCodeMessageKey(int $api_code): ?string |
|
103 | + { |
|
104 | + if (!static::isCodeValid($api_code)) { |
|
105 | + $min = static::getMinCode(); |
|
106 | + $max = static::getMaxCode(); |
|
107 | + throw new \InvalidArgumentException("API code value ({$api_code}) is out of allowed range {$min}-{$max}"); |
|
108 | + } |
|
109 | + |
|
110 | + $map = static::getMap(); |
|
111 | + |
|
112 | + return $map[ $api_code ] ?? null; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Checks if given API $code can be used in current configuration. |
|
117 | + * |
|
118 | + * @param int $code API code to validate |
|
119 | + * |
|
120 | + * @return bool |
|
121 | + */ |
|
122 | + public static function isCodeValid(int $code): bool |
|
123 | + { |
|
124 | + return ($code === 0) || (($code >= static::getMinCode()) && ($code <= static::getMaxCode())); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Returns final API code for internal code, remapped to configured code range |
|
129 | + * |
|
130 | + * @param int $internal_code |
|
131 | + * |
|
132 | + * @return int |
|
133 | + * |
|
134 | + * @throws \InvalidArgumentException |
|
135 | + */ |
|
136 | + public static function getCodeForInternalOffset(int $internal_code): int |
|
137 | + { |
|
138 | + $min = static::RESERVED_MIN_API_CODE_OFFSET; |
|
139 | + $max = static::RESERVED_MAX_API_CODE_OFFSET; |
|
140 | + Validator::assertIsIntRange('internal_code', $internal_code, $min, $max); |
|
141 | + |
|
142 | + return ($internal_code === 0) ? 0 : $internal_code + static::getMinCode(); |
|
143 | + } |
|
144 | 144 | |
145 | 145 | } |
@@ -10,54 +10,54 @@ |
||
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', |
|
51 | - 'http_500' => 'Internal Server Error', |
|
52 | - 'http_501' => 'Not Implemented', |
|
53 | - 'http_502' => 'Bad Gateway', |
|
54 | - 'http_503' => 'Service Unavailable', |
|
55 | - 'http_504' => 'Gateway Timeout', |
|
56 | - 'http_505' => 'HTTP Version Not Supported', |
|
57 | - 'http_506' => 'Variant Also Negotiates', |
|
58 | - 'http_507' => 'Insufficient Storage', |
|
59 | - 'http_508' => 'Loop Detected', |
|
60 | - 'http_509' => 'Unassigned', |
|
61 | - 'http_510' => 'Not Extended', |
|
62 | - 'http_511' => 'Network Authentication Required', |
|
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 | + 'http_500' => 'Internal Server Error', |
|
52 | + 'http_501' => 'Not Implemented', |
|
53 | + 'http_502' => 'Bad Gateway', |
|
54 | + 'http_503' => 'Service Unavailable', |
|
55 | + 'http_504' => 'Gateway Timeout', |
|
56 | + 'http_505' => 'HTTP Version Not Supported', |
|
57 | + 'http_506' => 'Variant Also Negotiates', |
|
58 | + 'http_507' => 'Insufficient Storage', |
|
59 | + 'http_508' => 'Loop Detected', |
|
60 | + 'http_509' => 'Unassigned', |
|
61 | + 'http_510' => 'Not Extended', |
|
62 | + 'http_511' => 'Network Authentication Required', |
|
63 | 63 | ]; |
@@ -10,55 +10,55 @@ |
||
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', |
|
51 | - 'http_500' => 'Internal Server Error', |
|
52 | - 'http_501' => 'Not Implemented', |
|
53 | - 'http_502' => 'Bad Gateway', |
|
54 | - 'http_503' => 'Service Unavailable', |
|
55 | - 'http_504' => 'Gateway Timeout', |
|
56 | - 'http_505' => 'HTTP Version Not Supported', |
|
57 | - 'http_506' => 'Variant Also Negotiates', |
|
58 | - 'http_507' => 'Insufficient Storage', |
|
59 | - 'http_508' => 'Loop Detected', |
|
60 | - 'http_509' => 'Unassigned', |
|
61 | - 'http_510' => 'Not Extended', |
|
62 | - 'http_511' => 'Network Authentication Required', |
|
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 | + 'http_500' => 'Internal Server Error', |
|
52 | + 'http_501' => 'Not Implemented', |
|
53 | + 'http_502' => 'Bad Gateway', |
|
54 | + 'http_503' => 'Service Unavailable', |
|
55 | + 'http_504' => 'Gateway Timeout', |
|
56 | + 'http_505' => 'HTTP Version Not Supported', |
|
57 | + 'http_506' => 'Variant Also Negotiates', |
|
58 | + 'http_507' => 'Insufficient Storage', |
|
59 | + 'http_508' => 'Loop Detected', |
|
60 | + 'http_509' => 'Unassigned', |
|
61 | + 'http_510' => 'Not Extended', |
|
62 | + 'http_511' => 'Network Authentication Required', |
|
63 | 63 | ]; |
64 | 64 |
@@ -34,20 +34,20 @@ |
||
34 | 34 | $array = $original; |
35 | 35 | foreach ($merging as $m_key => $m_val) { |
36 | 36 | if (array_key_exists($m_key, $original)) { |
37 | - $orig_type = gettype($original[ $m_key ]); |
|
37 | + $orig_type = gettype($original[$m_key]); |
|
38 | 38 | $m_type = gettype($m_val); |
39 | 39 | if ($orig_type !== $m_type) { |
40 | 40 | throw new \RuntimeException( |
41 | 41 | "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
42 | 42 | } |
43 | 43 | |
44 | - if (is_array($merging[ $m_key ])) { |
|
45 | - $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
44 | + if (is_array($merging[$m_key])) { |
|
45 | + $array[$m_key] = static::mergeConfig($original[$m_key], $m_val); |
|
46 | 46 | } else { |
47 | - $array[ $m_key ] = $m_val; |
|
47 | + $array[$m_key] = $m_val; |
|
48 | 48 | } |
49 | 49 | } else { |
50 | - $array[ $m_key ] = $m_val; |
|
50 | + $array[$m_key] = $m_val; |
|
51 | 51 | } |
52 | 52 | } |
53 | 53 |
@@ -15,59 +15,59 @@ |
||
15 | 15 | */ |
16 | 16 | final class Util |
17 | 17 | { |
18 | - /** |
|
19 | - * Merges the configs together and takes multi-dimensional arrays into account. |
|
20 | - * Support for multi-dimensional config array. Built-in config merge only supports flat arrays. |
|
21 | - * Throws \RuntimeException if arrays stucture causes type conflics (i.e. you want to merge |
|
22 | - * array with int). |
|
23 | - * |
|
24 | - * @param array $original Array to merge other array into. Usually default values to overwrite. |
|
25 | - * @param array $merging Array with items to be merged into $original, overriding (primitives) or merging |
|
26 | - * (arrays) entries in destination array. |
|
27 | - * |
|
28 | - * @return array |
|
29 | - * |
|
30 | - * @throws \RuntimeException |
|
31 | - */ |
|
32 | - public static function mergeConfig(array $original, array $merging): array |
|
33 | - { |
|
34 | - $array = $original; |
|
35 | - foreach ($merging as $m_key => $m_val) { |
|
36 | - if (array_key_exists($m_key, $original)) { |
|
37 | - $orig_type = gettype($original[ $m_key ]); |
|
38 | - $m_type = gettype($m_val); |
|
39 | - if ($orig_type !== $m_type) { |
|
40 | - throw new \RuntimeException( |
|
41 | - "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
|
42 | - } |
|
18 | + /** |
|
19 | + * Merges the configs together and takes multi-dimensional arrays into account. |
|
20 | + * Support for multi-dimensional config array. Built-in config merge only supports flat arrays. |
|
21 | + * Throws \RuntimeException if arrays stucture causes type conflics (i.e. you want to merge |
|
22 | + * array with int). |
|
23 | + * |
|
24 | + * @param array $original Array to merge other array into. Usually default values to overwrite. |
|
25 | + * @param array $merging Array with items to be merged into $original, overriding (primitives) or merging |
|
26 | + * (arrays) entries in destination array. |
|
27 | + * |
|
28 | + * @return array |
|
29 | + * |
|
30 | + * @throws \RuntimeException |
|
31 | + */ |
|
32 | + public static function mergeConfig(array $original, array $merging): array |
|
33 | + { |
|
34 | + $array = $original; |
|
35 | + foreach ($merging as $m_key => $m_val) { |
|
36 | + if (array_key_exists($m_key, $original)) { |
|
37 | + $orig_type = gettype($original[ $m_key ]); |
|
38 | + $m_type = gettype($m_val); |
|
39 | + if ($orig_type !== $m_type) { |
|
40 | + throw new \RuntimeException( |
|
41 | + "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
|
42 | + } |
|
43 | 43 | |
44 | - if (is_array($merging[ $m_key ])) { |
|
45 | - $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
46 | - } else { |
|
47 | - $array[ $m_key ] = $m_val; |
|
48 | - } |
|
49 | - } else { |
|
50 | - $array[ $m_key ] = $m_val; |
|
51 | - } |
|
52 | - } |
|
44 | + if (is_array($merging[ $m_key ])) { |
|
45 | + $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
46 | + } else { |
|
47 | + $array[ $m_key ] = $m_val; |
|
48 | + } |
|
49 | + } else { |
|
50 | + $array[ $m_key ] = $m_val; |
|
51 | + } |
|
52 | + } |
|
53 | 53 | |
54 | - return $array; |
|
55 | - } |
|
54 | + return $array; |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Sorts array by value, assuming value is an array and contains `pri` key with integer (positive/negative) |
|
59 | - * value which is used for sorting higher -> lower priority. |
|
60 | - * |
|
61 | - * @param array &$array |
|
62 | - */ |
|
63 | - public static function sortArrayByPri(array &$array): void |
|
64 | - { |
|
65 | - // we now need to sort 'converter' node by priority |
|
66 | - uasort($array, function($array_a, $array_b) { |
|
67 | - $pri_a = $array_a['pri'] ?? 0; |
|
68 | - $pri_b = $array_b['pri'] ?? 0; |
|
57 | + /** |
|
58 | + * Sorts array by value, assuming value is an array and contains `pri` key with integer (positive/negative) |
|
59 | + * value which is used for sorting higher -> lower priority. |
|
60 | + * |
|
61 | + * @param array &$array |
|
62 | + */ |
|
63 | + public static function sortArrayByPri(array &$array): void |
|
64 | + { |
|
65 | + // we now need to sort 'converter' node by priority |
|
66 | + uasort($array, function($array_a, $array_b) { |
|
67 | + $pri_a = $array_a['pri'] ?? 0; |
|
68 | + $pri_b = $array_b['pri'] ?? 0; |
|
69 | 69 | |
70 | - return $pri_b <=> $pri_a; |
|
71 | - }); |
|
72 | - } |
|
70 | + return $pri_b <=> $pri_a; |
|
71 | + }); |
|
72 | + } |
|
73 | 73 | } |
@@ -10,56 +10,56 @@ |
||
10 | 10 | */ |
11 | 11 | return [ |
12 | 12 | |
13 | - 'ok' => 'موفق', |
|
14 | - 'no_error_message' => 'خطای شماره :api_code', |
|
13 | + 'ok' => 'موفق', |
|
14 | + 'no_error_message' => 'خطای شماره :api_code', |
|
15 | 15 | |
16 | - // can be used by Exception Handler (if enabled) |
|
17 | - 'uncaught_exception' => 'استثناء مدیریت نشده :message', |
|
18 | - // we talking API call here, hence we have http_not_found |
|
19 | - 'http_exception' => 'استثناء HTTP :message', |
|
16 | + // can be used by Exception Handler (if enabled) |
|
17 | + 'uncaught_exception' => 'استثناء مدیریت نشده :message', |
|
18 | + // we talking API call here, hence we have http_not_found |
|
19 | + 'http_exception' => 'استثناء HTTP :message', |
|
20 | 20 | |
21 | - // HttpException handler (added in 6.4.0) |
|
22 | - // Error messages for HttpException caught w/o custom messages |
|
23 | - // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
24 | - 'http_400' => 'Bad Request', |
|
25 | - 'http_401' => 'اجازه دسترسی ندارید', |
|
26 | - 'http_402' => 'Payment Required', |
|
27 | - 'http_403' => 'Forbidden', |
|
28 | - 'http_404' => 'Not Found', |
|
29 | - 'http_405' => 'Method Not Allowed', |
|
30 | - 'http_406' => 'Not Acceptable', |
|
31 | - 'http_407' => 'Proxy Authentication Required', |
|
32 | - 'http_408' => 'Request Timeout', |
|
33 | - 'http_409' => 'Conflict', |
|
34 | - 'http_410' => 'Gone', |
|
35 | - 'http_411' => 'Length Required', |
|
36 | - 'http_412' => 'Precondition Failed', |
|
37 | - 'http_413' => 'Payload Too Large', |
|
38 | - 'http_414' => 'URI Too Long', |
|
39 | - 'http_415' => 'Unsupported Media Type', |
|
40 | - 'http_416' => 'Range Not Satisfiable', |
|
41 | - 'http_417' => 'Expectation Failed', |
|
42 | - 'http_421' => 'Misdirected Request', |
|
43 | - 'http_422' => 'داده معتبر نیست', |
|
44 | - 'http_423' => 'Locked', |
|
45 | - 'http_424' => 'Failed Dependency', |
|
46 | - 'http_425' => 'Too Early', |
|
47 | - 'http_426' => 'Upgrade Required', |
|
48 | - 'http_428' => 'Precondition Required', |
|
49 | - 'http_429' => 'Too Many Requests', |
|
50 | - 'http_431' => 'Request Header Fields Too Large', |
|
51 | - 'http_451' => 'Unavailable For Legal Reasons', |
|
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', |
|
21 | + // HttpException handler (added in 6.4.0) |
|
22 | + // Error messages for HttpException caught w/o custom messages |
|
23 | + // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
|
24 | + 'http_400' => 'Bad Request', |
|
25 | + 'http_401' => 'اجازه دسترسی ندارید', |
|
26 | + 'http_402' => 'Payment Required', |
|
27 | + 'http_403' => 'Forbidden', |
|
28 | + 'http_404' => 'Not Found', |
|
29 | + 'http_405' => 'Method Not Allowed', |
|
30 | + 'http_406' => 'Not Acceptable', |
|
31 | + 'http_407' => 'Proxy Authentication Required', |
|
32 | + 'http_408' => 'Request Timeout', |
|
33 | + 'http_409' => 'Conflict', |
|
34 | + 'http_410' => 'Gone', |
|
35 | + 'http_411' => 'Length Required', |
|
36 | + 'http_412' => 'Precondition Failed', |
|
37 | + 'http_413' => 'Payload Too Large', |
|
38 | + 'http_414' => 'URI Too Long', |
|
39 | + 'http_415' => 'Unsupported Media Type', |
|
40 | + 'http_416' => 'Range Not Satisfiable', |
|
41 | + 'http_417' => 'Expectation Failed', |
|
42 | + 'http_421' => 'Misdirected Request', |
|
43 | + 'http_422' => 'داده معتبر نیست', |
|
44 | + 'http_423' => 'Locked', |
|
45 | + 'http_424' => 'Failed Dependency', |
|
46 | + 'http_425' => 'Too Early', |
|
47 | + 'http_426' => 'Upgrade Required', |
|
48 | + 'http_428' => 'Precondition Required', |
|
49 | + 'http_429' => 'Too Many Requests', |
|
50 | + 'http_431' => 'Request Header Fields Too Large', |
|
51 | + 'http_451' => 'Unavailable For Legal Reasons', |
|
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 |
@@ -25,56 +25,56 @@ |
||
25 | 25 | |
26 | 26 | class ResponseBuilderServiceProvider extends ServiceProvider |
27 | 27 | { |
28 | - protected $config_files = [ |
|
29 | - 'response_builder.php', |
|
30 | - ]; |
|
28 | + protected $config_files = [ |
|
29 | + 'response_builder.php', |
|
30 | + ]; |
|
31 | 31 | |
32 | - /** |
|
33 | - * Register bindings in the container. |
|
34 | - * |
|
35 | - * @return void |
|
36 | - */ |
|
37 | - public function register() |
|
38 | - { |
|
39 | - foreach ($this->config_files as $file) { |
|
40 | - $this->mergeConfigFrom(__DIR__ . "/../config/{$file}", ResponseBuilder::CONF_CONFIG); |
|
41 | - } |
|
42 | - } |
|
32 | + /** |
|
33 | + * Register bindings in the container. |
|
34 | + * |
|
35 | + * @return void |
|
36 | + */ |
|
37 | + public function register() |
|
38 | + { |
|
39 | + foreach ($this->config_files as $file) { |
|
40 | + $this->mergeConfigFrom(__DIR__ . "/../config/{$file}", ResponseBuilder::CONF_CONFIG); |
|
41 | + } |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Sets up package resources |
|
46 | - * |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - public function boot() |
|
50 | - { |
|
51 | - $this->loadTranslationsFrom(__DIR__ . '/lang', 'response-builder'); |
|
44 | + /** |
|
45 | + * Sets up package resources |
|
46 | + * |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + public function boot() |
|
50 | + { |
|
51 | + $this->loadTranslationsFrom(__DIR__ . '/lang', 'response-builder'); |
|
52 | 52 | |
53 | - foreach ($this->config_files as $file) { |
|
54 | - $this->publishes([__DIR__ . "/../config/{$file}" => config_path($file)]); |
|
55 | - } |
|
56 | - } |
|
53 | + foreach ($this->config_files as $file) { |
|
54 | + $this->publishes([__DIR__ . "/../config/{$file}" => config_path($file)]); |
|
55 | + } |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Merge the given configuration with the existing configuration. |
|
60 | - * |
|
61 | - * @param string $path |
|
62 | - * @param string $key |
|
63 | - * |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - protected function mergeConfigFrom($path, $key) |
|
67 | - { |
|
68 | - $defaults = require $path; |
|
69 | - $config = $this->app['config']->get($key, []); |
|
58 | + /** |
|
59 | + * Merge the given configuration with the existing configuration. |
|
60 | + * |
|
61 | + * @param string $path |
|
62 | + * @param string $key |
|
63 | + * |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + protected function mergeConfigFrom($path, $key) |
|
67 | + { |
|
68 | + $defaults = require $path; |
|
69 | + $config = $this->app['config']->get($key, []); |
|
70 | 70 | |
71 | - $merged_config = Util::mergeConfig($defaults, $config); |
|
71 | + $merged_config = Util::mergeConfig($defaults, $config); |
|
72 | 72 | |
73 | - if (array_key_exists('converter', $merged_config)) { |
|
74 | - Util::sortArrayByPri($merged_config['converter']); |
|
75 | - } |
|
73 | + if (array_key_exists('converter', $merged_config)) { |
|
74 | + Util::sortArrayByPri($merged_config['converter']); |
|
75 | + } |
|
76 | 76 | |
77 | - $this->app['config']->set($key, $merged_config); |
|
78 | - } |
|
77 | + $this->app['config']->set($key, $merged_config); |
|
78 | + } |
|
79 | 79 | |
80 | 80 | } |
@@ -15,183 +15,183 @@ |
||
15 | 15 | */ |
16 | 16 | class Validator |
17 | 17 | { |
18 | - /** @var string */ |
|
19 | - public const TYPE_STRING = 'string'; |
|
20 | - |
|
21 | - /** @var string */ |
|
22 | - public const TYPE_INTEGER = 'integer'; |
|
23 | - |
|
24 | - /** @var string */ |
|
25 | - public const TYPE_BOOL = 'boolean'; |
|
26 | - |
|
27 | - /** @var string */ |
|
28 | - public const TYPE_ARRAY = 'array'; |
|
29 | - |
|
30 | - /** @var string */ |
|
31 | - public const TYPE_OBJECT = 'object'; |
|
32 | - |
|
33 | - /** @var string */ |
|
34 | - public const TYPE_NULL = 'NULL'; |
|
35 | - |
|
36 | - /** |
|
37 | - * Checks if given $val is of type boolean |
|
38 | - * |
|
39 | - * @param string $key Name of the key to be used if exception is thrown. |
|
40 | - * @param mixed $var Variable to be asserted. |
|
41 | - * |
|
42 | - * @return void |
|
43 | - * |
|
44 | - * @throws \InvalidArgumentException |
|
45 | - */ |
|
46 | - public static function assertIsBool(string $key, $var): void |
|
47 | - { |
|
48 | - self::assertIsType($key, $var, [self::TYPE_BOOL]); |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * Checks if given $val is of type integer |
|
53 | - * |
|
54 | - * @param string $key Name of the key to be used if exception is thrown. |
|
55 | - * @param mixed $var Variable to be asserted. |
|
56 | - * |
|
57 | - * @return void |
|
58 | - * |
|
59 | - * @throws \InvalidArgumentException |
|
60 | - */ |
|
61 | - public static function assertIsInt(string $key, $var): void |
|
62 | - { |
|
63 | - self::assertIsType($key, $var, [self::TYPE_INTEGER]); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Checks if given $val is of type array |
|
68 | - * |
|
69 | - * @param string $key Name of the key to be used if exception is thrown. |
|
70 | - * @param mixed $var Variable to be asserted. |
|
71 | - * |
|
72 | - * @return void |
|
73 | - * |
|
74 | - * @throws \InvalidArgumentException |
|
75 | - */ |
|
76 | - public static function assertIsArray(string $key, $var): void |
|
77 | - { |
|
78 | - self::assertIsType($key, $var, [self::TYPE_ARRAY]); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Checks if given $val is an object |
|
83 | - * |
|
84 | - * @param string $key Name of the key to be used if exception is thrown. |
|
85 | - * @param mixed $var Variable to be asserted. |
|
86 | - * |
|
87 | - * @return void |
|
88 | - * |
|
89 | - * @throws \InvalidArgumentException |
|
90 | - */ |
|
91 | - public static function assertIsObject(string $key, $var): void |
|
92 | - { |
|
93 | - self::assertIsType($key, $var, [self::TYPE_OBJECT]); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Checks if given $val is of type string |
|
98 | - * |
|
99 | - * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
100 | - * @param mixed $var Variable to be asserted. |
|
101 | - * |
|
102 | - * @return void |
|
103 | - * |
|
104 | - * @throws \InvalidArgumentException |
|
105 | - */ |
|
106 | - public static function assertIsString(string $name, $var): void |
|
107 | - { |
|
108 | - self::assertIsType($name, $var, [self::TYPE_STRING]); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
113 | - * @param mixed $var Variable to be asserted. |
|
114 | - * @param int $min Min allowed value (inclusive) |
|
115 | - * @param int $max Max allowed value (inclusive) |
|
116 | - * |
|
117 | - * @return void |
|
118 | - * |
|
119 | - * @throws \InvalidArgumentException |
|
120 | - * @throws \RuntimeException |
|
121 | - */ |
|
122 | - public static function assertIsIntRange(string $name, $var, int $min, int $max): void |
|
123 | - { |
|
124 | - self::assertIsInt($name, $var); |
|
125 | - |
|
126 | - if ($min > $max) { |
|
127 | - throw new \RuntimeException( |
|
128 | - sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $name)); |
|
129 | - } |
|
130 | - |
|
131 | - if (($min > $var) || ($var > $max)) { |
|
132 | - throw new \InvalidArgumentException( |
|
133 | - sprintf('Invalid value of "%s" (%d). Must be between %d-%d inclusive.', $name, $var, $min, $max)); |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Checks if $item (of name $key) is of type that is include in $allowed_types. |
|
139 | - * |
|
140 | - * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
141 | - * @param mixed $var Variable to be asserted. |
|
142 | - * @param array $allowed_types Array of allowed types for $var, i.e. [Validator::TYPE_INTEGER] |
|
143 | - * |
|
144 | - * @return void |
|
145 | - * |
|
146 | - * @throws \InvalidArgumentException |
|
147 | - */ |
|
148 | - public static function assertIsType(string $name, $var, array $allowed_types): void |
|
149 | - { |
|
150 | - $type = gettype($var); |
|
151 | - if (!in_array($type, $allowed_types)) { |
|
152 | - throw new \InvalidArgumentException( |
|
153 | - sprintf('"%s" must be one of allowed types: %s (%s given)', |
|
154 | - $name, implode(', ', $allowed_types), gettype($var)) |
|
155 | - ); |
|
156 | - } |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Ensures given $http_code is valid code for error response. |
|
161 | - * |
|
162 | - * @param int $http_code |
|
163 | - */ |
|
164 | - public static function assertErrorHttpCode(int $http_code): void |
|
165 | - { |
|
166 | - self::assertIsInt('http_code', $http_code); |
|
167 | - self::assertIsIntRange('http_code', $http_code, |
|
168 | - ResponseBuilder::ERROR_HTTP_CODE_MIN, ResponseBuilder::ERROR_HTTP_CODE_MAX); |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Ensures given $http_code is valid for response indicating sucessful operation. |
|
173 | - * |
|
174 | - * @param int $http_code |
|
175 | - */ |
|
176 | - public static function assertOkHttpCode(int $http_code): void |
|
177 | - { |
|
178 | - self::assertIsInt('http_code', $http_code); |
|
179 | - self::assertIsIntRange('http_code', $http_code, 200, 299); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Ensures $obj is instance of $cls. |
|
184 | - * |
|
185 | - * @param string $name |
|
186 | - * @param object $obj |
|
187 | - * @param string $cls |
|
188 | - */ |
|
189 | - public static function assertInstanceOf(string $name, object $obj, string $cls): void |
|
190 | - { |
|
191 | - if (!($obj instanceof $cls)) { |
|
192 | - throw new \InvalidArgumentException( |
|
193 | - sprintf('"%s" must be instance of "%s".', $name, $cls) |
|
194 | - ); |
|
195 | - } |
|
196 | - } |
|
18 | + /** @var string */ |
|
19 | + public const TYPE_STRING = 'string'; |
|
20 | + |
|
21 | + /** @var string */ |
|
22 | + public const TYPE_INTEGER = 'integer'; |
|
23 | + |
|
24 | + /** @var string */ |
|
25 | + public const TYPE_BOOL = 'boolean'; |
|
26 | + |
|
27 | + /** @var string */ |
|
28 | + public const TYPE_ARRAY = 'array'; |
|
29 | + |
|
30 | + /** @var string */ |
|
31 | + public const TYPE_OBJECT = 'object'; |
|
32 | + |
|
33 | + /** @var string */ |
|
34 | + public const TYPE_NULL = 'NULL'; |
|
35 | + |
|
36 | + /** |
|
37 | + * Checks if given $val is of type boolean |
|
38 | + * |
|
39 | + * @param string $key Name of the key to be used if exception is thrown. |
|
40 | + * @param mixed $var Variable to be asserted. |
|
41 | + * |
|
42 | + * @return void |
|
43 | + * |
|
44 | + * @throws \InvalidArgumentException |
|
45 | + */ |
|
46 | + public static function assertIsBool(string $key, $var): void |
|
47 | + { |
|
48 | + self::assertIsType($key, $var, [self::TYPE_BOOL]); |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * Checks if given $val is of type integer |
|
53 | + * |
|
54 | + * @param string $key Name of the key to be used if exception is thrown. |
|
55 | + * @param mixed $var Variable to be asserted. |
|
56 | + * |
|
57 | + * @return void |
|
58 | + * |
|
59 | + * @throws \InvalidArgumentException |
|
60 | + */ |
|
61 | + public static function assertIsInt(string $key, $var): void |
|
62 | + { |
|
63 | + self::assertIsType($key, $var, [self::TYPE_INTEGER]); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Checks if given $val is of type array |
|
68 | + * |
|
69 | + * @param string $key Name of the key to be used if exception is thrown. |
|
70 | + * @param mixed $var Variable to be asserted. |
|
71 | + * |
|
72 | + * @return void |
|
73 | + * |
|
74 | + * @throws \InvalidArgumentException |
|
75 | + */ |
|
76 | + public static function assertIsArray(string $key, $var): void |
|
77 | + { |
|
78 | + self::assertIsType($key, $var, [self::TYPE_ARRAY]); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Checks if given $val is an object |
|
83 | + * |
|
84 | + * @param string $key Name of the key to be used if exception is thrown. |
|
85 | + * @param mixed $var Variable to be asserted. |
|
86 | + * |
|
87 | + * @return void |
|
88 | + * |
|
89 | + * @throws \InvalidArgumentException |
|
90 | + */ |
|
91 | + public static function assertIsObject(string $key, $var): void |
|
92 | + { |
|
93 | + self::assertIsType($key, $var, [self::TYPE_OBJECT]); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Checks if given $val is of type string |
|
98 | + * |
|
99 | + * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
100 | + * @param mixed $var Variable to be asserted. |
|
101 | + * |
|
102 | + * @return void |
|
103 | + * |
|
104 | + * @throws \InvalidArgumentException |
|
105 | + */ |
|
106 | + public static function assertIsString(string $name, $var): void |
|
107 | + { |
|
108 | + self::assertIsType($name, $var, [self::TYPE_STRING]); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
113 | + * @param mixed $var Variable to be asserted. |
|
114 | + * @param int $min Min allowed value (inclusive) |
|
115 | + * @param int $max Max allowed value (inclusive) |
|
116 | + * |
|
117 | + * @return void |
|
118 | + * |
|
119 | + * @throws \InvalidArgumentException |
|
120 | + * @throws \RuntimeException |
|
121 | + */ |
|
122 | + public static function assertIsIntRange(string $name, $var, int $min, int $max): void |
|
123 | + { |
|
124 | + self::assertIsInt($name, $var); |
|
125 | + |
|
126 | + if ($min > $max) { |
|
127 | + throw new \RuntimeException( |
|
128 | + sprintf('%s: Invalid range for "%s". Ensure bound values are not swapped.', __FUNCTION__, $name)); |
|
129 | + } |
|
130 | + |
|
131 | + if (($min > $var) || ($var > $max)) { |
|
132 | + throw new \InvalidArgumentException( |
|
133 | + sprintf('Invalid value of "%s" (%d). Must be between %d-%d inclusive.', $name, $var, $min, $max)); |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Checks if $item (of name $key) is of type that is include in $allowed_types. |
|
139 | + * |
|
140 | + * @param string $name Label or name of the variable to be used in exception message (if thrown). |
|
141 | + * @param mixed $var Variable to be asserted. |
|
142 | + * @param array $allowed_types Array of allowed types for $var, i.e. [Validator::TYPE_INTEGER] |
|
143 | + * |
|
144 | + * @return void |
|
145 | + * |
|
146 | + * @throws \InvalidArgumentException |
|
147 | + */ |
|
148 | + public static function assertIsType(string $name, $var, array $allowed_types): void |
|
149 | + { |
|
150 | + $type = gettype($var); |
|
151 | + if (!in_array($type, $allowed_types)) { |
|
152 | + throw new \InvalidArgumentException( |
|
153 | + sprintf('"%s" must be one of allowed types: %s (%s given)', |
|
154 | + $name, implode(', ', $allowed_types), gettype($var)) |
|
155 | + ); |
|
156 | + } |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Ensures given $http_code is valid code for error response. |
|
161 | + * |
|
162 | + * @param int $http_code |
|
163 | + */ |
|
164 | + public static function assertErrorHttpCode(int $http_code): void |
|
165 | + { |
|
166 | + self::assertIsInt('http_code', $http_code); |
|
167 | + self::assertIsIntRange('http_code', $http_code, |
|
168 | + ResponseBuilder::ERROR_HTTP_CODE_MIN, ResponseBuilder::ERROR_HTTP_CODE_MAX); |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Ensures given $http_code is valid for response indicating sucessful operation. |
|
173 | + * |
|
174 | + * @param int $http_code |
|
175 | + */ |
|
176 | + public static function assertOkHttpCode(int $http_code): void |
|
177 | + { |
|
178 | + self::assertIsInt('http_code', $http_code); |
|
179 | + self::assertIsIntRange('http_code', $http_code, 200, 299); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Ensures $obj is instance of $cls. |
|
184 | + * |
|
185 | + * @param string $name |
|
186 | + * @param object $obj |
|
187 | + * @param string $cls |
|
188 | + */ |
|
189 | + public static function assertInstanceOf(string $name, object $obj, string $cls): void |
|
190 | + { |
|
191 | + if (!($obj instanceof $cls)) { |
|
192 | + throw new \InvalidArgumentException( |
|
193 | + sprintf('"%s" must be instance of "%s".', $name, $cls) |
|
194 | + ); |
|
195 | + } |
|
196 | + } |
|
197 | 197 | } |
@@ -15,14 +15,14 @@ |
||
15 | 15 | */ |
16 | 16 | interface ConverterContract |
17 | 17 | { |
18 | - /** |
|
19 | - * Returns array representation of the object. |
|
20 | - * |
|
21 | - * @param object $obj Object to be converted |
|
22 | - * @param array $config Converter config array to be used for this object (based on exact class |
|
23 | - * name match or inheritance). |
|
24 | - * |
|
25 | - * @return array |
|
26 | - */ |
|
27 | - public function convert($obj, array $config): array; |
|
18 | + /** |
|
19 | + * Returns array representation of the object. |
|
20 | + * |
|
21 | + * @param object $obj Object to be converted |
|
22 | + * @param array $config Converter config array to be used for this object (based on exact class |
|
23 | + * name match or inheritance). |
|
24 | + * |
|
25 | + * @return array |
|
26 | + */ |
|
27 | + public function convert($obj, array $config): array; |
|
28 | 28 | } |
@@ -19,19 +19,19 @@ |
||
19 | 19 | |
20 | 20 | class ToArrayConverter implements ConverterContract |
21 | 21 | { |
22 | - /** |
|
23 | - * Returns array representation of the object. |
|
24 | - * |
|
25 | - * @param object $obj Object to be converted |
|
26 | - * @param array $config Converter config array to be used for this object (based on exact class |
|
27 | - * name match or inheritance). |
|
28 | - * |
|
29 | - * @return array |
|
30 | - */ |
|
31 | - public function convert($obj, array /** @scrutinizer ignore-unused */ $config): array |
|
32 | - { |
|
33 | - Validator::assertIsObject('obj', $obj); |
|
22 | + /** |
|
23 | + * Returns array representation of the object. |
|
24 | + * |
|
25 | + * @param object $obj Object to be converted |
|
26 | + * @param array $config Converter config array to be used for this object (based on exact class |
|
27 | + * name match or inheritance). |
|
28 | + * |
|
29 | + * @return array |
|
30 | + */ |
|
31 | + public function convert($obj, array /** @scrutinizer ignore-unused */ $config): array |
|
32 | + { |
|
33 | + Validator::assertIsObject('obj', $obj); |
|
34 | 34 | |
35 | - return $obj->toArray(); |
|
36 | - } |
|
35 | + return $obj->toArray(); |
|
36 | + } |
|
37 | 37 | } |