Skip to content

Commit

Permalink
Make StrictList compatible with SplDoublyLinkedList
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-meyer committed Jan 22, 2024
1 parent 1eb426d commit f832b58
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
7 changes: 3 additions & 4 deletions src/DataStructures/StrictList.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,20 +279,18 @@ public function unshift(mixed $item): void
/**
* Create a type-sensitive, traversable list of items.
*
* @param iterable<AllowedType> $items Initial set of items
* @param string[] $allowedTypes Allowed types of items (optional)
*
* @throws InvalidArgumentException
*/
public function __construct(iterable $items = [], array $allowedTypes = [])
public function __construct(array $allowedTypes = [])
{
if (array_sum(array_map('is_string', $allowedTypes)) !== count($allowedTypes)) {
throw new InvalidArgumentException(
'Allowed types must be array of strings or empty array.'
);
}
$this->allowedTypes = $allowedTypes;
$this->append(...$items);
}

/**
Expand Down Expand Up @@ -330,9 +328,10 @@ public function __unserialize(array $data): void
{
/** @var string[] $allowedTypes */
$allowedTypes = $data['StrictList::allowedTypes'];
$this->__construct($allowedTypes);
/** @var iterable<AllowedType> $items */
$items = $data['SplDoublyLinkedList::dllist'];
$this->__construct($items, $allowedTypes);
$this->append(...$items);
/** @var int $flags */
$flags = $data['SplDoublyLinkedList::flags'];
$this->setIteratorMode($flags);
Expand Down
5 changes: 2 additions & 3 deletions src/DataStructures/StrictQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ final public function setIteratorMode(int $mode): int
/**
* Create a type-sensitive, traversable queue of items.
*
* @param iterable<AllowedType> $items Initial set of items
* @param string[] $allowedTypes Allowed types of items (optional)
*/
public function __construct(iterable $items = [], array $allowedTypes = [])
public function __construct(array $allowedTypes = [])
{
parent::__construct($items, $allowedTypes);
parent::__construct($allowedTypes);
$this->setIteratorMode(0);
}
}
5 changes: 2 additions & 3 deletions src/DataStructures/StrictStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ final public function setIteratorMode(int $mode): int
/**
* Create a type-sensitive, traversable stack of items.
*
* @param iterable<AllowedType> $items Initial set of items
* @param string[] $allowedTypes Allowed types of items (optional)
*/
public function __construct(iterable $items = [], array $allowedTypes = [])
public function __construct(array $allowedTypes = [])
{
parent::__construct($items, $allowedTypes);
parent::__construct($allowedTypes);
$this->setIteratorMode(2);
}
}

0 comments on commit f832b58

Please sign in to comment.