Skip to content

Commit

Permalink
Refactor array_is_list()
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinmo committed Nov 12, 2024
1 parent e4764ae commit 0c0d8a8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/SimpleJWT/Util/ASN1/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

namespace SimpleJWT\Util\ASN1;

use \SimpleJWT\Util\Util;
use \InvalidArgumentException;

/**
Expand Down Expand Up @@ -207,7 +208,7 @@ static public function oid(string $value): self {
* @return Value
*/
static public function sequence(array $value): self {
if (!is_array($value)) {
if (!Util::array_is_list($value)) {
throw new InvalidArgumentException('Not a sequence');
}
return new self(static::SEQUENCE, $value);
Expand Down
19 changes: 3 additions & 16 deletions src/SimpleJWT/Util/CBOR/DataItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ static public function simple(int $value): self {
* @return DataItem
*/
static public function list(array $value): self {
if (!self::array_is_list($value)) {
if (!Util::array_is_list($value)) {
throw new InvalidArgumentException('Not an array');
}
return new self(static::LIST_TYPE, $value);
Expand All @@ -245,7 +245,7 @@ static public function list(array $value): self {
* @return DataItem
*/
static public function map(array $value): self {
if (self::array_is_list($value)) {
if (Util::array_is_list($value)) {
throw new InvalidArgumentException('Not an associative array');
}
return new self(static::MAP_TYPE, $value);
Expand All @@ -259,7 +259,7 @@ static public function default($value): self {
if ($value instanceof DataItem) {
return $value;
} elseif (is_array($value)) {
if (self::array_is_list($value)) {
if (Util::array_is_list($value)) {
return static::list($value);
} else {
return static::map($value);
Expand Down Expand Up @@ -423,19 +423,6 @@ protected function prettyPrint(int $indent = 0): string {
public function __toString(): string {
return $this->prettyPrint();
}

/**
* Returns whether an array is a list.
*
* This is a polyfill for PHP 8.1's array_as_list function.
*
* @param array<mixed> $array
* @return bool if the array is a list
*/
public static function array_is_list(array $array): bool {
if (function_exists('array_is_list')) return \array_is_list($array);
return $array === [] || (array_keys($array) === range(0, count($array) - 1));
}
}

?>
13 changes: 13 additions & 0 deletions src/SimpleJWT/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ static function packInt64(int $x): string {
static function random_bytes(int $num_bytes): string {
return random_bytes($num_bytes);
}

/**
* Returns whether an array is a list.
*
* This is a polyfill for PHP 8.1's array_as_list function.
*
* @param array<mixed> $array
* @return bool if the array is a list
*/
public static function array_is_list(array $array): bool {
if (function_exists('array_is_list')) return \array_is_list($array);
return $array === [] || (array_keys($array) === range(0, count($array) - 1));
}
}

?>

0 comments on commit 0c0d8a8

Please sign in to comment.