Completed
Branch FET-9222-rest-api-writes (b69541)
by
unknown
25:55 queued 14:06
created

Basic::getLocalPartOfEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\validation\strategies;
4
5
use EventEspresso\core\services\validation\strategies\EmailValidationInterface;
6
use EventEspresso\core\domain\services\validation\EmailValidationException;
7
8
defined('EVENT_ESPRESSO_VERSION') || exit('No direct script access allowed');
9
10
11
12
/**
13
 * Class Basic
14
 * Performs super basic email validation
15
 *
16
 * @package        Event Espresso
17
 * @author         Mike Nelson
18
 * @since          $VID:$
19
 */
20
class Basic implements EmailValidationInterface
21
{
22
23
    /**
24
     * @param string $input
25
     * @return bool
26
     * @throws EmailValidationException
27
     */
28
    public function validate($input)
29
    {
30
        if (! preg_match('/^.+\@\S+$/', $input)) {
31
            // email not in correct {string}@{string} format
32
            throw new EmailValidationException(
33
                esc_html__('Email does not have the required @ sign.', 'event_espresso')
34
            );
35
        } else {
36
            $domain = $this->getDomainPartOfEmail($input);
37
            $local = $this->getLocalPartOfEmail($input);
38
            $localLen = strlen($local);
39
            $domainLen = strlen($domain);
40
            if ($localLen < 1) {
41
                //no local part
42
                throw new EmailValidationException(
43
                    esc_html__('Email local-part (before the @) is required.', 'event_espresso')
44
                );
45
            }
46
            if ($localLen > 64) {
47
                // local part length exceeded
48
                throw new EmailValidationException(
49
                    esc_html__('Email local-part (before the @) is too long.', 'event_espresso')
50
                );
51
            }
52
            if ($domainLen < 1) {
53
                throw new EmailValidationException(
54
                    esc_html__('Email domain (after the @) is required.', 'event_espresso')
55
                );
56
            }
57
            if ($domainLen > 255) {
58
                // domain part length exceeded
59
                throw new EmailValidationException(
60
                    esc_html__('Email domain (after the @) is too long.', 'event_espresso')
61
                );
62
            }
63
            if ($local[0] === '.') {
64
                // local part starts with '.'
65
                throw new EmailValidationException(
66
                    esc_html__('Email local-part (before the @) must not begin with a period.', 'event_espresso')
67
                );
68
            }
69
            if ($local[$localLen - 1] === '.') {
70
                // local part starts or ends with '.'
71
                throw new EmailValidationException(
72
                    esc_html__('Email local-part (before the @) must not end with a period.', 'event_espresso')
73
                );
74
            }
75
            if (preg_match('/\\.\\./', $local)) {
76
                // local part has two consecutive dots
77
                throw new EmailValidationException(
78
                    esc_html__(
79
                        'Email local-part (before the @) must not have two consecutive periods.',
80
                        'event_espresso'
81
                    )
82
                );
83
            }
84
            if (preg_match('/\\.\\./', $domain)) {
85
                // domain part has two consecutive dots
86
                throw new EmailValidationException(
87
                    esc_html__('Email domain (after the @) must not have two consecutive periods.', 'event_espresso')
88
                );
89
            }
90
        }
91
        return true;
92
    }
93
94
95
96
    /**
97
     * Gets the local part of the email
98
     * @param $input
99
     * @return bool|string
100
     */
101
    protected function getLocalPartOfEmail($input)
102
    {
103
        $atIndex = strrpos($input, '@');
104
        return substr($input, 0, $atIndex);
105
    }
106
107
108
109
    /**
110
     * Gets the domain part of the email
111
     * @param $input
112
     * @return bool|string
113
     */
114
    protected function getDomainPartOfEmail($input)
115
    {
116
        $atIndex = strrpos($input, '@');
117
        return substr($input, $atIndex + 1);
118
    }
119
}
120
// End of file Basic.php
121
// Location: core\services\validation/Basic.php