Skip to content

Commit

Permalink
Added match()->groupBy()->offsets() #35
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Feb 25, 2020
1 parent a61bb90 commit 8d80f06
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 29 deletions.

This file was deleted.

35 changes: 35 additions & 0 deletions src/TRegx/CleanRegex/Internal/Match/GroupBy/OffsetsStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace TRegx\CleanRegex\Internal\Match\GroupBy;

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

class OffsetsStrategy implements Strategy
{
/** @var Subjectable */
private $subjectable;
/** @var boolean */
private $characterOffsets;

public function __construct(?Subjectable $subjectable, bool $characterOffsets)
{
$this->subjectable = $subjectable;
$this->characterOffsets = $characterOffsets;
}

function transform(array $groups, IRawMatchesOffset $matches): array
{
foreach ($groups as &$group) {
$group = array_map(function (IRawMatchOffset $match) {
$byteOffset = $match->byteOffset();
if ($this->characterOffsets) {
return ByteOffset::toCharacterOffset($this->subjectable->getSubject(), $byteOffset);
}
return $byteOffset;
}, $group);
}
return $groups;
}
}
9 changes: 7 additions & 2 deletions src/TRegx/CleanRegex/Match/GroupByPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
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\OffsetsStrategy;
use TRegx\CleanRegex\Internal\Match\GroupBy\Strategy;
use TRegx\CleanRegex\Internal\Match\GroupBy\TextsStrategy;
use TRegx\CleanRegex\Internal\Model\Factory\MatchObjectFactoryImpl;
Expand All @@ -28,9 +28,14 @@ public function texts(): array
return $this->groupBy(new TextsStrategy());
}

public function offsets(): array
{
return $this->groupBy(new OffsetsStrategy($this->base, true));
}

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

public function map(callable $mapper): array
Expand Down
33 changes: 24 additions & 9 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_offsets()
{
// when
$result = $this->groupBy()->offsets();

// then
$this->assertEquals([
'cm' => [5, 15, 25],
'mm' => [10, 20],
], $result);
}

/**
* @test
*/
Expand All @@ -33,8 +48,8 @@ public function shouldGroupBy_byteOffsets()

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

Expand Down Expand Up @@ -67,8 +82,8 @@ public function shouldGroupBy_flatMap()

// then
$this->assertEquals([
'cm' => ['14cm', 4, '19cm', 14, '2cm', 24],
'mm' => ['13mm', 9, '18mm', 19],
'cm' => ['14cm', 5, '19cm', 15, '2cm', 25],
'mm' => ['13mm', 10, '18mm', 20],
], $result);
}

Expand Down Expand Up @@ -228,12 +243,12 @@ public function mappersWithMatch(): array
{
return [
'map' => ['map', [
'cm' => [['19cm', 14], ['2cm', 24]],
'mm' => [['18mm', 19]],
'cm' => [['19cm', 15], ['2cm', 25]],
'mm' => [['18mm', 20]],
]],
'flatMap' => ['flatMap', [
'cm' => ['19cm', 14, '2cm', 24],
'mm' => ['18mm', 19],
'cm' => ['19cm', 15, '2cm', 25],
'mm' => ['18mm', 20],
]],
];
}
Expand All @@ -257,6 +272,6 @@ private function filtered(): GroupByPattern

private function match(): AbstractMatchPattern
{
return pattern('\d+(?<unit>cm|mm)?')->match('12, 14cm 13mm 19cm 18mm 2cm');
return pattern('\d+(?<unit>cm|mm)?')->match('12, 14cm 13mm 19cm 18mm 2cm');
}
}

0 comments on commit 8d80f06

Please sign in to comment.