Skip to content

Commit

Permalink
added the IteratorAggregate interface
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickschur committed Jan 9, 2017
1 parent be0c538 commit c4092a9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
12 changes: 10 additions & 2 deletions src/LanguageDetection/LanguageResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author Patrick Schur <[email protected]>
* @package LanguageDetection
*/
class LanguageResult implements \JsonSerializable
class LanguageResult implements \JsonSerializable, \IteratorAggregate
{
private $result = [];

Expand Down Expand Up @@ -60,11 +60,19 @@ public function blacklist(string ...$blacklist): LanguageResult
/**
* @return array
*/
public function all(): array
public function close(): array
{
return $this->result;
}

/**
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->result);
}

/**
* @param int $offset
* @param int|null $length
Expand Down
25 changes: 19 additions & 6 deletions tests/LanguageResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public function testLimit()

$count = (new \GlobIterator(__DIR__ . '/../etc/[^_]*'))->count();

$sample = $l->detect("Example");
$sample = $l->detect('Example');

for ($i = 0; $i < $count; $i++)
{
$this->assertEquals($i, count($sample->limit(0, $i)->all()));
$this->assertEquals($i, count($sample->limit(0, $i)->close()));
}
}

Expand All @@ -37,7 +37,7 @@ public function testWhitelist(string $expected, string $sample)
{
$l = new Language();

$this->assertArrayHasKey($expected, $l->detect($sample)->whitelist($expected)->all());
$this->assertArrayHasKey($expected, $l->detect($sample)->whitelist($expected)->close());
}

/**
Expand All @@ -49,7 +49,7 @@ public function testBlacklist(string $expected, string $sample)
{
$l = new Language();

$this->assertArrayNotHasKey($expected, $l->detect($sample)->blacklist($expected)->all());
$this->assertArrayNotHasKey($expected, $l->detect($sample)->blacklist($expected)->close());
}

/**
Expand All @@ -68,11 +68,24 @@ public function testJsonSerialize()
{
$l = new Language();

$expected = $l->detect("Example");
$expected = $l->detect('Example');

$serialized = json_encode($expected);

$this->assertEquals($expected->all(), json_decode($serialized, true));
$this->assertEquals($expected->close(), json_decode($serialized, true));
}

public function testArrayIterator()
{
$l = new Language();

$actual = $expected = $l->detect('Example');
$actual = $actual->close();

foreach ($expected as $key => $value)
{
$this->assertEquals($value, $actual[$key]);
}
}

/**
Expand Down

0 comments on commit c4092a9

Please sign in to comment.