appwrite/src/Appwrite/Network/Validator/Email.php

80 lines
1.4 KiB
PHP
Raw Normal View History

2021-03-23 17:27:51 +00:00
<?php
2021-03-24 17:47:17 +00:00
namespace Appwrite\Network\Validator;
2021-03-23 17:27:51 +00:00
2024-10-01 14:30:47 +00:00
use Utopia\Http\Validator;
2021-03-23 17:27:51 +00:00
/**
* Email
*
* Validate that an variable is a valid email address
*
2024-10-01 14:30:47 +00:00
* @package Utopia\Http\Validator
2021-03-23 17:27:51 +00:00
*/
class Email extends Validator
{
protected bool $allowEmpty;
public function __construct(bool $allowEmpty = false)
{
$this->allowEmpty = $allowEmpty;
}
2021-03-23 17:27:51 +00:00
/**
* Get Description
*
* Returns validator description
*
* @return string
*/
public function getDescription(): string
2021-03-23 17:27:51 +00:00
{
return 'Value must be a valid email address';
}
/**
* Is valid
*
* Validation will pass when $value is valid email address.
*
* @param mixed $value
* @return bool
*/
public function isValid($value): bool
2021-03-23 17:27:51 +00:00
{
if ($this->allowEmpty && \strlen($value) === 0) {
return true;
}
2021-03-23 17:27:51 +00:00
if (!\filter_var($value, FILTER_VALIDATE_EMAIL)) {
return false;
}
return true;
}
/**
* Is array
*
* Function will return true if object is array.
*
* @return bool
*/
public function isArray(): bool
{
return false;
}
/**
* Get Type
*
* Returns validator type.
*
* @return string
*/
public function getType(): string
{
return self::TYPE_STRING;
}
2021-03-23 17:27:51 +00:00
}