for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* Highlighter
*
* Copyright (C) 2015, Some right reserved.
* @author Kacper "Kadet" Donat <[email protected]>
* Contact with author:
* Xmpp: [email protected]
* E-mail: [email protected]
* From Kadet with love.
*/
namespace Kadet\Highlighter\Matcher;
use Kadet\Highlighter\Parser\Token;
use Kadet\Highlighter\Parser\TokenFactoryInterface;
class RegexMatcher implements MatcherInterface
{
private $regex;
private $groups;
* RegexMatcher constructor.
* @param $regex
* @param array|null $groups
public function __construct($regex, array $groups = [1 => null])
$this->regex = $regex;
$this->groups = $groups;
}
* Matches all occurrences and returns token list
* @param string $source Source to match tokens
* @param TokenFactoryInterface $factory
* @return array
public function match($source, TokenFactoryInterface $factory)
preg_match_all($this->regex, $source, $matches, PREG_OFFSET_CAPTURE);
$matches = array_intersect_key($matches, $this->groups);
$result = [];
foreach ($matches as $id => $group) {
$name = $this->groups[$id];
foreach ($group as $match) {
if ($match[1] === -1) {
continue;
/** @var Token $token */
$token = $factory->create([$name, 'pos' => $match[1], 'length' => strlen($match[0])]);
$end = $token->getEnd();
$result[spl_object_hash($token)] = $token;
$result[spl_object_hash($end)] = $end;
return $result;