Skip to content

Commit

Permalink
allow empty array iterables
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Feb 20, 2025
1 parent 27c12b5 commit 4914eaa
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/IterableParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Chevere\Parameter;

use Chevere\Parameter\Interfaces\ArrayParameterInterface;
use Chevere\Parameter\Interfaces\IterableParameterInterface;
use Chevere\Parameter\Interfaces\ParameterInterface;
use Chevere\Parameter\Interfaces\TypeInterface;
Expand All @@ -34,6 +35,8 @@ final class IterableParameter implements IterableParameterInterface
*/
private ?iterable $default = null;

private bool $isEmptyAllowed = false;

public function __construct(
private ParameterInterface $value,
private ParameterInterface $key,
Expand All @@ -45,14 +48,16 @@ public function __construct(
K: $this->key,
V: $this->value
);
$this->isEmptyAllowed = $this->value instanceof ArrayParameterInterface
&& count($this->value->parameters()->requiredKeys()) === 0;
}

/**
* @phpstan-ignore-next-line
*/
public function __invoke(iterable $value): iterable
{
if (empty($value)) {
if (empty($value) && ! $this->isEmptyAllowed) {
throw new InvalidArgumentException(
(string) message('Argument value provided is empty')
);
Expand Down

0 comments on commit 4914eaa

Please sign in to comment.