xref: /plugin/struct/types/Url.php (revision 7d18b5ca1bdf29c4409316c574a7a574ec05e26f)
1<?php
2
3namespace plugin\struct\types;
4
5use plugin\struct\meta\ValidationException;
6
7class Url extends Text {
8
9    /**
10     * The final string should be an URL
11     *
12     * @param string $value
13     */
14    public function validate($value) {
15        $url = $this->config['prefix'] . trim($value) . $this->config['postfix'];
16
17        $schemes = getSchemes();
18        $regex = '^(' . join('|', $schemes) . '):\/\/.+';
19        if(!preg_match("/$regex/i", $url)) {
20            throw new ValidationException('Url invalid', $url);
21        }
22    }
23
24    /**
25     * @param string $value
26     * @param \Doku_Renderer $R
27     * @param string $mode
28     * @return bool
29     */
30    public function renderValue($value, \Doku_Renderer $R, $mode) {
31        $url = $this->config['prefix'] . trim($value) . $this->config['postfix'];
32        $R->externallink($url);
33        return true;
34    }
35
36}
37