Completed
Pull Request — master (#153)
by Marcin
13:12 queued 09:17
created
src/Validator.php 1 patch
Indentation   +179 added lines, -179 removed lines patch added patch discarded remove patch
@@ -15,183 +15,183 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/lang/de/builder.php 2 patches
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -10,58 +10,58 @@
 block discarded – undo
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
 
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -44,7 +44,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 ];
Please login to merge, or discard this patch.
src/lang/en/builder.php 1 patch
Indentation   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -10,56 +10,56 @@
 block discarded – undo
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
 
Please login to merge, or discard this patch.
src/lang/pl/builder.php 1 patch
Indentation   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -10,55 +10,55 @@
 block discarded – undo
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
 ];
Please login to merge, or discard this patch.
src/ExceptionHandlers/HttpExceptionHandler.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -42,12 +42,12 @@
 block discarded – undo
42 42
 		$config = \array_replace($default_config, $user_config);
43 43
 
44 44
 		$http_code = $ex->getStatusCode();
45
-		$result = $config[ $http_code ] ?? null;
45
+		$result = $config[$http_code] ?? null;
46 46
 
47 47
 		// If we do not have dedicated entry fort this particular http_code,
48 48
 		// fall back to default value.
49 49
 		if ($result === null) {
50
-			$result = $config[ ResponseBuilder::KEY_DEFAULT ];
50
+			$result = $config[ResponseBuilder::KEY_DEFAULT];
51 51
 		}
52 52
 
53 53
 		// Some defaults to fall back to if not set in user config.
Please login to merge, or discard this patch.
config/response_builder.php 1 patch
Indentation   +78 added lines, -78 removed lines patch added patch discarded remove patch
@@ -12,78 +12,78 @@  discard block
 block discarded – undo
12 12
  */
13 13
 
