Completed
Push — master ( 1aeb0e...0f6b10 )
by Rick
03:02
created

Integer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
use Particle\Validator\Rule;
12
13
/**
14
 * This rule is for validating if a value represents an integer.
15
 *
16
 * @package Particle\Validator\Rule
17
 */
18
class Integer extends Rule
19
{
20
    /**
21
     * A constant that will be used when the value does not represent an integer value.
22
     */
23
    const NOT_AN_INTEGER = 'Integer::NOT_AN_INTEGER';
24
25
    /**
26
     * The message templates which can be returned by this validator.
27
     *
28
     * @var array
29
     */
30
    protected $messageTemplates = [
31
        self::NOT_AN_INTEGER => '{{ name }} must be an integer',
32
    ];
33
34
    /**
35
     * A bool denoting whether or not strict checking should be done.
36
     *
37
     * @var bool
38
     */
39
    private $strict;
40
41
    /**
42
     * @param bool $strict
43
     */
44 18
    public function __construct($strict = false)
45
    {
46 18
        $this->strict = $strict;
47 18
    }
48
49
    /**
50
     * Validates if $value represents an integer.
51
     *
52
     * @param mixed $value
53
     * @return bool
54
     */
55 18
    public function validate($value)
56
    {
57 18
        if ($this->strict && is_int($value)) {
58 4
            return true;
59
        }
60
61 14
        if (!$this->strict && false !== filter_var($value, FILTER_VALIDATE_INT)) {
62 8
            return true;
63
        }
64
65 6
        return $this->error(self::NOT_AN_INTEGER);
66
    }
67
}
68