Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AlphaNumeric
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace Verja\Validator;
4
5use Verja\Error;
6use Verja\Validator;
7
8class AlphaNumeric extends Validator
9{
10    /** @var bool */
11    protected $allowSpaces;
12
13    /**
14     * Alpha constructor.
15     * @param bool $allowSpaces
16     */
17    public function __construct(bool $allowSpaces = false)
18    {
19        $this->allowSpaces = $allowSpaces;
20    }
21
22    /**
23     * Validate $value
24     *
25     * @param mixed $value
26     * @param array $context
27     * @return bool
28     */
29    public function validate($value, array $context = []): bool
30    {
31        $regex = '/^[\pL\pM\pN' . ($this->allowSpaces ? ' ' : '') . ']*$/u';
32        if (preg_match($regex, $value)) {
33            return true;
34        }
35
36        $this->error = new Error(
37            'CONTAINS_NON_ALPHANUMERIC',
38            $value,
39            'value should not contain non alphanumeric characters'
40        );
41        return false;
42    }
43}