Skip to content

Commit

Permalink
Merge pull request #10 from asm89/foreachable
Browse files Browse the repository at this point in the history
Make it possible to consume the Option value using foreach
  • Loading branch information
schmittjoh committed Jan 9, 2014
2 parents a807c38 + 2c99758 commit 5d099bc
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/PhpOption/LazyOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ public function reject($value)
return $this->option()->reject($value);
}

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

/**
* @return Option
*/
Expand Down
9 changes: 8 additions & 1 deletion src/PhpOption/None.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace PhpOption;

use EmptyIterator;

final class None extends Option
{
private static $instance;
Expand Down Expand Up @@ -109,5 +111,10 @@ public function reject($value)
return $this;
}

public function getIterator()
{
return new EmptyIterator();
}

private function __construct() { }
}
}
4 changes: 3 additions & 1 deletion src/PhpOption/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

namespace PhpOption;

use IteratorAggregate;

/**
* Base Option Class.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
abstract class Option
abstract class Option implements IteratorAggregate
{
/**
* Creates an option given a return value.
Expand Down
7 changes: 7 additions & 0 deletions src/PhpOption/Some.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace PhpOption;

use ArrayIterator;

final class Some extends Option
{
private $value;
Expand Down Expand Up @@ -132,4 +134,9 @@ public function reject($value)

return $this;
}

public function getIterator()
{
return new ArrayIterator(array($this->value));
}
}
14 changes: 13 additions & 1 deletion tests/PhpOption/Tests/NoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,20 @@ public function testReject()
$this->assertSame($this->none, $this->none->reject(null));
}

public function testForeach()
{
$none = \PhpOption\None::create();

$called = 0;
foreach ($none as $value) {
$called++;
}

$this->assertEquals(0, $called);
}

protected function setUp()
{
$this->none = None::create();
}
}
}
15 changes: 15 additions & 0 deletions tests/PhpOption/Tests/SomeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ public function testReject()
$this->assertSame($some, $some->reject(true));
$this->assertInstanceOf('PhpOption\None', $some->reject('foo'));
}

public function testForeach()
{
$some = new Some('foo');

$called = 0;
$extractedValue = null;
foreach ($some as $value) {
$extractedValue = $value;
$called++;
}

$this->assertEquals('foo', $extractedValue);
$this->assertEquals(1, $called);
}
}

// For the interested reader of these tests, we have gone some great lengths
Expand Down

3 comments on commit 5d099bc

@asm89
Copy link
Contributor

@asm89 asm89 commented on 5d099bc Jan 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance you tag a release? @schmittjoh

@schmittjoh
Copy link
Owner Author

@schmittjoh schmittjoh commented on 5d099bc Jan 19, 2014 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asm89
Copy link
Contributor

@asm89 asm89 commented on 5d099bc Jan 19, 2014 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.