3 abstract class PhabricatorCustomFieldMonogramParser
6 abstract protected function getPrefixes();
7 abstract protected function getSuffixes();
8 abstract protected function getInfixes();
9 abstract protected function getMonogramPattern();
11 public function parseCorpus($corpus) {
12 $prefixes = $this->getPrefixes();
13 $suffixes = $this->getSuffixes();
14 $infixes = $this->getInfixes();
16 $prefix_regex = $this->buildRegex($prefixes);
17 $infix_regex = $this->buildRegex($infixes, true);
18 $suffix_regex = $this->buildRegex($suffixes, true, true);
20 $monogram_pattern = $this->getMonogramPattern();
27 '((?:'.$monogram_pattern.'(?:\b|$)[,\s]*)+)'.
28 '(?:\band\s+('.$monogram_pattern.'(?:\b|$)))?'.
38 PREG_SET_ORDER | PREG_OFFSET_CAPTURE
);
41 throw new Exception(pht('Regular expression "%s" is invalid!', $pattern));
45 foreach ($matches as $set) {
46 $monograms = array_filter(preg_split('/[,\s]+/', $set[3][0]));
48 if (isset($set[4]) && $set[4][0]) {
49 $monograms[] = $set[4][0];
53 'match' => $set[0][0],
54 'prefix' => $set[1][0],
55 'infix' => $set[2][0],
56 'monograms' => $monograms,
57 'suffix' => idx(idx($set, 5, array()), 0, ''),
58 'offset' => $set[0][1],
65 private function buildRegex(array $list, $optional = false, $final = false) {
67 foreach ($list as $string) {
68 $parts[] = preg_quote($string, '/');
70 $parts = implode('|', $parts);
72 $maybe_tail = $final ?
'' : '\s+';
73 $maybe_optional = $optional ?
'?' : '';
75 return '(?i:('.$parts.')'.$maybe_tail.')'.$maybe_optional;