<?php
class StringList implements ArrayAccess, IteratorAggregate
{
protected $items = [];
public function offsetSet($key, $value)
{
$key ?
$this->items[$key] = $value : array_push($this->items, $value);
return $this;
}
throw new \UnexpectedValueException('Essa é uma lista que aceita somente string');
}
public function offsetGet($key)
{
return $this->items[$key];
}
public function offsetExists($key)
{
return isset($this->items[$key]); }
public function offsetUnset($key)
{
unset($this->items[$key]); }
public function getIterator()
{
return new ArrayIterator($this->items);
}
}
$list = new StringList;
$list[] = 'Wallace';
$list[] = 'Bigown';
$list[] = 'Denner Carvalho';
foreach ($list as $string) {
}
PD9waHAKCgpjbGFzcyBTdHJpbmdMaXN0IGltcGxlbWVudHMgQXJyYXlBY2Nlc3MsIEl0ZXJhdG9yQWdncmVnYXRlCnsKICAgIHByb3RlY3RlZCAkaXRlbXMgPSBbXTsKCiAgICBwdWJsaWMgZnVuY3Rpb24gb2Zmc2V0U2V0KCRrZXksICR2YWx1ZSkKICAgIHsKICAgICAgICBpZiAoaXNfc3RyaW5nKCR2YWx1ZSkpIHsKCiAgICAgICAgICAgICRrZXkgPyAkdGhpcy0+aXRlbXNbJGtleV0gPSAkdmFsdWUgOiBhcnJheV9wdXNoKCR0aGlzLT5pdGVtcywgJHZhbHVlKTsKICAgIAogICAgICAgICAgICByZXR1cm4gJHRoaXM7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIHRocm93IG5ldyBcVW5leHBlY3RlZFZhbHVlRXhjZXB0aW9uKCdFc3NhIMOpIHVtYSBsaXN0YSBxdWUgYWNlaXRhIHNvbWVudGUgc3RyaW5nJyk7CiAgICB9CgogICAgcHVibGljIGZ1bmN0aW9uIG9mZnNldEdldCgka2V5KQogICAgewogICAgICAgIHJldHVybiAkdGhpcy0+aXRlbXNbJGtleV07CiAgICB9CgogICAgcHVibGljIGZ1bmN0aW9uIG9mZnNldEV4aXN0cygka2V5KQogICAgewogICAgICAgIHJldHVybiBpc3NldCgkdGhpcy0+aXRlbXNbJGtleV0pOwogICAgfQoKICAgIHB1Ymx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