Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

foreach to an iterator wich returns an array #191

Open
SamueleD78 opened this issue Sep 25, 2021 · 1 comment
Open

foreach to an iterator wich returns an array #191

SamueleD78 opened this issue Sep 25, 2021 · 1 comment
Labels

Comments

@SamueleD78
Copy link

I don't know if it's me, but i have a class which implements iterator, and on the current() override i return an array with the subkeys definitions.

When i use the object inside a foreach, the value elements are not been recognized. If i explicitly call the ->current() method, it is.

class A implements \Iterator
{
	private int $position;
	private array $keys;
	private array $values;

	public function __construct(array $matches)
	{
		$this->position = 0;
		$this->keys = array_keys($matches);
		$this->values = array_values($matches);
	}

	/**
	 * @return array [
	 *  'rule' => new RoutingRule
	 * ]
	 */
	public function current(): array
	{
		return $this->values[$this->position];
	}

	public function next(): void
	{
		++$this->position;
	}

	public function key(): string
	{
		return $this->keys[$this->position];
	}

	public function valid(): bool
	{
		return isset($this->values[$this->position]);
	}

	public function rewind(): void
	{
		$this->position = 0;
	}
}

$test = new A(["a" => new RoutingRule()]);
$test->current()[WORKS];
foreach ($test as $value) {
  $value[DOESNTWORKS]
}

@klesun
Copy link
Owner

klesun commented Sep 25, 2021

Hi. Thanks for the use case. I think the plugin did not have support for the custom \Iterator classes. Would need to implement it. I won't have the time myself anytime soon sadly, the only hope is on an MR ;c

@klesun klesun added the status: help wanted Backlog label Sep 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants