1<?php
2
3use dokuwiki\Extension\Plugin;
4use dokuwiki\plugin\struct\meta\StructException;
5use dokuwiki\plugin\struct\meta\Search;
6
7/**
8 * DokuWiki Plugin struct (Helper Component)
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
12 */
13
14class helper_plugin_struct_config extends Plugin
15{
16    /**
17     * @param string $val
18     *
19     * @return array
20     */
21    public function parseSort($val)
22    {
23        if (substr($val, 0, 1) == '^') {
24            return [substr($val, 1), false];
25        }
26        return [$val, true];
27    }
28
29    /**
30     * @param $logic
31     * @param $val
32     *
33     * @return array|bool
34     */
35    public function parseFilterLine($logic, $val)
36    {
37        $flt = $this->parseFilter($val);
38        if ($flt) {
39            $flt[] = $logic;
40            return $flt;
41        }
42        return false;
43    }
44
45    /**
46     * Parse a filter
47     *
48     * @param string $val
49     *
50     * @return array ($col, $comp, $value)
51     * @throws StructException
52     */
53    protected function parseFilter($val)
54    {
55
56        $comps = Search::$COMPARATORS;
57        $comps[] = '*~';
58        array_unshift($comps, '<>');
59        $comps = array_map('preg_quote_cb', $comps);
60        $comps = implode('|', $comps);
61
62        if (!preg_match('/^(.*?)(' . $comps . ')(.*)$/', $val, $match)) {
63            throw new StructException('Invalid search filter %s', hsc($val));
64        }
65        array_shift($match); // we don't need the zeroth match
66        $match[0] = trim($match[0]);
67        $match[2] = trim($match[2]);
68        return $match;
69    }
70}
71
72// vim:ts=4:sw=4:et:
73