Skip to content

Commit

Permalink
feat: Allow object as payload
Browse files Browse the repository at this point in the history
Changed JWT::encode and JWT::jsonEncode so that they now accepts an
object as well as an array as their first argument
  • Loading branch information
Oinkling committed Aug 7, 2024
1 parent 500501c commit c6d5305
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public static function decode(
/**
* Converts and signs a PHP array into a JWT string.
*
* @param array<mixed> $payload PHP array
* @param array<mixed>|object $payload PHP array or object
* @param string|resource|OpenSSLAsymmetricKey|OpenSSLCertificate $key The secret key.
* @param string $alg Supported algorithms are 'ES384','ES256', 'ES256K', 'HS256',
* 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
Expand All @@ -197,7 +197,7 @@ public static function decode(
* @uses urlsafeB64Encode
*/
public static function encode(
array $payload,
array|object $payload,
$key,
string $alg,
string $keyId = null,
Expand Down Expand Up @@ -379,13 +379,13 @@ public static function jsonDecode(string $input)
/**
* Encode a PHP array into a JSON string.
*
* @param array<mixed> $input A PHP array
* @param array<mixed>|object $input A PHP array or object
*
* @return string JSON representation of the PHP array
* @return string JSON representation of the PHP array or object
*
* @throws DomainException Provided object could not be encoded to valid JSON
*/
public static function jsonEncode(array $input): string
public static function jsonEncode(array|object $input): string
{
if (PHP_VERSION_ID >= 50400) {
$json = \json_encode($input, \JSON_UNESCAPED_SLASHES);
Expand All @@ -399,7 +399,8 @@ public static function jsonEncode(array $input): string
throw new DomainException('Null result with non-null input');
}
if ($json === false) {
throw new DomainException('Provided object could not be encoded to valid JSON');
$type = is_array($input) ? 'array' : 'object';
throw new DomainException('Provided ' . $type . ' could not be encoded to valid JSON');
}
return $json;
}
Expand Down

0 comments on commit c6d5305

Please sign in to comment.