既然SplFixedArray不是一个Interator而是一个IteratorAggregate,我该如何更改下面的代码以适应PHP8
<?php
declare(strict_types=1);
namespace BitWasp\Bitcoin\Transaction\Mutator;
abstract class AbstractCollectionMutator implements \Iterator, \ArrayAccess, \Countable
{
/**
* @var \SplFixedArray
*/
protected $set;
/**
* @return array
*/
public function all(): array
{
return $this->set->toArray();
}
/**
* @return bool
*/
public function isNull(): bool
{
return count($this->set) === 0;
}
/**
* @return int
*/
public function count(): int
{
return $this->set->count();
}
/**
*
*/
public function rewind()
{
$this->set->rewind();
}
/**
* @return mixed
*/
public function current()
{
return $this->set->current();
}
/**
* @return int
*/
public function key()
{
return $this->set->key();
}
/**
*
*/
public function next()
{
$this->set->next();
}
/**
* @return bool
*/
public function valid()
{
return $this->set->valid();
}
/**
* @param int $offset
* @return bool
*/
public function offsetExists($offset)
{
return $this->set->offsetExists($offset);
}
/**
* @param int $offset
*/
public function offsetUnset($offset)
{
if (!$this->offsetExists($offset)) {
throw new \InvalidArgumentException('Offset does not exist');
}
$this->set->offsetUnset($offset);
}
/**
* @param int $offset
* @return mixed
*/
public function offsetGet($offset)
{
if (!$this->set->offsetExists($offset)) {
throw new \OutOfRangeException('Nothing found at this offset');
}
return $this->set->offsetGet($offset);
}
/**
* @param int $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
$this->set->offsetSet($offset, $value);
}
}
字符串
我试图将implementation\Iterator改为\InteratorAggregate,但它不能正常工作。
1条答案
按热度按时间jtw3ybtb1#
SplFixedArray过去是,现在仍然是 Traversable,但它改变了它的接口:从 Iterator 到 IteratorAggregate,并使用它的协议。
这现在会侵 eclipse 你的抽象超类AbstractCollectionMutator,因为它仍然实现早期的Iterator协议,并且还没有实现IteratorAggregate协议。
这是直接的:
1.删除 Iterator 接口。
1.删除rewind()、valid()、key()、current()和next()。
1.然后实现 IteratorAggregate 接口。
1.添加getIterator()方法。
1.运行测试、提交、推送和完成。
实现与之前相同,您委托给set,好处是您现在可以使用set本身,您不需要在Traversable<IteratorAggregate>中转发协议:
字符串
不需要事件转发,
$this->set->getIterator()
对于任何带有 IteratorAggregate 接口的可遍历性都是隐式的。PHP在内部实现了该协议。