@@ -184,7 +184,7 @@ public static function decode(
184
184
/**
185
185
* Converts and signs a PHP array into a JWT string.
186
186
*
187
- * @param array<mixed> $payload PHP array
187
+ * @param array<mixed>|object $payload PHP array or object
188
188
* @param string|resource|OpenSSLAsymmetricKey|OpenSSLCertificate $key The secret key.
189
189
* @param string $alg Supported algorithms are 'ES384','ES256', 'ES256K', 'HS256',
190
190
* 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
@@ -197,7 +197,7 @@ public static function decode(
197
197
* @uses urlsafeB64Encode
198
198
*/
199
199
public static function encode (
200
- array $ payload ,
200
+ array | object $ payload ,
201
201
$ key ,
202
202
string $ alg ,
203
203
string $ keyId = null ,
@@ -379,13 +379,13 @@ public static function jsonDecode(string $input)
379
379
/**
380
380
* Encode a PHP array into a JSON string.
381
381
*
382
- * @param array<mixed> $input A PHP array
382
+ * @param array<mixed>|object $input A PHP array or object
383
383
*
384
- * @return string JSON representation of the PHP array
384
+ * @return string JSON representation of the PHP array or object
385
385
*
386
386
* @throws DomainException Provided object could not be encoded to valid JSON
387
387
*/
388
- public static function jsonEncode (array $ input ): string
388
+ public static function jsonEncode (array | object $ input ): string
389
389
{
390
390
if (PHP_VERSION_ID >= 50400 ) {
391
391
$ json = \json_encode ($ input , \JSON_UNESCAPED_SLASHES );
@@ -399,7 +399,8 @@ public static function jsonEncode(array $input): string
399
399
throw new DomainException ('Null result with non-null input ' );
400
400
}
401
401
if ($ json === false ) {
402
- throw new DomainException ('Provided object could not be encoded to valid JSON ' );
402
+ $ type = is_array ($ input ) ? 'array ' : 'object ' ;
403
+ throw new DomainException ('Provided ' . $ type . ' could not be encoded to valid JSON ' );
403
404
}
404
405
return $ json ;
405
406
}
0 commit comments