Skip to content

Commit

Permalink
Added match()->groupBy()->byteOffsets() #35
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Feb 25, 2020
1 parent 0e247c6 commit a61bb90
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace TRegx\CleanRegex\Internal\Match\GroupBy;

use TRegx\CleanRegex\Internal\Model\Match\IRawMatchOffset;
use TRegx\CleanRegex\Internal\Model\Matches\IRawMatchesOffset;

class ByteOffsetsStrategy implements Strategy
{
function transform(array $groups, IRawMatchesOffset $matches): array
{
foreach ($groups as &$group) {
$group = array_map(function (IRawMatchOffset $match) {
return $match->byteOffset();
}, $group);
}
return $groups;
}
}
6 changes: 6 additions & 0 deletions src/TRegx/CleanRegex/Match/GroupByPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace TRegx\CleanRegex\Match;

use TRegx\CleanRegex\Internal\Match\Base\Base;
use TRegx\CleanRegex\Internal\Match\GroupBy\ByteOffsetsStrategy;
use TRegx\CleanRegex\Internal\Match\GroupBy\FlatMapStrategy;
use TRegx\CleanRegex\Internal\Match\GroupBy\MapStrategy;
use TRegx\CleanRegex\Internal\Match\GroupBy\Strategy;
Expand All @@ -27,6 +28,11 @@ public function texts(): array
return $this->groupBy(new TextsStrategy());
}

public function byteOffsets(): array
{
return $this->groupBy(new ByteOffsetsStrategy());
}

public function map(callable $mapper): array
{
return $this->groupBy(new MapStrategy($mapper, $this->factory()));
Expand Down
15 changes: 15 additions & 0 deletions test/Feature/TRegx/CleanRegex/Match/groupBy/MatchPatternTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ public function shouldGroupBy_texts()
], $result);
}

/**
* @test
*/
public function shouldGroupBy_byteOffsets()
{
// when
$result = $this->groupBy()->byteOffsets();

// then
$this->assertEquals([
'cm' => [4, 14, 24],
'mm' => [9, 19],
], $result);
}

/**
* @test
*/
Expand Down

0 comments on commit a61bb90

Please sign in to comment.