-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Email Class for DataType and its tests added
- Loading branch information
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Selami\Entity\DataType; | ||
|
||
use Selami\Entity\Interfaces\DataTypeInterface; | ||
use InvalidArgumentException; | ||
|
||
class Email extends DataTypeAbstract implements DataTypeInterface | ||
{ | ||
const DATA_FORMAT_ERROR = 'Assertion failed for value "%s" for "%s" : INVALID_MAIL_ADDRESS_FORMAT'; | ||
|
||
protected static $defaults = [ | ||
'default' => '' | ||
]; | ||
|
||
|
||
/** | ||
* Email constructor. | ||
* @param string $key | ||
* @param mixed $datum | ||
* @param array $options | ||
* @throws validArgumentException | ||
*/ | ||
public function __construct(string $key, $datum, array $options = []) | ||
{ | ||
$this->key = $key; | ||
$this->datum = $datum; | ||
$this->checkValidOptions($options); | ||
$this->options = array_merge(self::$defaults, $options); | ||
} | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function assert() | ||
{ | ||
if (filter_var($this->datum, FILTER_VALIDATE_EMAIL) === false) { | ||
$this->errorMessageTemplate = self::DATA_FORMAT_ERROR; | ||
$this->throwException(); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function normalize() | ||
{ | ||
try { | ||
$this->assert(); | ||
return $this->datum; | ||
} catch (InvalidArgumentException $e) { | ||
return $this->options['default']; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace tests; | ||
|
||
use Selami\Test\Abstracts\DataType; | ||
|
||
class MyEmailClass extends DataType | ||
{ | ||
|
||
public function setUp() | ||
{ | ||
$this->className = '\\Selami\\Entity\\DataType\\Email'; | ||
$this->validValue = 'mehmet@mkorkmaz.com'; | ||
$this->key = 'email'; | ||
$this->options = []; | ||
} | ||
|
||
public function trueProvider() | ||
{ | ||
return [ | ||
['m@k.co'], | ||
['mehmet.korkmaz@reformo.net'], | ||
['mehmet+korkmaz@reformo.net'] | ||
]; | ||
} | ||
|
||
public function exceptionProvider() | ||
{ | ||
return [ | ||
['mehmet@korkmaz@reformo.net'], // no two @ char | ||
['mehmet @mkorkmaz.com'], // no whitespace | ||
['m@k.co,m@k.com'], // no comma char | ||
['m@k.co;m@k.com'], // no colon char | ||
['.m@k.co'], // no period at the first char | ||
['m\k@k.co'], // backslash is invalid | ||
['m+k.co'], // there must be at least one @ char | ||
['email123456789012345678901234567890123456789012345678901234567890@k.co'] // max 64 char before @ | ||
]; | ||
} | ||
|
||
public function defaultsProvider() | ||
{ | ||
return [ | ||
['mehmet,@mkorkmaz.com', 'mehmet@mkorkmaz.com', ['default' => 'mehmet@mkorkmaz.com']], | ||
['m@k.co;m@k.com', null, ['default' => null]] | ||
]; | ||
} | ||
} |