1<?php 2 3 4namespace dokuwiki\Subscriptions; 5 6 7use Exception; 8 9class SubscriberRegexBuilder 10{ 11 12 /** 13 * Construct a regular expression for parsing a subscription definition line 14 * 15 * @author Andreas Gohr <andi@splitbrain.org> 16 * 17 * @param string|array $user 18 * @param string|array $style 19 * @param string|array $data 20 * @return string complete regexp including delimiters 21 * @throws Exception when no data is passed 22 */ 23 public function buildRegex($user = null, $style = null, $data = null) { 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 === '') throw new Exception('no data passed'); 49 50 // replace empty values, set which ones are optional 51 $sopt = ''; 52 $dopt = ''; 53 if($user === '') { 54 $user = '\S+'; 55 } 56 if($style === '') { 57 $style = '\S+'; 58 $sopt = '?'; 59 } 60 if($data === '') { 61 $data = '\S+'; 62 $dopt = '?'; 63 } 64 65 // assemble 66 return "/^($user)(?:\\s+($style))$sopt(?:\\s+($data))$dopt$/"; 67 } 68} 69