Completed
Push — master ( d7d169...d93f91 )
by Filipe
02:03
created

AlphaNumeric::validates()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
/**
4
 * This file is part of slick/validator package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Validator;
11
12
/**
13
 * AlphaNumeric validator
14
 *
15
 * @package Slick\Validator
16
 * @author  Filipe Silva <[email protected]>
17
 */
18 View Code Duplication
class AlphaNumeric extends AbstractValidator implements ValidatorInterface
0 ignored issues
show
Duplication introduced by
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...
19
{
20
21
    /**
22
     * @readwrite
23
     * @var array Message templates
24
     */
25
    protected $messageTemplate =
26
        'The value can only contain alpha numeric characters.';
27
28
29
    /**
30
     * Returns true if and only if $value meets the validation requirements
31
     *
32
     * The context specified can be used in the validation process so that
33
     * the same value can be valid or invalid depending on that data.
34
     *
35
     * @param mixed $value
36
     * @param array|mixed $context
37
     *
38
     * @return bool
39
     */
40 2
    public function validates($value, $context = [])
41
    {
42 2
        $result = (boolean) preg_match('/^([0-9a-zA-Z\-]+)$/i', $value);
43 2
        if (!$result) {
44 2
            $this->addMessage($this->messageTemplate, $value);
45 2
        }
46 2
        return $result;
47
    }
48
}