Skip to content

Commit

Permalink
Add byteOffsets() for Match.groups() and Match.namedGroups() (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Wilkowski committed Dec 2, 2018
1 parent 654db12 commit 8025c6c
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@
namespace TRegx\CleanRegex\Match\Details\Groups;

use TRegx\CleanRegex\Exception\CleanRegex\InternalCleanRegexException;
use TRegx\CleanRegex\Internal\ByteOffset;
use TRegx\CleanRegex\Internal\Model\Match\IRawMatchOffset;
use TRegx\CleanRegex\Internal\Subjectable;

abstract class AbstractMatchGroups implements MatchGroups
{
/** @var IRawMatchOffset */
protected $match;
/** @var int */
protected $index;
/** @var Subjectable */
private $subjectable;

protected function __construct(IRawMatchOffset $match)
public function __construct(IRawMatchOffset $match, Subjectable $subjectable)
{
$this->match = $match;
$this->subjectable = $subjectable;
}

/**
Expand All @@ -28,6 +33,16 @@ public function texts(): array
* @return (int|null)[]
*/
public function offsets(): array
{
return array_map(function (int $offset) {
return ByteOffset::toCharacterOffset($this->subjectable->getSubject(), $offset);
}, $this->byteOffsets());
}

/**
* @return (int|null)[]
*/
public function byteOffsets()
{
return $this->sliceAndFilter($this->match->getGroupsOffsets());
}
Expand Down
7 changes: 0 additions & 7 deletions src/TRegx/CleanRegex/Match/Details/Groups/IndexedGroups.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
<?php
namespace TRegx\CleanRegex\Match\Details\Groups;

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

class IndexedGroups extends AbstractMatchGroups
{
public function __construct(IRawMatchOffset $match)
{
parent::__construct($match);
}

protected function filterGroupKey($nameOrIndex): bool
{
return is_int($nameOrIndex);
Expand Down
7 changes: 0 additions & 7 deletions src/TRegx/CleanRegex/Match/Details/Groups/NamedGroups.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
<?php
namespace TRegx\CleanRegex\Match\Details\Groups;

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

class NamedGroups extends AbstractMatchGroups
{
public function __construct(IRawMatchOffset $match)
{
parent::__construct($match);
}

protected function filterGroupKey($nameOrIndex): bool
{
return is_string($nameOrIndex);
Expand Down
4 changes: 2 additions & 2 deletions src/TRegx/CleanRegex/Match/Details/MatchImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ public function groupNames(): array

public function groups(): IndexedGroups
{
return new IndexedGroups($this->match);
return new IndexedGroups($this->match, $this->subjectable);
}

public function namedGroups(): NamedGroups
{
return new NamedGroups($this->match);
return new NamedGroups($this->match, $this->subjectable);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,76 @@ public function shouldGetEmptyMatchedGroup()
/**
* @test
*/
public function shouldGetGroupNames()
public function shouldGetNamedGroup()
{
// given
pattern('(?<one>first) and (?<two>second)')
->match('first and second')
->first(function (Match $match) {
// when
$groupNames = $match->groupNames();
$groupNames = $match->namedGroups()->texts();

// then
$this->assertEquals(['one', 'two'], $groupNames);
$expected = [
'one' => 'first',
'two' => 'second'
];
$this->assertEquals($expected, $groupNames);
});
}

/**
* @test
*/
public function shouldGetNamedGroup()
public function shouldGetGroupsOffsets_indexedGroups()
{
// given
pattern('(?<one>first ę) and (?<two>second)')
->match('first ę and second')
->first(function (Match $match) {
// when
$offsets = $match->groups()->offsets();
$byteOffsets = $match->groups()->byteOffsets();

// then
$this->assertEquals([0, 12], $offsets);
$this->assertEquals([0, 13], $byteOffsets);
});
}

/**
* @test
*/
public function shouldGetGroupsOffsets_namedGroups()
{
// given
pattern('(?<one>first ę) and (?<two>second)')
->match('first ę and second')
->first(function (Match $match) {
// when
$offsets = $match->namedGroups()->offsets();
$byteOffsets = $match->namedGroups()->byteOffsets();

// then
$this->assertEquals(['one' => 0, 'two' => 12], $offsets);
$this->assertEquals(['one' => 0, 'two' => 13], $byteOffsets);
});
}

/**
* @test
*/
public function shouldGetGroupNames()
{
// given
pattern('(?<one>first) and (?<two>second)')
->match('first and second')
->first(function (Match $match) {
// when
$groupNames = $match->namedGroups()->texts();
$groupNames = $match->groupNames();

// then
$expected = [
'one' => 'first',
'two' => 'second'
];
$this->assertEquals($expected, $groupNames);
$this->assertEquals(['one', 'two'], $groupNames);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ public function shouldGetOffset()
$matchGroup->offset();
}

/**
* @test
*/
public function shouldGetByteOffset()
{
// given
$matchGroup = $this->matchGroup();

// then
$this->expectException(GroupNotMatchedException::class);
$this->expectExceptionMessage("Expected to call byteOffset() for group 'first', but group was not matched at all");

// when
$matchGroup->byteOffset();
}

/**
* @test
*/
Expand Down

0 comments on commit 8025c6c

Please sign in to comment.