Differential D8261 Diff 19659 src/infrastructure/customfield/parser/PhabricatorCustomFieldMonogramParser.php
Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/customfield/parser/PhabricatorCustomFieldMonogramParser.php
- This file was added.
<?php | |||||
abstract class PhabricatorCustomFieldMonogramParser | |||||
extends Phobject { | |||||
abstract protected function getPrefixes(); | |||||
abstract protected function getSuffixes(); | |||||
abstract protected function getInfixes(); | |||||
abstract protected function getMonogramPattern(); | |||||
public function parseCorpus($corpus) { | |||||
$prefixes = $this->getPrefixes(); | |||||
$suffixes = $this->getSuffixes(); | |||||
$infixes = $this->getInfixes(); | |||||
$prefix_regex = $this->buildRegex($prefixes); | |||||
$infix_regex = $this->buildRegex($infixes, true); | |||||
$suffix_regex = $this->buildRegex($suffixes, true, true); | |||||
$monogram_pattern = $this->getMonogramPattern(); | |||||
$pattern = | |||||
'/'. | |||||
'(?:^|\b)'. | |||||
$prefix_regex. | |||||
$infix_regex. | |||||
'((?:'.$monogram_pattern.'[,\s]*)+)'. | |||||
$suffix_regex. | |||||
'(?:$|\b)'. | |||||
'/i'; | |||||
$matches = null; | |||||
$ok = preg_match_all( | |||||
$pattern, | |||||
$corpus, | |||||
$matches, | |||||
PREG_SET_ORDER | PREG_OFFSET_CAPTURE); | |||||
if ($ok === false) { | |||||
throw new Exception(pht('Regular expression "%s" is invalid!', $pattern)); | |||||
} | |||||
$results = array(); | |||||
foreach ($matches as $set) { | |||||
$results[] = array( | |||||
'match' => $set[0][0], | |||||
'prefix' => $set[1][0], | |||||
'infix' => $set[2][0], | |||||
'monograms' => array_filter(preg_split('/[,\s]+/', $set[3][0])), | |||||
'suffix' => $set[4][0], | |||||
'offset' => $set[0][1], | |||||
); | |||||
} | |||||
return $results; | |||||
} | |||||
private function buildRegex(array $list, $optional = false, $final = false) { | |||||
$parts = array(); | |||||
foreach ($list as $string) { | |||||
$parts[] = preg_quote($string, '/'); | |||||
} | |||||
$parts = implode('|', $parts); | |||||
$maybe_tail = $final ? '' : '\s+'; | |||||
$maybe_optional = $optional ? '?' : ''; | |||||
return '(?:('.$parts.')'.$maybe_tail.')'.$maybe_optional; | |||||
} | |||||
} |