Skip to content

Commit

Permalink
Implement case sensitivity switch: value is represented as RfcEmailAd…
Browse files Browse the repository at this point in the history
…dress (case sensitive), or CaseInsensitiveEmailAddress (case insensitive)
  • Loading branch information
xificurk committed Jun 5, 2020
1 parent d800d96 commit 8d9fb57
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
14 changes: 11 additions & 3 deletions README.md
Expand Up @@ -23,12 +23,12 @@ extensions:
- Nepada\Bridges\EmailAddressInputDI\EmailAddressInputExtension
```

It will register extension method `addEmailAddress($name, $label)` to `Nette\Forms\Container`.
It will register extension method `addEmailAddress($name, $label, $caseSensitive = false)` to `Nette\Forms\Container`.


### Option B: use trait in your base form/container class

You can also use `EmailAddressInputMixin` trait in your base form/container class to add method `addEmailAddress($name, $label)`.
You can also use `EmailAddressInputMixin` trait in your base form/container class to add method `addEmailAddress($name, $label, $caseSensitive = false)`.

Example:

Expand Down Expand Up @@ -78,7 +78,7 @@ It automatically validates the user input and `getValue()` method always returns
$emailAddressInput = $form->addEmailAddress('E-mail');

// set value using EmailAddress value object
$emailAddressInput->setValue(EmailAddress::fromString('example@example.com'));
$emailAddressInput->setValue(CaseInsensitiveEmailAddress::fromString('example@example.com'));

// set value using string with a valid email address (it is internally converted to EmailAddress value object)
$emailAddressInput->setValue('example@example.com');
Expand All @@ -89,3 +89,11 @@ $emailAddressInput->getValue();
// InvalidEmailAddressException is thrown
$emailAddressInput->setValue('42');
```

### Case sensitivity

By default the input returns instance of `CaseInsensitiveEmailAddress`, i.e. the value object that treats the whole email address as case insensitive.

You can change this behaviour by calling `EmailAddressInput::setCaseSensitive(true)`, or by passing `$caseSensitive = true` when creating the input. With enabled case sensitivity the input's value will be represented as `RfcEmailAddress` instance.

For further details see the readme of [nepada/email-address](https://github.com/nepada/email-address).
5 changes: 3 additions & 2 deletions src/Bridges/EmailAddressInputForms/EmailAddressInputMixin.php
Expand Up @@ -12,11 +12,12 @@ trait EmailAddressInputMixin
/**
* @param string|int $name
* @param string|Html|null $label
* @param bool $caseSensitive
* @return EmailAddressInput
*/
public function addEmailAddress($name, $label = null): EmailAddressInput
public function addEmailAddress($name, $label = null, bool $caseSensitive = false): EmailAddressInput
{
return $this[$name] = new EmailAddressInput($label);
return $this[$name] = new EmailAddressInput($label, null, $caseSensitive);
}

}
Expand Up @@ -16,7 +16,11 @@ public static function register(): void
{
Container::extensionMethod(
'addEmailAddress',
fn (Container $container, $name, $label = null): EmailAddressInput => $container[$name] = new EmailAddressInput($label),
function (Container $container, $name, $label = null, bool $caseSensitive = false): EmailAddressInput {
$control = new EmailAddressInput($label, null, $caseSensitive);
$container[$name] = $control;
return $control;
},
);
}

Expand Down
32 changes: 28 additions & 4 deletions src/EmailAddressInput/EmailAddressInput.php
Expand Up @@ -3,6 +3,7 @@

namespace Nepada\EmailAddressInput;

use Nepada\EmailAddress\CaseInsensitiveEmailAddress;
use Nepada\EmailAddress\EmailAddress;
use Nepada\EmailAddress\InvalidEmailAddressException;
use Nepada\EmailAddress\RfcEmailAddress;
Expand All @@ -14,17 +15,26 @@
class EmailAddressInput extends TextInput
{

private bool $caseSensitive;

/**
* @param string|Html<mixed>|null $label
* @param int|null $maxLength
* @param bool $caseSensitive
*/
public function __construct($label = null, ?int $maxLength = null)
public function __construct($label = null, ?int $maxLength = null, bool $caseSensitive = false)
{
parent::__construct($label, $maxLength);
$this->caseSensitive = $caseSensitive;
$this->setNullable();
$this->addRule(Form::EMAIL);
}

public function setCaseSensitive(bool $caseSensitive): void
{
$this->caseSensitive = $caseSensitive;
}

public function getValue(): ?EmailAddress
{
$value = parent::getValue();
Expand All @@ -38,9 +48,9 @@ public function getValue(): ?EmailAddress
*/
public function setValue($value): self
{
if (is_string($value)) {
$value = RfcEmailAddress::fromString($value);
} elseif ($value !== null && ! $value instanceof EmailAddress) {
if (is_string($value) || $value instanceof EmailAddress) {
$value = $this->toEmailAddress($value);
} elseif ($value !== null) {
throw new \InvalidArgumentException(
sprintf(
'Value must be null, EmailAddress instance, or string with a valid email address, %s given in field "%s".',
Expand Down Expand Up @@ -87,4 +97,18 @@ public function isFilled(): bool
return $this->rawValue !== '' && $this->rawValue !== Strings::trim($this->translate($this->emptyValue));
}

/**
* @param string|EmailAddress $value
* @return EmailAddress
* @throws InvalidEmailAddressException
*/
private function toEmailAddress($value): EmailAddress
{
if ($this->caseSensitive) {
return RfcEmailAddress::fromString((string) $value);
}

return CaseInsensitiveEmailAddress::fromString((string) $value);
}

}
15 changes: 13 additions & 2 deletions tests/EmailAddressInput/EmailAddressInputTest.phpt
Expand Up @@ -3,6 +3,7 @@ declare(strict_types = 1);

namespace NepadaTests\EmailAddressInput;

use Nepada\EmailAddress\CaseInsensitiveEmailAddress;
use Nepada\EmailAddress\EmailAddress;
use Nepada\EmailAddress\InvalidEmailAddressException;
use Nepada\EmailAddress\RfcEmailAddress;
Expand Down Expand Up @@ -32,7 +33,7 @@ class EmailAddressInputTest extends TestCase
$emailAddress = RfcEmailAddress::fromString('example@example.com');
$input = new EmailAddressInput();
$input->setValue($emailAddress);
Assert::type(EmailAddress::class, $input->getValue());
Assert::type(CaseInsensitiveEmailAddress::class, $input->getValue());
Assert::same((string) $emailAddress, (string) $input->getValue());
}

Expand All @@ -41,7 +42,7 @@ class EmailAddressInputTest extends TestCase
$emailAddress = 'example@example.com';
$input = new EmailAddressInput();
$input->setValue($emailAddress);
Assert::type(EmailAddress::class, $input->getValue());
Assert::type(CaseInsensitiveEmailAddress::class, $input->getValue());
Assert::same($emailAddress, (string) $input->getValue());
}

Expand All @@ -67,6 +68,16 @@ class EmailAddressInputTest extends TestCase
);
}

public function testCaseSensitivityEnabled(): void
{
$emailAddress = CaseInsensitiveEmailAddress::fromString('Example@Example.com');
$input = new EmailAddressInput();
$input->setCaseSensitive(true);
$input->setValue($emailAddress);
Assert::type(RfcEmailAddress::class, $input->getValue());
Assert::same((string) $emailAddress, (string) $input->getValue());
}

public function testNoDataSubmitted(): void
{
$_SERVER['REQUEST_METHOD'] = 'POST';
Expand Down

0 comments on commit 8d9fb57

Please sign in to comment.