Failed Conditions
Pull Request — 4.0 (#3667)
by k-yamamura
06:36
created

NoRFCEmailValidator::isValid()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Validator\EmailValidator;
15
16
use Egulias\EmailValidator\EmailValidator;
17
use Egulias\EmailValidator\Validation\EmailValidation;
18
19
class NoRFCEmailValidator extends EmailValidator
20
{
21
    /**
22
     * @var boolean
23
     */
24
    protected $eccube_rfc_email_check;
25
26
    /**
27
     * NoRFCEmailValidator constructor.
28
     *
29
     * @param bool $eccube_rfc_email_check
30
     */
31
    public function __construct($eccube_rfc_email_check)
32
    {
33
        parent::__construct();
34
        $this->eccube_rfc_email_check = $eccube_rfc_email_check;
35
    }
36
37
    /**
38
     * @param $email
39
     * @param EmailValidation $emailValidation
40
     *
41
     * @return bool
42
     */
43
    public function isValid($email, EmailValidation $emailValidation)
44
    {
45
        $wsp = '[\x20\x09]';
46
        $vchar = '[\x21-\x7e]';
47
        $quoted_pair = "\\\\(?:$vchar|$wsp)";
48
        $qtext = '[\x21\x23-\x5b\x5d-\x7e]';
49
        $qcontent = "(?:$qtext|$quoted_pair)";
50
        $quoted_string = "\"$qcontent*\"";
51
        $atext = '[a-zA-Z0-9!#$%&\'*+\-\/\=?^_`{|}~]';
52
        $dot_atom = "$atext+(?:[.]$atext+)*";
53
        $local_part = "(?:$dot_atom|$quoted_string)";
54
        $domain = $dot_atom;
55
        $addr_spec = "{$local_part}[@]$domain";
56
        $dot_atom_loose = "$atext+(?:[.]|$atext)*";
57
        $local_part_loose = "(?:$dot_atom_loose|$quoted_string)";
58
        $addr_spec_loose = "{$local_part_loose}[@]$domain";
59
60
        if ($this->eccube_rfc_email_check) {
61
            $regexp = "/\A{$addr_spec}\z/";
62
        } else {
63
            // 携帯メールアドレス用に、..や.@を許容する。
64
            $regexp = "/\A{$addr_spec_loose}\z/";
65
        }
66
67
        if (preg_match($regexp, $email)) {
68
            return true;
69
        }
70
71
        return false;
72
    }
73
}
74