[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/infrastructure/customfield/parser/ -> PhabricatorCustomFieldMonogramParser.php (source)

   1  <?php
   2  
   3  abstract class PhabricatorCustomFieldMonogramParser
   4    extends Phobject {
   5  
   6    abstract protected function getPrefixes();
   7    abstract protected function getSuffixes();
   8    abstract protected function getInfixes();
   9    abstract protected function getMonogramPattern();
  10  
  11    public function parseCorpus($corpus) {
  12      $prefixes = $this->getPrefixes();
  13      $suffixes = $this->getSuffixes();
  14      $infixes = $this->getInfixes();
  15  
  16      $prefix_regex = $this->buildRegex($prefixes);
  17      $infix_regex = $this->buildRegex($infixes, true);
  18      $suffix_regex = $this->buildRegex($suffixes, true, true);
  19  
  20      $monogram_pattern = $this->getMonogramPattern();
  21  
  22      $pattern =
  23        '/'.
  24        '(?:^|\b)'.
  25        $prefix_regex.
  26        $infix_regex.
  27        '((?:'.$monogram_pattern.'[,\s]*)+)'.
  28        $suffix_regex.
  29        '(?:$|\b)'.
  30        '/';
  31  
  32      $matches = null;
  33      $ok = preg_match_all(
  34        $pattern,
  35        $corpus,
  36        $matches,
  37        PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
  38  
  39      if ($ok === false) {
  40        throw new Exception(pht('Regular expression "%s" is invalid!', $pattern));
  41      }
  42  
  43      $results = array();
  44      foreach ($matches as $set) {
  45        $results[] = array(
  46          'match' => $set[0][0],
  47          'prefix' => $set[1][0],
  48          'infix' => $set[2][0],
  49          'monograms' => array_filter(preg_split('/[,\s]+/', $set[3][0])),
  50          'suffix' => idx(idx($set, 4, array()), 0, ''),
  51          'offset' => $set[0][1],
  52        );
  53      }
  54  
  55      return $results;
  56    }
  57  
  58    private function buildRegex(array $list, $optional = false, $final = false) {
  59      $parts = array();
  60      foreach ($list as $string) {
  61        $parts[] = preg_quote($string, '/');
  62      }
  63      $parts = implode('|', $parts);
  64  
  65      $maybe_tail = $final ? '' : '\s+';
  66      $maybe_optional = $optional ? '?' : '';
  67  
  68      return '(?i:('.$parts.')'.$maybe_tail.')'.$maybe_optional;
  69    }
  70  
  71  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1