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

Support iterator state pattern #39

Merged
merged 4 commits into from
Dec 20, 2021
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Collections library for php language",
"minimum-stability": "dev",
"license": "MIT",
"version": "1.1.1",
"version": "1.1.2",
"authors": [
{
"name": "Maxim Sokolovsky",
Expand Down
35 changes: 34 additions & 1 deletion src/WS/Utils/Collections/CollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

namespace WS\Utils\Collections;

use Iterator;
use IteratorAggregate;
use RuntimeException;
use WS\Utils\Collections\Exception\UnsupportedException;

class CollectionFactory
{
Expand Down Expand Up @@ -37,7 +40,7 @@ public static function generate(int $times, ?callable $generator = null): Collec
/**
* Generate collection of int numbers between $from and $to. If $to arg is absent $from - is count of numbers
* @param int $from
* @param int $to
* @param int|null $to
* @return Collection
*/
public static function numbers(int $from, ?int $to = null): Collection
Expand Down Expand Up @@ -68,8 +71,21 @@ public static function fromStrict(array $values): Collection
return new ArrayStrictList($values);
}

/**
* @throws UnsupportedException
*/
public static function fromIterable(iterable $iterable): Collection
{
if (self::isStatePatternIterator($iterable)) {
if ($iterable instanceof IteratorAggregate) {
/** @noinspection PhpUnhandledExceptionInspection */
$iterable = $iterable->getIterator();
}
if (!$iterable instanceof Iterator) {
throw new UnsupportedException('Only Iterator interface can be applied to IteratorCollection');
}
return new IteratorCollection($iterable);
}
$list = ArrayList::of();
foreach ($iterable as $item) {
$list->add($item);
Expand All @@ -82,4 +98,21 @@ public static function empty(): Collection
{
return ArrayList::of();
}

private static function isStatePatternIterator(iterable $iterable): bool
{
$i = 2;
$lastItem = null;
foreach ($iterable as $item) {
if ($i === 0) {
break;
}
if (is_object($item) && $item === $lastItem) {
return true;
}
$lastItem = $item;
$i--;
}
return false;
}
}
9 changes: 9 additions & 0 deletions src/WS/Utils/Collections/Exception/UnsupportedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace WS\Utils\Collections\Exception;

use RuntimeException;

class UnsupportedException extends RuntimeException
{
}
135 changes: 135 additions & 0 deletions src/WS/Utils/Collections/IteratorCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace WS\Utils\Collections;

use Iterator;
use WS\Utils\Collections\Exception\UnsupportedException;

/**
* Collection which support Traversable interface
*/
class IteratorCollection implements Collection
{

/**
* @var Iterator
*/
private $iterator;

public function __construct(Iterator $iterator)
{
$this->iterator = $iterator;
}

/**
* @throws UnsupportedException
* @codeCoverageIgnore
*/
public function add($element): bool
{
throw new UnsupportedException();
}

/**
* @throws UnsupportedException
* @codeCoverageIgnore
*/
public function addAll(iterable $elements): bool
{
throw new UnsupportedException();
}

/**
* @throws UnsupportedException
* @codeCoverageIgnore
*/
public function merge(Collection $collection): bool
{
throw new UnsupportedException();
}

/**
* @throws UnsupportedException
* @codeCoverageIgnore
*/
public function clear(): void
{
throw new UnsupportedException();
}

/**
* @throws UnsupportedException
* @codeCoverageIgnore
*/
public function remove($element): bool
{
throw new UnsupportedException();
}

/**
* @throws UnsupportedException
* @codeCoverageIgnore
*/
public function contains($element): bool
{
throw new UnsupportedException();
}

/**
* @throws UnsupportedException
* @codeCoverageIgnore
*/
public function equals(Collection $collection): bool
{
throw new UnsupportedException();
}

public function size(): int
{
$this->iterator->rewind();
$count = 0;
while ($this->iterator->valid()) {
$this->iterator->next();
$count++;
}

return $count;
}

/**
* @codeCoverageIgnore
* @return bool
*/
public function isEmpty(): bool
{
return $this->size() === 0;
}

public function stream(): Stream
{
return new IteratorStream($this);
}

/**
* @codeCoverageIgnore
* @return array
*/
public function toArray(): array
{
throw new UnsupportedException();
}

/**
* @codeCoverageIgnore
* @return Collection
*/
public function copy(): Collection
{
throw new UnsupportedException();
}

public function getIterator()
{
return $this->iterator;
}
}
Loading