Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
10 / 10 |
AlphaNumeric | |
100.00% |
1 / 1 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
10 / 10 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
validate | |
100.00% |
1 / 1 |
3 | |
100.00% |
8 / 8 |
<?php | |
namespace Verja\Validator; | |
use Verja\Error; | |
use Verja\Validator; | |
class AlphaNumeric extends Validator | |
{ | |
/** @var bool */ | |
protected $allowSpaces; | |
/** | |
* Alpha constructor. | |
* @param bool $allowSpaces | |
*/ | |
public function __construct(bool $allowSpaces = false) | |
{ | |
$this->allowSpaces = $allowSpaces; | |
} | |
/** | |
* Validate $value | |
* | |
* @param mixed $value | |
* @param array $context | |
* @return bool | |
*/ | |
public function validate($value, array $context = []): bool | |
{ | |
$regex = '/^[\pL\pM\pN' . ($this->allowSpaces ? ' ' : '') . ']*$/u'; | |
if (preg_match($regex, $value)) { | |
return true; | |
} | |
$this->error = new Error( | |
'CONTAINS_NON_ALPHANUMERIC', | |
$value, | |
'value should not contain non alphanumeric characters' | |
); | |
return false; | |
} | |
} |