xref: /plugin/struct/types/Mail.php (revision 7fe2cdf28c472c686961bf42f0123eb33d2f3e60)
1a5cdcc05SAndreas Gohr<?php
2d6d97f60SAnna Dabrowska
3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\types;
4a5cdcc05SAndreas Gohr
5ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\ValidationException;
6a5cdcc05SAndreas Gohr
7d6d97f60SAnna Dabrowskaclass Mail extends Text
8d6d97f60SAnna Dabrowska{
9*7fe2cdf2SAndreas Gohr    protected $config = [
10*7fe2cdf2SAndreas Gohr        'prefix' => '',
11*7fe2cdf2SAndreas Gohr        'postfix' => ''
12*7fe2cdf2SAndreas Gohr    ];
13a5cdcc05SAndreas Gohr
14a5cdcc05SAndreas Gohr    /**
15a5cdcc05SAndreas Gohr     * Output the stored data
16a5cdcc05SAndreas Gohr     *
17a5cdcc05SAndreas Gohr     * @param string|int $value the value stored in the database
18a5cdcc05SAndreas Gohr     * @param \Doku_Renderer $R the renderer currently used to render the data
19a5cdcc05SAndreas Gohr     * @param string $mode The mode the output is rendered in (eg. XHTML)
20a5cdcc05SAndreas Gohr     * @return bool true if $mode could be satisfied
21a5cdcc05SAndreas Gohr     */
22d6d97f60SAnna Dabrowska    public function renderValue($value, \Doku_Renderer $R, $mode)
23d6d97f60SAnna Dabrowska    {
24a5cdcc05SAndreas Gohr        $mail = $this->config['prefix'] . $value . $this->config['postfix'];
25a5cdcc05SAndreas Gohr        $R->emaillink($mail);
26a5cdcc05SAndreas Gohr        return true;
27a5cdcc05SAndreas Gohr    }
28a5cdcc05SAndreas Gohr
29a5cdcc05SAndreas Gohr    /**
30a5cdcc05SAndreas Gohr     * Validate
31a5cdcc05SAndreas Gohr     *
3223169abeSAndreas Gohr     * @param int|string $rawvalue
33806eec82SAndreas Gohr     * @return int|string
34a5cdcc05SAndreas Gohr     */
35d6d97f60SAnna Dabrowska    public function validate($rawvalue)
36d6d97f60SAnna Dabrowska    {
3723169abeSAndreas Gohr        $rawvalue = parent::validate($rawvalue);
38806eec82SAndreas Gohr
3923169abeSAndreas Gohr        $mail = $this->config['prefix'] . $rawvalue . $this->config['postfix'];
40a5cdcc05SAndreas Gohr        if (!mail_isvalid($mail)) {
41a5cdcc05SAndreas Gohr            throw new ValidationException('Mail invalid', $mail);
42a5cdcc05SAndreas Gohr        }
43806eec82SAndreas Gohr
4423169abeSAndreas Gohr        return $rawvalue;
45a5cdcc05SAndreas Gohr    }
46a5cdcc05SAndreas Gohr}
47