1479c05b1SMichael Große<?php 2479c05b1SMichael Große 3479c05b1SMichael Großenamespace dokuwiki\Subscriptions; 4479c05b1SMichael Große 5479c05b1SMichael Großeuse Exception; 6479c05b1SMichael Große 7479c05b1SMichael Großeclass SubscriberRegexBuilder 8479c05b1SMichael Große{ 9479c05b1SMichael Große 10479c05b1SMichael Große /** 11479c05b1SMichael Große * Construct a regular expression for parsing a subscription definition line 12479c05b1SMichael Große * 13479c05b1SMichael Große * @param string|array $user 14479c05b1SMichael Große * @param string|array $style 15479c05b1SMichael Große * @param string|array $data 16*9c22b77cSMichael Große * 17479c05b1SMichael Große * @return string complete regexp including delimiters 18479c05b1SMichael Große * @throws Exception when no data is passed 19*9c22b77cSMichael Große * @author Andreas Gohr <andi@splitbrain.org> 20*9c22b77cSMichael Große * 21479c05b1SMichael Große */ 22*9c22b77cSMichael Große public function buildRegex($user = null, $style = null, $data = null) 23*9c22b77cSMichael Große { 24479c05b1SMichael Große // always work with arrays 25479c05b1SMichael Große $user = (array)$user; 26479c05b1SMichael Große $style = (array)$style; 27479c05b1SMichael Große $data = (array)$data; 28479c05b1SMichael Große 29479c05b1SMichael Große // clean 30479c05b1SMichael Große $user = array_filter(array_map('trim', $user)); 31479c05b1SMichael Große $style = array_filter(array_map('trim', $style)); 32479c05b1SMichael Große $data = array_filter(array_map('trim', $data)); 33479c05b1SMichael Große 34479c05b1SMichael Große // user names are encoded 35479c05b1SMichael Große $user = array_map('auth_nameencode', $user); 36479c05b1SMichael Große 37479c05b1SMichael Große // quote 38479c05b1SMichael Große $user = array_map('preg_quote_cb', $user); 39479c05b1SMichael Große $style = array_map('preg_quote_cb', $style); 40479c05b1SMichael Große $data = array_map('preg_quote_cb', $data); 41479c05b1SMichael Große 42479c05b1SMichael Große // join 43479c05b1SMichael Große $user = join('|', $user); 44479c05b1SMichael Große $style = join('|', $style); 45479c05b1SMichael Große $data = join('|', $data); 46479c05b1SMichael Große 47479c05b1SMichael Große // any data at all? 48*9c22b77cSMichael Große if ($user . $style . $data === '') { 49*9c22b77cSMichael Große throw new Exception('no data passed'); 50*9c22b77cSMichael Große } 51479c05b1SMichael Große 52479c05b1SMichael Große // replace empty values, set which ones are optional 53479c05b1SMichael Große $sopt = ''; 54479c05b1SMichael Große $dopt = ''; 55479c05b1SMichael Große if ($user === '') { 56479c05b1SMichael Große $user = '\S+'; 57479c05b1SMichael Große } 58479c05b1SMichael Große if ($style === '') { 59479c05b1SMichael Große $style = '\S+'; 60479c05b1SMichael Große $sopt = '?'; 61479c05b1SMichael Große } 62479c05b1SMichael Große if ($data === '') { 63479c05b1SMichael Große $data = '\S+'; 64479c05b1SMichael Große $dopt = '?'; 65479c05b1SMichael Große } 66479c05b1SMichael Große 67479c05b1SMichael Große // assemble 68479c05b1SMichael Große return "/^($user)(?:\\s+($style))$sopt(?:\\s+($data))$dopt$/"; 69479c05b1SMichael Große } 70479c05b1SMichael Große} 71