14 14
 return [
15
-    /*
15
+	/*
16 16
     |-----------------------------------------------------------------------------------------------------------
17 17
     | Code range settings
18 18
     |-----------------------------------------------------------------------------------------------------------
19 19
     */
20
-    'min_code'          => 100,
21
-    'max_code'          => 1024,
20
+	'min_code'          => 100,
21
+	'max_code'          => 1024,
22 22
 
23
-    /*
23
+	/*
24 24
     |-----------------------------------------------------------------------------------------------------------
25 25
     | Error code to message mapping
26 26
     |-----------------------------------------------------------------------------------------------------------
27 27
     |
28 28
     */
29
-    'map'               => [
30
-        // YOUR_API_CODE => '<MESSAGE_KEY>',
31
-    ],
29
+	'map'               => [
30
+		// YOUR_API_CODE => '<MESSAGE_KEY>',
31
+	],
32 32
 
33
-    /*
33
+	/*
34 34
     |-----------------------------------------------------------------------------------------------------------
35 35
     | Response Builder data converter
36 36
     |-----------------------------------------------------------------------------------------------------------
37 37
     |
38 38
     */
39
-    'converter'         => [
40
-        \Illuminate\Database\Eloquent\Model::class          => [
41
-            'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
42
-            // 'key'     => 'item',
43
-            'pri'     => 0,
44
-        ],
45
-        \Illuminate\Support\Collection::class               => [
46
-            'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
47
-            // 'key'     => 'item',
48
-            'pri'     => 0,
49
-        ],
50
-        \Illuminate\Database\Eloquent\Collection::class     => [
51
-            'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
52
-            // 'key'     => 'item',
53
-            'pri'     => 0,
54
-        ],
55
-        \Illuminate\Http\Resources\Json\JsonResource::class => [
56
-            'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
57
-            // 'key'     => 'item',
58
-            'pri'     => 0,
59
-        ],
39
+	'converter'         => [
40
+		\Illuminate\Database\Eloquent\Model::class          => [
41
+			'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
42
+			// 'key'     => 'item',
43
+			'pri'     => 0,
44
+		],
45
+		\Illuminate\Support\Collection::class               => [
46
+			'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
47
+			// 'key'     => 'item',
48
+			'pri'     => 0,
49
+		],
50
+		\Illuminate\Database\Eloquent\Collection::class     => [
51
+			'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
52
+			// 'key'     => 'item',
53
+			'pri'     => 0,
54
+		],
55
+		\Illuminate\Http\Resources\Json\JsonResource::class => [
56
+			'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ToArrayConverter::class,
57
+			// 'key'     => 'item',
58
+			'pri'     => 0,
59
+		],
60 60
 
61
-        /*
61
+		/*
62 62
         |-----------------------------------------------------------------------------------------------------------
63 63
         | Generic converters should have lower pri to allow dedicated ones to kick in first when class matches
64 64
         |-----------------------------------------------------------------------------------------------------------
65 65
         */
66
-        \JsonSerializable::class                            => [
67
-            'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class,
68
-            // 'key'     => 'item',
69
-            'pri'     => -10,
70
-        ],
71
-        \Illuminate\Contracts\Support\Arrayable::class      => [
72
-            'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class,
73
-            // 'key'     => 'item',
74
-            'pri'     => -10,
75
-        ],
66
+		\JsonSerializable::class                            => [
67
+			'handler' => \MarcinOrlowski\ResponseBuilder\Converters\JsonSerializableConverter::class,
68
+			// 'key'     => 'item',
69
+			'pri'     => -10,
70
+		],
71
+		\Illuminate\Contracts\Support\Arrayable::class      => [
72
+			'handler' => \MarcinOrlowski\ResponseBuilder\Converters\ArrayableConverter::class,
73
+			// 'key'     => 'item',
74
+			'pri'     => -10,
75
+		],
76 76
 
77
-    ],
77
+	],
78 78
 
79
-    /*
79
+	/*
80 80
     |-----------------------------------------------------------------------------------------------------------
81 81
     | Exception handler error codes
82 82
     |-----------------------------------------------------------------------------------------------------------
83 83
     |
84 84
     */
85
-    'exception_handler' => [
86
-	    /*
85
+	'exception_handler' => [
86
+		/*
87 87
 	     * The following keys are supported for each handler specified.
88 88
 	     *   `handler`
89 89
 	     *   `pri`
@@ -103,19 +103,19 @@  discard block
 block discarded – undo
103 103
 		 *                  message ($ex->getMessage()).
104 104
 		 */
105 105
 
106
-    	\Illuminate\Validation\ValidationException::class => [
107
-		    'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\ValidationExceptionHandler::class,
108
-		    'pri'     => -100,
109
-		    'config' => [
106
+		\Illuminate\Validation\ValidationException::class => [
107
+			'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\ValidationExceptionHandler::class,
108
+			'pri'     => -100,
109
+			'config' => [
110 110
 //		        'api_code'  => ApiCodes::YOUR_API_CODE_FOR_VALIDATION_EXCEPTION,
111 111
 //		        'http_code' => HttpResponse::HTTP_UNPROCESSABLE_ENTITY,
112
-		    	],
113
-	    ],
112
+				],
113
+		],
114 114
 
115 115
 		\Symfony\Component\HttpKernel\Exception\HttpException::class => [
116
-	        'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class,
117
-	        'pri'     => -100,
118
-	        'config'  => [
116
+			'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class,
117
+			'pri'     => -100,
118
+			'config'  => [
119 119
 //		        HttpException::class => [
120 120
 //			        // used by unauthenticated() to obtain api and http code for the exception
121 121
 //			        HttpResponse::HTTP_UNAUTHORIZED         => [
@@ -131,47 +131,47 @@  discard block
 block discarded – undo
131 131
 //				        'http_code' => HttpResponse::HTTP_BAD_REQUEST,
132 132
 //			        ],
133 133
 //		        ],
134
-	        ],
134
+			],
135 135
 //	        // This is final exception handler. If ex is not dealt with yet this is its last stop.
136
-	        // default handler is mandatory and MUST have both `api_code` and `http_code` set.
136
+			// default handler is mandatory and MUST have both `api_code` and `http_code` set.
137 137
 
138
-	        'default' => [
139
-		        'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class,
140
-		        'pri'     => -127,
141
-		        'config'  => [
138
+			'default' => [
139
+				'handler' => \MarcinOrlowski\ResponseBuilder\ExceptionHandlers\HttpExceptionHandler::class,
140
+				'pri'     => -127,
141
+				'config'  => [
142 142
 //			        'api_code'  => ApiCodes::YOUR_API_CODE_FOR_UNHANDLED_EXCEPTION,
143 143
 //			        'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR,
144
-		        ],
145
-	        ],
146
-	    ],
147
-    ],
144
+				],
145
+			],
146
+		],
147
+	],
148 148
 
