xref: /dokuwiki/inc/Subscriptions/SubscriberRegexBuilder.php (revision 5983e241c3fbc8b211747041c6d129f0c4c94d3e)
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
169c22b77cSMichael Große     *
17479c05b1SMichael Große     * @return string complete regexp including delimiters
18479c05b1SMichael Große     * @throws Exception when no data is passed
199c22b77cSMichael Große     * @author Andreas Gohr <andi@splitbrain.org>
209c22b77cSMichael Große     *
21479c05b1SMichael Große     */
229c22b77cSMichael Große    public function buildRegex($user = null, $style = null, $data = null)
239c22b77cSMichael 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);
39*5983e241SAndreas Gohr
40479c05b1SMichael Große        $style = array_map('preg_quote_cb', $style);
41479c05b1SMichael Große        $data = array_map('preg_quote_cb', $data);
42479c05b1SMichael Große
43479c05b1SMichael Große        // join
44*5983e241SAndreas Gohr        $user = implode('|', $user);
45*5983e241SAndreas Gohr        $style = implode('|', $style);
46*5983e241SAndreas Gohr        $data = implode('|', $data);
47479c05b1SMichael Große
48479c05b1SMichael Große        // any data at all?
499c22b77cSMichael Große        if ($user . $style . $data === '') {
509c22b77cSMichael Große            throw new Exception('no data passed');
519c22b77cSMichael Große        }
52479c05b1SMichael Große
53479c05b1SMichael Große        // replace empty values, set which ones are optional
54479c05b1SMichael Große        $sopt = '';
55479c05b1SMichael Große        $dopt = '';
56479c05b1SMichael Große        if ($user === '') {
57479c05b1SMichael Große            $user = '\S+';
58479c05b1SMichael Große        }
59479c05b1SMichael Große        if ($style === '') {
60479c05b1SMichael Große            $style = '\S+';
61479c05b1SMichael Große            $sopt = '?';
62479c05b1SMichael Große        }
63479c05b1SMichael Große        if ($data === '') {
64479c05b1SMichael Große            $data = '\S+';
65479c05b1SMichael Große            $dopt = '?';
66479c05b1SMichael Große        }
67479c05b1SMichael Große
68479c05b1SMichael Große        // assemble
69479c05b1SMichael Große        return "/^($user)(?:\\s+($style))$sopt(?:\\s+($data))$dopt$/";
70479c05b1SMichael Große    }
71479c05b1SMichael Große}
72