[ 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       * Construct a regular expression for parsing a subscription definition line
  11       *
  12       * @param string|array $user
  13       * @param string|array $style
  14       * @param string|array $data
  15       *
  16       * @return string complete regexp including delimiters
  17       * @throws Exception when no data is passed
  18       * @author Andreas Gohr <andi@splitbrain.org>
  19       *
  20       */
  21      public function buildRegex($user = null, $style = null, $data = null)
  22      {
  23          // always work with arrays
  24          $user = (array)$user;
  25          $style = (array)$style;
  26          $data = (array)$data;
  27  
  28          // clean
  29          $user = array_filter(array_map('trim', $user));
  30          $style = array_filter(array_map('trim', $style));
  31          $data = array_filter(array_map('trim', $data));
  32  
  33          // user names are encoded
  34          $user = array_map('auth_nameencode', $user);
  35  
  36          // quote
  37          $user = array_map('preg_quote_cb', $user);
  38  
  39          $style = array_map('preg_quote_cb', $style);
  40          $data = array_map('preg_quote_cb', $data);
  41  
  42          // join
  43          $user = implode('|', $user);
  44          $style = implode('|', $style);
  45          $data = implode('|', $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  }