Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Alpha
100.00% covered (success)
100.00%
6 / 6
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%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace Verja\Validator;
4
5use Verja\Error;
6use Verja\Validator;
7
8class Alpha 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' . ($this->allowSpaces ? ' ' : '') . ']*$/u';
32        if (preg_match($regex, $value)) {
33            return true;
34        }
35
36        $this->error = new Error('CONTAINS_NON_ALPHA', $value, 'value should not contain non alphabetical characters');
37        return false;
38    }
39}