Completed
Push — master ( 6199e8...c85b49 )
by Rick
03:48 queued 01:37
created

src/Rule/Alnum.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator\Rule;
10
11
/**
12
 * This rule checks if the value consists solely out of alphanumeric characters.
13
 *
14
 * @package Particle\Validator\Rule
15
 */
16 View Code Duplication
class Alnum extends Regex
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    /**
19
     * A constant that will be used for the error message when the value is not alphanumeric.
20
     */
21
    const NOT_ALNUM = 'Alnum::NOT_ALNUM';
22
23
    /**
24
     * A constant indicating spaces are allowed.
25
     */
26
    const ALLOW_SPACES = true;
27
28
    /**
29
     * A constant indicated spaces are *not* allowed.
30
     */
31
    const DISALLOW_SPACES = false;
32
33
    /**
34
     * The message templates which can be returned by this validator.
35
     *
36
     * @var array
37
     */
38
    protected $messageTemplates = [
39
        self::NOT_ALNUM => '{{ name }} may only consist out of numeric and alphabetic characters'
40
    ];
41
42
    /**
43
     * Construct the validation rule.
44
     *
45
     * @param bool $allowSpaces
46
     */
47 7
    public function __construct($allowSpaces = self::DISALLOW_SPACES)
48
    {
49 7
        parent::__construct($allowSpaces ? '~^[\p{L}0-9\s]*$~iu' : '~^[\p{L}0-9]*$~iu');
50 7
    }
51
52
    /**
53
     * Checks whether $value consists solely out of alphanumeric characters.
54
     *
55
     * @param mixed $value
56
     * @return bool
57
     */
58 7
    public function validate($value)
59
    {
60 7
        return $this->match($this->regex, $value, self::NOT_ALNUM);
61
    }
62
}
63