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

fix: IterableFieldTrait:current() should be compatible with Iterator:… #3404

Closed
wants to merge 1 commit into from
Closed
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
57 changes: 6 additions & 51 deletions src/Entity/IterableFieldTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,14 @@

namespace Bolt\Entity;

trait IterableFieldTrait
{
private $iteratorCursor = 0;

/** @var array Field */
private $fields = [];

/**
* Makes ListFieldInterface fields |length filter
* return the number of elements in the field
*/
public function count(): int
{
return count($this->getValue());
}

/**
* Makes ListFieldInterface fields .length attribute
* return the number of elements in the field
*/
public function length(): int
if (PHP_MAJOR_VERSION >= 8) {
trait IterableFieldTrait
{
return $this->count();
use IterableFieldTraitPhp8;
}

/**
* @return Field|string
*/
public function current()
} else {
trait IterableFieldTrait
{
return $this->fields[$this->iteratorCursor];
}

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

public function key(): int
{
return $this->iteratorCursor;
}

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

public function rewind(): void
{
// Ensure $this->fields is initialised
$this->fields = $this->getValue();

$this->iteratorCursor = 0;
use IterableFieldTraitPhp7;
}
}
62 changes: 62 additions & 0 deletions src/Entity/IterableFieldTraitPhp7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Bolt\Entity;

trait IterableFieldTraitPhp7
{
private $iteratorCursor = 0;

/** @var array Field */
private $fields = [];

/**
* Makes ListFieldInterface fields |length filter
* return the number of elements in the field
*/
public function count(): int
{
return count($this->getValue());
}

/**
* Makes ListFieldInterface fields .length attribute
* return the number of elements in the field
*/
public function length(): int
{
return $this->count();
}

/**
* @return Field|string
*/
public function current()
{
return $this->fields[$this->iteratorCursor];
}

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

public function key(): int
{
return $this->iteratorCursor;
}

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

public function rewind(): void
{
// Ensure $this->fields is initialised
$this->fields = $this->getValue();

$this->iteratorCursor = 0;
}
}
21 changes: 21 additions & 0 deletions src/Entity/IterableFieldTraitPhp8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Bolt\Entity;

trait IterableFieldTraitPhp8
{
use IterableFieldTraitPhp7 {
IterableFieldTraitPhp7::current as private parentCurrent;
}

/**
* @return Field|string
* @noinspection PhpMixedReturnTypeCanBeReducedInspection
*/
public function current(): mixed
{
return $this->parentCurrent();
}
}