-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
pattern()->match()->groupBy()->flatMap()
#35
- Loading branch information
Showing
3 changed files
with
107 additions
and
34 deletions.
There are no files selected for viewing
15 changes: 14 additions & 1 deletion
15
src/TRegx/CleanRegex/Internal/Match/GroupBy/FlatMapStrategy.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,33 @@ | ||
<?php | ||
namespace TRegx\CleanRegex\Internal\Match\GroupBy; | ||
|
||
use TRegx\CleanRegex\Internal\Match\FlatMapper; | ||
use TRegx\CleanRegex\Internal\Match\MatchAll\EagerMatchAllFactory; | ||
use TRegx\CleanRegex\Internal\Model\Factory\MatchObjectFactory; | ||
use TRegx\CleanRegex\Internal\Model\Match\IndexedRawMatchOffset; | ||
use TRegx\CleanRegex\Internal\Model\Matches\IRawMatchesOffset; | ||
|
||
class FlatMapStrategy implements Strategy | ||
{ | ||
/** @var callable */ | ||
private $mapper; | ||
/** @var MatchObjectFactory */ | ||
private $factory; | ||
|
||
public function __construct(callable $mapper) | ||
public function __construct(callable $mapper, MatchObjectFactory $factory) | ||
{ | ||
$this->mapper = $mapper; | ||
$this->factory = $factory; | ||
} | ||
|
||
function transform(array $groups, IRawMatchesOffset $matches): array | ||
{ | ||
foreach ($groups as &$group) { | ||
$group = (new FlatMapper($group, function (IndexedRawMatchOffset $match) use ($matches) { | ||
$mapper = $this->mapper; | ||
return $mapper($this->factory->create($match->getIndex(), $match, new EagerMatchAllFactory($matches))); | ||
}))->get(); | ||
} | ||
return $groups; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters