Path: blob/master/src/infrastructure/customfield/parser/PhabricatorCustomFieldMonogramParser.php
13400 views
<?php12abstract class PhabricatorCustomFieldMonogramParser3extends Phobject {45abstract protected function getPrefixes();6abstract protected function getSuffixes();7abstract protected function getInfixes();8abstract protected function getMonogramPattern();910public function parseCorpus($corpus) {11$prefixes = $this->getPrefixes();12$suffixes = $this->getSuffixes();13$infixes = $this->getInfixes();1415$prefix_regex = $this->buildRegex($prefixes);16$infix_regex = $this->buildRegex($infixes, true);17$suffix_regex = $this->buildRegex($suffixes, true, true);1819$monogram_pattern = $this->getMonogramPattern();2021$pattern =22'/'.23'(?:^|\b)'.24$prefix_regex.25$infix_regex.26'((?:'.$monogram_pattern.'(?:\b|$)[,\s]*)+)'.27'(?:\band\s+('.$monogram_pattern.'(?:\b|$)))?'.28$suffix_regex.29'(?:$|\b)'.30'/';3132$matches = null;33$ok = preg_match_all(34$pattern,35$corpus,36$matches,37PREG_SET_ORDER | PREG_OFFSET_CAPTURE);3839if ($ok === false) {40throw new Exception(pht('Regular expression "%s" is invalid!', $pattern));41}4243$results = array();44foreach ($matches as $set) {45$monograms = array_filter(preg_split('/[,\s]+/', $set[3][0]));4647if (isset($set[4]) && $set[4][0]) {48$monograms[] = $set[4][0];49}5051$results[] = array(52'match' => $set[0][0],53'prefix' => $set[1][0],54'infix' => $set[2][0],55'monograms' => $monograms,56'suffix' => idx(idx($set, 5, array()), 0, ''),57'offset' => $set[0][1],58);59}6061return $results;62}6364private function buildRegex(array $list, $optional = false, $final = false) {65$parts = array();66foreach ($list as $string) {67$parts[] = preg_quote($string, '/');68}69$parts = implode('|', $parts);7071$maybe_tail = $final ? '' : '\s+';72$maybe_optional = $optional ? '?' : '';7374return '(?i:('.$parts.')'.$maybe_tail.')'.$maybe_optional;75}7677}787980