Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ private function setAccessToken(array $config): void
}

$this->utilities()->validateString(
$config['access_token'],
"config['access_token']",
32,
!$this->sender->requireAccessToken(),
input: $config['access_token'],
name: "config['access_token']",
len: [32, 96],
allowNull: !$this->sender->requireAccessToken(),
);
$this->accessToken = $config['access_token'] ?? '';
}
Expand Down
37 changes: 30 additions & 7 deletions src/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,25 @@ public static function isWindows()
return php_uname('s') == 'Windows NT';
}

/**
* Validate that the given $input is a string or null if $allowNull is true and that it has as many characters as
* one of the given $len.
*
* @param mixed $input The value to validate.
* @param string $name The name of the variable being validated.
* @param int|int[]|null $len The length(s) to validate against. Can be a single integer or an array of integers.
* @param bool $allowNull Whether to allow null values.
* @return void
*
* @since 1.0.0
* @since 4.1.3 Added support for array of lengths.
*/
public static function validateString(
$input,
$name = "?",
$len = null,
$allowNull = true
) {
mixed $input,
string $name = "?",
int|array $len = null,
bool $allowNull = true
): void {
if (is_null($input)) {
if (!$allowNull) {
throw new \InvalidArgumentException("\$$name must not be null");
Expand All @@ -29,9 +42,19 @@ public static function validateString(
if (!is_string($input)) {
throw new \InvalidArgumentException("\$$name must be a string");
}
if (!is_null($len) && strlen($input) != $len) {
throw new \InvalidArgumentException("\$$name must be $len characters long, was '$input'");
if (null === $len) {
return;
}
if (!is_array($len)) {
$len = [$len];
}
foreach ($len as $l) {
if (strlen($input) == $l) {
return;
}
}
$lens = implode(", ", $len);
throw new \InvalidArgumentException("\$$name must be $lens characters long, was '$input'");
}

public static function validateBoolean(
Expand Down
8 changes: 8 additions & 0 deletions tests/UtilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public function testValidateString(): void
} catch (\InvalidArgumentException $e) {
$this->assertEquals("\$str must be 2 characters long, was '1'", $e->getMessage());
}

try {
Utilities::validateString("foo", "str", [2, 4]);
$this->fail("Above should throw");
} catch (\InvalidArgumentException $e) {
$this->assertEquals("\$str must be 2, 4 characters long, was 'foo'", $e->getMessage());
}
Utilities::validateString("four", "local", [2, 4]);
}

public function testValidateInteger(): void
Expand Down