Completed
Push — master ( f02f00...968d20 )
by Rick
03:07
created

Integer::shouldBreakChainOnError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 constant indicated the integer check is strict
36
     */
37
    const STRICT = true;
38
39
    /**
40
     * A constant indicating the integer check is *not* strict.
41
     */
42
    const NOT_STRICT = false;
43
44
    /**
45
     * A bool denoting whether or not strict checking should be done.
46
     *
47
     * @var bool
48
     */
49
    private $strict;
50
51
    /**
52
     * @param bool $strict
53
     */
54 18
    public function __construct($strict = self::NOT_STRICT)
55
    {
56 18
        $this->strict = $strict;
57 18
    }
58
59
    /**
60
     * Validates if $value represents an integer.
61
     *
62
     * @param mixed $value
63
     * @return bool
64
     */
65 20
    public function validate($value)
66
    {
67 20
        if ($this->strict && is_int($value)) {
68 4
            return true;
69
        }
70
71 16
        if (!$this->strict && false !== filter_var($value, FILTER_VALIDATE_INT)) {
72 9
            return true;
73
        }
74
75 7
        return $this->error(self::NOT_AN_INTEGER);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 7
    public function shouldBreakChainOnError()
82
    {
83 7
        return true;
84
    }
85
}
86