1<?php 2 3namespace dokuwiki\Subscriptions; 4 5use Exception; 6 7class 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 40 $style = array_map('preg_quote_cb', $style); 41 $data = array_map('preg_quote_cb', $data); 42 43 // join 44 $user = implode('|', $user); 45 $style = implode('|', $style); 46 $data = implode('|', $data); 47 48 // any data at all? 49 if ($user . $style . $data === '') { 50 throw new Exception('no data passed'); 51 } 52 53 // replace empty values, set which ones are optional 54 $sopt = ''; 55 $dopt = ''; 56 if ($user === '') { 57 $user = '\S+'; 58 } 59 if ($style === '') { 60 $style = '\S+'; 61 $sopt = '?'; 62 } 63 if ($data === '') { 64 $data = '\S+'; 65 $dopt = '?'; 66 } 67 68 // assemble 69 return "/^($user)(?:\\s+($style))$sopt(?:\\s+($data))$dopt$/"; 70 } 71} 72