Skip to content

Commit

Permalink
Removed keys from collection
Browse files Browse the repository at this point in the history
  • Loading branch information
TemirkhanN committed Oct 17, 2022
1 parent 0ee0f0d commit b325573
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
12 changes: 3 additions & 9 deletions src/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,21 @@ final class Collection implements CollectionInterface
private array $items;

/**
* @param iterable<string|int, T> $items
* @param iterable<T> $items
*/
public function __construct(iterable $items)
{
$this->items = Internal\Iterating::toArray($items, false);
$this->items = Internal\Iterating::toArray($items);
}

/**
* @return Traversable<int, T>
*/
public function getIterator(): Traversable
{
yield from $this->items;
}

/**
* @return array<int, T>
*/
public function items(): array
{
return Internal\Iterating::toArray($this->getIterator(), false);
return $this->items;
}

public function isEmpty(): bool
Expand Down
12 changes: 4 additions & 8 deletions src/Internal/Iterating.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
class Iterating
{
/**
* @template K of string|int
* @template T
*
* @param iterable<K, T> $iterable
* @return array<K, T>
* @param iterable<T> $iterable
*
* @return array<T>
*/
public static function toArray(iterable $iterable, bool $preserveKeys = true): array
public static function toArray(iterable $iterable): array
{
$result = [];
if (!is_array($iterable)) {
Expand All @@ -27,10 +27,6 @@ public static function toArray(iterable $iterable, bool $preserveKeys = true): a
$result = $iterable;
}

if ($preserveKeys) {
return $result;
}

return array_values($result);
}
}

0 comments on commit b325573

Please sign in to comment.