xref: /dokuwiki/inc/Subscriptions/SubscriberRegexBuilder.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
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     * Construct a regular expression for parsing a subscription definition line
11479c05b1SMichael Große     *
12479c05b1SMichael Große     * @param string|array $user
13479c05b1SMichael Große     * @param string|array $style
14479c05b1SMichael Große     * @param string|array $data
159c22b77cSMichael Große     *
16479c05b1SMichael Große     * @return string complete regexp including delimiters
17479c05b1SMichael Große     * @throws Exception when no data is passed
189c22b77cSMichael Große     * @author Andreas Gohr <andi@splitbrain.org>
199c22b77cSMichael Große     *
20479c05b1SMichael Große     */
219c22b77cSMichael Große    public function buildRegex($user = null, $style = null, $data = null)
229c22b77cSMichael Große    {
23479c05b1SMichael Große        // always work with arrays
24479c05b1SMichael Große        $user = (array)$user;
25479c05b1SMichael Große        $style = (array)$style;
26479c05b1SMichael Große        $data = (array)$data;
27479c05b1SMichael Große
28479c05b1SMichael Große        // clean
29479c05b1SMichael Große        $user = array_filter(array_map('trim', $user));
30479c05b1SMichael Große        $style = array_filter(array_map('trim', $style));
31479c05b1SMichael Große        $data = array_filter(array_map('trim', $data));
32479c05b1SMichael Große
33479c05b1SMichael Große        // user names are encoded
34479c05b1SMichael Große        $user = array_map('auth_nameencode', $user);
35479c05b1SMichael Große
36479c05b1SMichael Große        // quote
37479c05b1SMichael Große        $user = array_map('preg_quote_cb', $user);
38*5983e241SAndreas Gohr
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
43*5983e241SAndreas Gohr        $user = implode('|', $user);
44*5983e241SAndreas Gohr        $style = implode('|', $style);
45*5983e241SAndreas Gohr        $data = implode('|', $data);
46479c05b1SMichael Große
47479c05b1SMichael Große        // any data at all?
489c22b77cSMichael Große        if ($user . $style . $data === '') {
499c22b77cSMichael Große            throw new Exception('no data passed');
509c22b77cSMichael 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