149
-    /*
149
+	/*
150 150
     |-----------------------------------------------------------------------------------------------------------
151 151
     | data-to-json encoding options
152 152
     |-----------------------------------------------------------------------------------------------------------
153 153
     |
154 154
     */
155
-    'encoding_options'  => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE,
155
+	'encoding_options'  => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE,
156 156
 
157
-    /*
157
+	/*
158 158
     |-----------------------------------------------------------------------------------------------------------
159 159
     | Debug config
160 160
     |-----------------------------------------------------------------------------------------------------------
161 161
     |
162 162
     */
163
-    'debug'             => [
164
-        'debug_key'         => 'debug',
165
-        'exception_handler' => [
166
-            'trace_key'     => 'trace',
167
-            'trace_enabled' => env('APP_DEBUG', false),
168
-        ],
163
+	'debug'             => [
164
+		'debug_key'         => 'debug',
165
+		'exception_handler' => [
166
+			'trace_key'     => 'trace',
167
+			'trace_enabled' => env('APP_DEBUG', false),
168
+		],
169 169
 
170
-        // Controls debugging features of payload converter class.
171
-        'converter' => [
172
-        	// Set to true to figure out what converter is used for given data payload and why.
173
-        	'debug_enabled' => env('RB_CONVERTER_DEBUG', false),
174
-        ],
175
-    ],
170
+		// Controls debugging features of payload converter class.
171
+		'converter' => [
172
+			// Set to true to figure out what converter is used for given data payload and why.
173
+			'debug_enabled' => env('RB_CONVERTER_DEBUG', false),
174
+		],
175
+	],
176 176
 
177 177
 ];
Please login to merge, or discard this patch.
src/Converter.php 2 patches
Indentation   +182 added lines, -182 removed lines patch added patch discarded remove patch
@@ -23,187 +23,187 @@
 block discarded – undo
23 23
  */
24 24
 class Converter
