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