• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nepada / email-address-input / 6343920702

28 Sep 2023 08:22PM UTC coverage: 96.296% (+0.1%) from 96.154%
6343920702

push

github

xificurk
Drop useless dev dependency

52 of 54 relevant lines covered (96.3%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.87
/src/EmailAddressInput/EmailAddressInput.php
1
<?php
2
declare(strict_types = 1);
3

4
namespace Nepada\EmailAddressInput;
5

6
use Nepada\EmailAddress\CaseInsensitiveEmailAddress;
7
use Nepada\EmailAddress\EmailAddress;
8
use Nepada\EmailAddress\InvalidEmailAddressException;
9
use Nepada\EmailAddress\RfcEmailAddress;
10
use Nette\Forms\Controls\TextInput;
11
use Nette\Forms\Form;
12
use Nette\Utils\Html;
13
use Nette\Utils\Strings;
14

15
class EmailAddressInput extends TextInput
16
{
17

18
    private bool $caseSensitive;
19

20
    public function __construct(string|Html|null $label = null, ?int $maxLength = null, bool $caseSensitive = false)
1✔
21
    {
22
        parent::__construct($label, $maxLength);
1✔
23
        $this->caseSensitive = $caseSensitive;
1✔
24
        $this->setNullable();
1✔
25
        $this->addRule(Form::EMAIL);
1✔
26
    }
1✔
27

28
    public function setCaseSensitive(bool $caseSensitive): void
1✔
29
    {
30
        $this->caseSensitive = $caseSensitive;
1✔
31
    }
1✔
32

33
    public function getValue(): ?EmailAddress
34
    {
35
        /** @var EmailAddress|null $value */
36
        $value = parent::getValue();
1✔
37
        return $value;
1✔
38
    }
39

40
    /**
41
     * @internal
42
     * @return $this
43
     */
44
    public function setValue(mixed $value): static
1✔
45
    {
46
        if (is_string($value) || $value instanceof EmailAddress) {
1✔
47
            $value = $this->toEmailAddress($value);
1✔
48
        } elseif ($value !== null) {
1✔
49
            throw new \InvalidArgumentException(
1✔
50
                sprintf(
1✔
51
                    'Value must be null, EmailAddress instance, or string with a valid email address, %s given in field "%s".',
1✔
52
                    gettype($value),
1✔
53
                    $this->name,
1✔
54
                ),
55
            );
56
        }
57

58
        parent::setValue($value);
1✔
59
        return $this;
1✔
60
    }
61

62
    /**
63
     * @param EmailAddress|string|null $value
64
     * @return $this
65
     */
66
    public function setDefaultValue(mixed $value): static
67
    {
68
        parent::setDefaultValue($value);
×
69
        return $this;
×
70
    }
71

72
    public function loadHttpData(): void
73
    {
74
        /** @var string $value */
75
        $value = $this->getHttpData(Form::DATA_LINE);
1✔
76

77
        if ($value === '' || $value === Strings::trim($this->translate($this->emptyValue))) {
1✔
78
            $this->value = null;
1✔
79
            $this->rawValue = $value;
1✔
80
            return;
1✔
81
        }
82

83
        try {
84
            $this->setValue($value);
1✔
85
        } catch (InvalidEmailAddressException $exception) {
1✔
86
            $this->value = null;
1✔
87
            $this->rawValue = $value;
1✔
88
        }
89
    }
1✔
90

91
    public function isFilled(): bool
92
    {
93
        return $this->rawValue !== '' && $this->rawValue !== Strings::trim($this->translate($this->emptyValue));
1✔
94
    }
95

96
    /**
97
     * @throws InvalidEmailAddressException
98
     */
99
    private function toEmailAddress(string|EmailAddress $value): EmailAddress
1✔
100
    {
101
        if ($this->caseSensitive) {
1✔
102
            return RfcEmailAddress::fromString((string) $value);
1✔
103
        }
104

105
        return CaseInsensitiveEmailAddress::fromString((string) $value);
1✔
106
    }
107

108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc