xref: /plugin/struct/helper/config.php (revision 7234bfb14e712ff548d9266ef32fdcc8eaf2d04e)
129877279SMichael Große<?php
2d6d97f60SAnna Dabrowska
3*7234bfb1Ssplitbrainuse dokuwiki\Extension\Plugin;
4*7234bfb1Ssplitbrainuse dokuwiki\plugin\struct\meta\StructException;
5*7234bfb1Ssplitbrainuse dokuwiki\plugin\struct\meta\Search;
6*7234bfb1Ssplitbrain
729877279SMichael Große/**
829877279SMichael Große * DokuWiki Plugin struct (Helper Component)
929877279SMichael Große *
1029877279SMichael Große * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
1129877279SMichael Große * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
1229877279SMichael Große */
1329877279SMichael Große
14*7234bfb1Ssplitbrainclass helper_plugin_struct_config extends Plugin
15d6d97f60SAnna Dabrowska{
1629877279SMichael Große    /**
1729877279SMichael Große     * @param string $val
1829877279SMichael Große     *
1929877279SMichael Große     * @return array
2029877279SMichael Große     */
21d6d97f60SAnna Dabrowska    public function parseSort($val)
22d6d97f60SAnna Dabrowska    {
2329877279SMichael Große        if (substr($val, 0, 1) == '^') {
24*7234bfb1Ssplitbrain            return [substr($val, 1), false];
2529877279SMichael Große        }
26*7234bfb1Ssplitbrain        return [$val, true];
2729877279SMichael Große    }
2829877279SMichael Große
2929877279SMichael Große    /**
3029877279SMichael Große     * @param $logic
3129877279SMichael Große     * @param $val
3229877279SMichael Große     *
3329877279SMichael Große     * @return array|bool
3429877279SMichael Große     */
35d6d97f60SAnna Dabrowska    public function parseFilterLine($logic, $val)
36d6d97f60SAnna Dabrowska    {
3729877279SMichael Große        $flt = $this->parseFilter($val);
3829877279SMichael Große        if ($flt) {
3929877279SMichael Große            $flt[] = $logic;
4029877279SMichael Große            return $flt;
4129877279SMichael Große        }
4229877279SMichael Große        return false;
4329877279SMichael Große    }
4429877279SMichael Große
4529877279SMichael Große    /**
4629877279SMichael Große     * Parse a filter
4729877279SMichael Große     *
4829877279SMichael Große     * @param string $val
4929877279SMichael Große     *
5029877279SMichael Große     * @return array ($col, $comp, $value)
51*7234bfb1Ssplitbrain     * @throws StructException
5229877279SMichael Große     */
53d6d97f60SAnna Dabrowska    protected function parseFilter($val)
54d6d97f60SAnna Dabrowska    {
5529877279SMichael Große
56*7234bfb1Ssplitbrain        $comps = Search::$COMPARATORS;
5701a8eccdSMichael Große        $comps[] = '*~';
5832c6849eSMichael Große        array_unshift($comps, '<>');
5929877279SMichael Große        $comps = array_map('preg_quote_cb', $comps);
60*7234bfb1Ssplitbrain        $comps = implode('|', $comps);
6129877279SMichael Große
6229877279SMichael Große        if (!preg_match('/^(.*?)(' . $comps . ')(.*)$/', $val, $match)) {
63*7234bfb1Ssplitbrain            throw new StructException('Invalid search filter %s', hsc($val));
6429877279SMichael Große        }
6529877279SMichael Große        array_shift($match); // we don't need the zeroth match
6629877279SMichael Große        $match[0] = trim($match[0]);
6729877279SMichael Große        $match[2] = trim($match[2]);
6829877279SMichael Große        return $match;
6929877279SMichael Große    }
7029877279SMichael Große}
7129877279SMichael Große
7229877279SMichael Große// vim:ts=4:sw=4:et:
73