[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/inc/Subscriptions/ -> SubscriberRegexBuilder.php (source)

   1  <?php
   2  
   3  namespace dokuwiki\Subscriptions;
   4  
   5  use Exception;
   6  
   7  class SubscriberRegexBuilder
   8  {
   9  
  10      /**
  11       * Construct a regular expression for parsing a subscription definition line
  12       *
  13       * @param string|array $user
  14       * @param string|array $style
  15       * @param string|array $data
  16       *
  17       * @return string complete regexp including delimiters
  18       * @throws Exception when no data is passed
  19       * @author Andreas Gohr <andi@splitbrain.org>
  20       *
  21       */
  22      public function buildRegex($user = null, $style = null, $data = null)
  23      {
  24          // always work with arrays
  25          $user = (array)$user;
  26          $style = (array)$style;
  27          $data = (array)$data;
  28  
  29          // clean
  30          $user = array_filter(array_map('trim', $user));
  31          $style = array_filter(array_map('trim', $style));
  32          $data = array_filter(array_map('trim', $data));
  33  
  34          // user names are encoded
  35          $user = array_map('auth_nameencode', $user);
  36  
  37          // quote
  38          $user = array_map('preg_quote_cb', $user);
  39          $style = array_map('preg_quote_cb', $style);
  40          $data = array_map('preg_quote_cb', $data);
  41  
  42          // join
  43          $user = join('|', $user);
  44          $style = join('|', $style);
  45          $data = join('|', $data);
  46  
  47          // any data at all?
  48          if ($user . $style . $data === '') {
  49              throw new Exception('no data passed');
  50          }
  51  
  52          // replace empty values, set which ones are optional
  53          $sopt = '';
  54          $dopt = '';
  55          if ($user === '') {
  56              $user = '\S+';
  57          }
  58          if ($style === '') {
  59              $style = '\S+';
  60              $sopt = '?';
  61          }
  62          if ($data === '') {
  63              $data = '\S+';
  64              $dopt = '?';
  65          }
  66  
  67          // assemble
  68          return "/^($user)(?:\\s+($style))$sopt(?:\\s+($data))$dopt$/";
  69      }
  70  }