25 25
 {
26
-    /**
27
-     * @var array
28
-     */
29
-    protected $classes = [];
30
-
31
-    /** @var bool */
32
-    protected $debug_enabled = false;
33
-
34
-    /**
35
-     * Converter constructor.
36
-     *
37
-     * @throws \RuntimeException
38
-     */
39
-    public function __construct()
40
-    {
41
-        $this->classes = static::getClassesMapping() ?? [];
42
-
43
-	    $this->debug_enabled = Config::get(ResponseBuilder::CONF_KEY_CONVERTER_DEBUG_KEY, false);
44
-    }
45
-
46
-    /**
47
-     * Returns local copy of configuration mapping for the classes.
48
-     *
49
-     * @return array
50
-     */
51
-    public function getClasses(): array
52
-    {
53
-        return $this->classes;
54
-    }
55
-
56
-    /**
57
-     * Checks if we have "classes" mapping configured for $data object class.
58
-     * Returns @true if there's valid config for this class.
59
-     * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured.
60
-     * Throws \InvalidArgumentException if No data conversion mapping configured for given class.
61
-     *
62
-     * @param object $data Object to check mapping for.
63
-     *
64
-     * @return array
65
-     *
66
-     * @throws \InvalidArgumentException
67
-     */
68
-    protected function getClassMappingConfigOrThrow(object $data): array
69
-    {
70
-        $result = null;
71
-        $debug_result = '';
72
-
73
-        // check for exact class name match...
74
-        $cls = \get_class($data);
75
-        if (\is_string($cls)) {
76
-	        if (\array_key_exists($cls, $this->classes)) {
77
-		        $result = $this->classes[ $cls ];
78
-		        $debug_result = 'exact config match';
79
-	        } else {
80
-		        // no exact match, then lets try with `instanceof`
81
-		        foreach (\array_keys($this->getClasses()) as $class_name) {
82
-			        if ($data instanceof $class_name) {
83
-				        $result = $this->classes[ $class_name ];
84
-				        $debug_result = "subclass of {$class_name}";
85
-				        break;
86
-			        }
87
-		        }
88
-	        }
89
-        }
90
-
91
-        if ($result === null) {
92
-            throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls));
93
-        }
94
-
95
-        if ($this->debug_enabled) {
26
+	/**
27
+	 * @var array
28
+	 */
29
+	protected $classes = [];
30
+
31
+	/** @var bool */
32
+	protected $debug_enabled = false;
33
+
34
+	/**
35
+	 * Converter constructor.
36
+	 *
37
+	 * @throws \RuntimeException
38
+	 */
39
+	public function __construct()
40
+	{
41
+		$this->classes = static::getClassesMapping() ?? [];
42
+
43
+		$this->debug_enabled = Config::get(ResponseBuilder::CONF_KEY_CONVERTER_DEBUG_KEY, false);
44
+	}
45
+
46
+	/**
47
+	 * Returns local copy of configuration mapping for the classes.
48
+	 *
49
+	 * @return array
50
+	 */
51
+	public function getClasses(): array
52
+	{
53
+		return $this->classes;
54
+	}
55
+
56
+	/**
57
+	 * Checks if we have "classes" mapping configured for $data object class.
58
+	 * Returns @true if there's valid config for this class.
59
+	 * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured.
60
+	 * Throws \InvalidArgumentException if No data conversion mapping configured for given class.
61
+	 *
62
+	 * @param object $data Object to check mapping for.
63
+	 *
64
+	 * @return array
65
+	 *
66
+	 * @throws \InvalidArgumentException
67
+	 */
68
+	protected function getClassMappingConfigOrThrow(object $data): array
69
+	{
70
+		$result = null;
71
+		$debug_result = '';
72
+
73
+		// check for exact class name match...
74
+		$cls = \get_class($data);
75
+		if (\is_string($cls)) {
76
+			if (\array_key_exists($cls, $this->classes)) {
77
+				$result = $this->classes[ $cls ];
78
+				$debug_result = 'exact config match';
79
+			} else {
80
+				// no exact match, then lets try with `instanceof`
81
+				foreach (\array_keys($this->getClasses()) as $class_name) {
82
+					if ($data instanceof $class_name) {
83
+						$result = $this->classes[ $class_name ];
84
+						$debug_result = "subclass of {$class_name}";
85
+						break;
86
+					}
87
+				}
88
+			}
89
+		}
90
+
91
+		if ($result === null) {
92
+			throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls));
93
+		}
94
+
95
+		if ($this->debug_enabled) {
96 96
 			Log::debug(__CLASS__ . ": Converting {$cls} using {$result['handler']} because: {$debug_result}.");
97
-        }
98
-
99
-	    return $result;
100
-    }
101
-
102
-    /**
103
-     * We need to prepare source data
104
-     *
105
-     * @param object|array|null $data
106
-     *
107
-     * @return array|null
108
-     *
109
-     * @throws \InvalidArgumentException
110
-     */
111
-    public function convert($data = null): ?array
112
-    {
113
-        if ($data === null) {
114
-            return null;
115
-        }
116
-
117
-        Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY,
118
-                                                Validator::TYPE_OBJECT]);
119
-
120
-        if (\is_object($data)) {
121
-            $cfg = $this->getClassMappingConfigOrThrow($data);
122
-            $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ]();
123
-            $data = $worker->convert($data, $cfg);
124
-        } else {
125
-            $data = $this->convertArray($data);
126
-        }
127
-
128
-        return $data;
129
-    }
130
-
131
-    /**
132
-     * Recursively walks $data array and converts all known objects if found. Note
133
-     * $data array is passed by reference so source $data array may be modified.
134
-     *
135
-     * @param array $data array to recursively convert known elements of
136
-     *
137
-     * @return array
138
-     *
139
-     * @throws \RuntimeException
140
-     */
141
-    protected function convertArray(array $data): array
142
-    {
143
-        // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then
144
-        // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON
145
-        // array. But you can't mix these two as the final JSON would not produce predictable results.
146
-        $string_keys_cnt = 0;
147
-        $int_keys_cnt = 0;
148
-        foreach ($data as $key => $val) {
149
-            if (\is_int($key)) {
150
-                $int_keys_cnt++;
151
-            } else {
152
-                $string_keys_cnt++;
153
-            }
154
-
155
-            if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) {
156
-                throw new \RuntimeException(
157
-                    'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' .
158
-                    'Arrays with mixed keys are not supported by design.');
159
-            }
160
-        }
161
-
162
-        foreach ($data as $key => $val) {
163
-            if (\is_array($val)) {
164
-                $data[ $key ] = $this->convertArray($val);
165
-            } elseif (\is_object($val)) {
166
-                $cfg = $this->getClassMappingConfigOrThrow($val);
167
-                $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ]();
168
-                $converted_data = $worker->convert($val, $cfg);
169
-                $data[ $key ] = $converted_data;
170
-            }
171
-        }
172
-
173
-        return $data;
174
-    }
175
-
176
-    /**
177
-     * Reads and validates "classes" config mapping
178
-     *
179
-     * @return array Classes mapping as specified in configuration or empty array if configuration found
180
-     *
181
-     * @throws \RuntimeException if "classes" mapping is technically invalid (i.e. not array etc).
182
-     */
183
-    protected static function getClassesMapping(): array
184
-    {
185
-        $classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER);
186
-
187
-        if ($classes !== null) {
188
-            if (!\is_array($classes)) {
189
-                throw new \RuntimeException(
190
-                    \sprintf('CONFIG: "classes" mapping must be an array (%s given)', \gettype($classes)));
191
-            }
192
-
193
-            $mandatory_keys = [
194
-                ResponseBuilder::KEY_HANDLER,
195
-            ];
196
-            foreach ($classes as $class_name => $class_config) {
197
-                foreach ($mandatory_keys as $key_name) {
198
-                    if (!\array_key_exists($key_name, $class_config)) {
199
-                        throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping");
200
-                    }
201
-                }
202
-            }
203
-        } else {
204
-            $classes = [];
205
-        }
206
-
207
-        return $classes;
208
-    }
97
+		}
98
+
99
+		return $result;
100
+	}
101
+
102
+	/**
103
+	 * We need to prepare source data
104
+	 *
105
+	 * @param object|array|null $data
106
+	 *
107
+	 * @return array|null
108
+	 *
109
+	 * @throws \InvalidArgumentException
110
+	 */
111
+	public function convert($data = null): ?array
112
+	{
113
+		if ($data === null) {
114
+			return null;
115
+		}
116
+
117
+		Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY,
118
+												Validator::TYPE_OBJECT]);
119
+
120
+		if (\is_object($data)) {
121
+			$cfg = $this->getClassMappingConfigOrThrow($data);
122
+			$worker = new $cfg[ ResponseBuilder::KEY_HANDLER ]();
123
+			$data = $worker->convert($data, $cfg);
124
+		} else {
125
+			$data = $this->convertArray($data);
126
+		}
127
+
128
+		return $data;
129
+	}
130
+
131
+	/**
132
+	 * Recursively walks $data array and converts all known objects if found. Note
133
+	 * $data array is passed by reference so source $data array may be modified.
134
+	 *
135
+	 * @param array $data array to recursively convert known elements of
136
+	 *
137
+	 * @return array
138
+	 *
139
+	 * @throws \RuntimeException
140
+	 */
141
+	protected function convertArray(array $data): array
142
+	{
143
+		// This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then
144
+		// be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON
145
+		// array. But you can't mix these two as the final JSON would not produce predictable results.
146
+		$string_keys_cnt = 0;
147
+		$int_keys_cnt = 0;
148
+		foreach ($data as $key => $val) {
149
+			if (\is_int($key)) {
150
+				$int_keys_cnt++;
151
+			} else {
152
+				$string_keys_cnt++;
153
+			}
154
+
155
+			if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) {
156
+				throw new \RuntimeException(
157
+					'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' .
158
+					'Arrays with mixed keys are not supported by design.');
159
+			}
160
+		}
161
+
162
+		foreach ($data as $key => $val) {
163
+			if (\is_array($val)) {
164
+				$data[ $key ] = $this->convertArray($val);
165
+			} elseif (\is_object($val)) {
166
+				$cfg = $this->getClassMappingConfigOrThrow($val);
167
+				$worker = new $cfg[ ResponseBuilder::KEY_HANDLER ]();
168
+				$converted_data = $worker->convert($val, $cfg);
169
+				$data[ $key ] = $converted_data;
170
+			}
171
+		}
172
+
173
+		return $data;
174
+	}
175
+
176
+	/**
177
+	 * Reads and validates "classes" config mapping
178
+	 *
179
+	 * @return array Classes mapping as specified in configuration or empty array if configuration found
180
+	 *
181
+	 * @throws \RuntimeException if "classes" mapping is technically invalid (i.e. not array etc).
182
+	 */
183
+	protected static function getClassesMapping(): array
184
+	{
185
+		$classes = Config::get(ResponseBuilder::CONF_KEY_CONVERTER);
186
+
187
+		if ($classes !== null) {
188
+			if (!\is_array($classes)) {
189
+				throw new \RuntimeException(
190
+					\sprintf('CONFIG: "classes" mapping must be an array (%s given)', \gettype($classes)));
191
+			}
192
+
193
+			$mandatory_keys = [
194
+				ResponseBuilder::KEY_HANDLER,
195
+			];
196
+			foreach ($classes as $class_name => $class_config) {
197
+				foreach ($mandatory_keys as $key_name) {
198
+					if (!\array_key_exists($key_name, $class_config)) {
199
+						throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping");
200
+					}
201
+				}
202
+			}
203
+		} else {
204
+			$classes = [];
205
+		}
206
+
207
+		return $classes;
208
+	}
209 209
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -74,13 +74,13 @@  discard block
 block discarded – undo
