Skip to content

Commit c6d5305

Browse files
author
Oinkling
committed
feat: Allow object as payload
Changed JWT::encode and JWT::jsonEncode so that they now accepts an object as well as an array as their first argument
1 parent 500501c commit c6d5305

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/JWT.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public static function decode(
184184
/**
185185
* Converts and signs a PHP array into a JWT string.
186186
*
187-
* @param array<mixed> $payload PHP array
187+
* @param array<mixed>|object $payload PHP array or object
188188
* @param string|resource|OpenSSLAsymmetricKey|OpenSSLCertificate $key The secret key.
189189
* @param string $alg Supported algorithms are 'ES384','ES256', 'ES256K', 'HS256',
190190
* 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
@@ -197,7 +197,7 @@ public static function decode(
197197
* @uses urlsafeB64Encode
198198
*/
199199
public static function encode(
200-
array $payload,
200+
array|object $payload,
201201
$key,
202202
string $alg,
203203
string $keyId = null,
@@ -379,13 +379,13 @@ public static function jsonDecode(string $input)
379379
/**
380380
* Encode a PHP array into a JSON string.
381381
*
382-
* @param array<mixed> $input A PHP array
382+
* @param array<mixed>|object $input A PHP array or object
383383
*
384-
* @return string JSON representation of the PHP array
384+
* @return string JSON representation of the PHP array or object
385385
*
386386
* @throws DomainException Provided object could not be encoded to valid JSON
387387
*/
388-
public static function jsonEncode(array $input): string
388+
public static function jsonEncode(array|object $input): string
389389
{
390390
if (PHP_VERSION_ID >= 50400) {
391391
$json = \json_encode($input, \JSON_UNESCAPED_SLASHES);
@@ -399,7 +399,8 @@ public static function jsonEncode(array $input): string
399399
throw new DomainException('Null result with non-null input');
400400
}
401401
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');
403404
}
404405
return $json;
405406
}

0 commit comments

Comments
 (0)