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