74 74
         $cls = \get_class($data);
75 75
         if (\is_string($cls)) {
76 76
 	        if (\array_key_exists($cls, $this->classes)) {
77
-		        $result = $this->classes[ $cls ];
77
+		        $result = $this->classes[$cls];
78 78
 		        $debug_result = 'exact config match';
79 79
 	        } else {
80 80
 		        // no exact match, then lets try with `instanceof`
81 81
 		        foreach (\array_keys($this->getClasses()) as $class_name) {
82 82
 			        if ($data instanceof $class_name) {
83
-				        $result = $this->classes[ $class_name ];
83
+				        $result = $this->classes[$class_name];
84 84
 				        $debug_result = "subclass of {$class_name}";
85 85
 				        break;
86 86
 			        }
@@ -119,7 +119,7 @@  discard block
 block discarded – undo
119 119
 
120 120
         if (\is_object($data)) {
121 121
             $cfg = $this->getClassMappingConfigOrThrow($data);
122
-            $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ]();
122
+            $worker = new $cfg[ResponseBuilder::KEY_HANDLER]();
123 123
             $data = $worker->convert($data, $cfg);
124 124
         } else {
125 125
             $data = $this->convertArray($data);
@@ -161,12 +161,12 @@  discard block
 block discarded – undo
161 161
 
162 162
         foreach ($data as $key => $val) {
163 163
             if (\is_array($val)) {
164
-                $data[ $key ] = $this->convertArray($val);
164
+                $data[$key] = $this->convertArray($val);
165 165
             } elseif (\is_object($val)) {
166 166
                 $cfg = $this->getClassMappingConfigOrThrow($val);
167
-                $worker = new $cfg[ ResponseBuilder::KEY_HANDLER ]();
167
+                $worker = new $cfg[ResponseBuilder::KEY_HANDLER]();
168 168
                 $converted_data = $worker->convert($val, $cfg);
169
-                $data[ $key ] = $converted_data;
169
+                $data[$key] = $converted_data;
170 170
             }
171 171
         }
172 172
 
Please login to merge, or discard this patch.