Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ $chain->reduce(function ($current, $value) {
- `->join([$glue])`
- `->last()`
- `->reduce()`
- `->some()`
- `->sum()`

## Author
Expand Down
2 changes: 2 additions & 0 deletions src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Cocur\Chain\Link\Shift;
use Cocur\Chain\Link\Shuffle;
use Cocur\Chain\Link\Slice;
use Cocur\Chain\Link\Some;
use Cocur\Chain\Link\Sort;
use Cocur\Chain\Link\SortKeys;
use Cocur\Chain\Link\Splice;
Expand Down Expand Up @@ -81,6 +82,7 @@ class Chain extends AbstractChain implements Countable
use Shift;
use Shuffle;
use Slice;
use Some;
use Sort;
use SortKeys;
use Splice;
Expand Down
27 changes: 27 additions & 0 deletions src/Link/Some.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Cocur\Chain\Link;

/**
* Some.
*
* @author Nicolas Reynis
*/
trait Some
{
/**
* @param callable $callback
*
* @return bool
*/
public function some(callable $callback): bool
{
foreach ($this->array as $index => $element) {
if ($callback($element, $index)) {
return true;
}
}

return false;
}
}
1 change: 1 addition & 0 deletions tests/ChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function chainHasTraits(): void
$this->assertTrue(method_exists($c, 'shift'));
$this->assertTrue(method_exists($c, 'shuffle'));
$this->assertTrue(method_exists($c, 'slice'));
$this->assertTrue(method_exists($c, 'some'));
$this->assertTrue(method_exists($c, 'sort'));
$this->assertTrue(method_exists($c, 'sortKeys'));
$this->assertTrue(method_exists($c, 'splice'));
Expand Down
87 changes: 87 additions & 0 deletions tests/Link/SomeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Cocur\Chain\Link;

/**
* SomeTest.
*
* @author Nicolas Reynis
* @group unit
*/
class SomeTest extends \PHPUnit\Framework\TestCase
{
/**
* @test
* @covers \Cocur\Chain\Link\Some::some()
*/
public function someReturnTrueWhenConditionPass(): void
{
/** @var Some $mock */
$mock = $this->getMockForTrait(Some::class);
$mock->array = [1, 2, 3, 4];

$this->assertTrue($mock->some(function ($v) {
return $v > 0;
}));
}

/**
* @test
* @covers \Cocur\Chain\Link\Some::some()
*/
public function someReturnFalseWhenConditionFail(): void
{
/** @var Some $mock */
$mock = $this->getMockForTrait(Some::class);
$mock->array = [-1, -2, -3, -4];

$this->assertFalse($mock->some(function ($v) {
return $v > 0;
}));
}

/**
* @test
* @covers \Cocur\Chain\Link\Some::some()
*/
public function someReturnTrueWhenConditionFailOnSomeElements(): void
{
/** @var Some $mock */
$mock = $this->getMockForTrait(Some::class);
$mock->array = [1, 2, -3, 4];

$this->assertTrue($mock->some(function ($v) {
return $v > 0;
}));
}

/**
* @test
* @covers \Cocur\Chain\Link\Some::some()
*/
public function someReturnFalseWithEmptyChain(): void
{
/** @var Some $mock */
$mock = $this->getMockForTrait(Some::class);
$mock->array = [];

$this->assertFalse($mock->some(function ($v) {
return true;
}));
}

/**
* @test
* @covers \Cocur\Chain\Link\Some::some()
*/
public function someCallbackReceivesAlsoArrayKeys(): void
{
/** @var Some $mock */
$mock = $this->getMockForTrait(Some::class);
$mock->array = ['foo' => 'fizz', 'bar' => 'buzz'];

$this->assertTrue($mock->some(function ($v, $k) {
return 'foo' === $k || 'bar' === $k;
}));
}
}