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