xref: /plugin/struct/types/Mail.php (revision 8ec87814898d9c0f4ea1082a8bacaa17ac6b1bf7)
1<?php
2
3namespace dokuwiki\plugin\struct\types;
4
5use dokuwiki\plugin\struct\meta\ValidationException;
6
7class Mail extends Text
8{
9
10    protected $config = array(
11        'prefix' => '',
12        'postfix' => '',
13    );
14
15    /**
16     * Output the stored data
17     *
18     * @param string|int $value the value stored in the database
19     * @param \Doku_Renderer $R the renderer currently used to render the data
20     * @param string $mode The mode the output is rendered in (eg. XHTML)
21     * @return bool true if $mode could be satisfied
22     */
23    public function renderValue($value, \Doku_Renderer $R, $mode)
24    {
25        $mail = $this->config['prefix'] . $value . $this->config['postfix'];
26        $R->emaillink($mail);
27        return true;
28    }
29
30    /**
31     * Validate
32     *
33     * @param int|string $rawvalue
34     * @return int|string
35     */
36    public function validate($rawvalue)
37    {
38        $rawvalue = parent::validate($rawvalue);
39
40        $mail = $this->config['prefix'] . $rawvalue . $this->config['postfix'];
41        if (!mail_isvalid($mail)) {
42            throw new ValidationException('Mail invalid', $mail);
43        }
44
45        return $rawvalue;
46    }
47}
48