Skip to content

Commit

Permalink
Merge pull request #2 from snowio/feature/segment-filteration
Browse files Browse the repository at this point in the history
Add segment filtration
  • Loading branch information
Alexander Wanyoike authored Apr 15, 2021
2 parents 053199f + ca00fcb commit 7691483
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/SegmentSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ public function getIterator()
}
}

public function filter(callable $predicate): SegmentSet
{
return self::of(array_filter($this->segments, $predicate));
}

public function first(): ?Segment
{
if ($this->size() === 0) {
return null;
}
return reset($this->segments);
}

public function toJson(?string $action): array
{
$itemData = [];
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/SegmentSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SnowIO\DataLakeDataModel\Test;

use PHPUnit\Framework\TestCase;
use SnowIO\DataLakeDataModel\Segment;
use SnowIO\DataLakeDataModel\SegmentSet;
use SnowIO\DataLakeDataModel\Test\ACME\DataLake\Segments\Price;
use SnowIO\DataLakeDataModel\Test\ACME\DataLake\Segments\Stock;
Expand Down Expand Up @@ -68,4 +69,36 @@ public function testEquals()
self::assertFalse($otherSegments->equals($segments));
}

public function testFiltration()
{
$segments = SegmentSet::of([
Price::fromJson(SegmentTest::getPriceJson()),
Stock::fromJson(SegmentTest::getStockJson())
]);

$filteredSegments = $segments->filter(function (Segment $segment) {
return $segment->getSegmentId() === Price::NAME;
});

self::assertEquals(1, $filteredSegments->size());
}

public function testFirst()
{
$segments = SegmentSet::of([
$price = Price::fromJson(SegmentTest::getPriceJson()),
Stock::fromJson(SegmentTest::getStockJson())
]);

$actual = $segments->first();
self::assertTrue($actual->equals($price));
}

public function testFirstWithEmptySegmentSet()
{
$segments = SegmentSet::of([]);

$actual = $segments->first();
self::assertNull($actual);
}
}

0 comments on commit 7691483

Please sign in to comment.