File: IsNumeric.php

package info (click to toggle)
php-hamcrest 1.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 812 kB
  • ctags: 1,410
  • sloc: php: 6,278; makefile: 20; sh: 10
file content (54 lines) | stat: -rw-r--r-- 1,033 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace Hamcrest\Type;

/*
 Copyright (c) 2010 hamcrest.org
 */
use Hamcrest\Core\IsTypeOf;

/**
 * Tests whether the value is numeric.
 */
class IsNumeric extends IsTypeOf
{

    public function __construct()
    {
        parent::__construct('number');
    }

    public function matches($item)
    {
        if ($this->isHexadecimal($item)) {
            return true;
        }

        return is_numeric($item);
    }

    /**
     * Return if the string passed is a valid hexadecimal number.
     * This check is necessary because PHP 7 doesn't recognize hexadecimal string as numeric anymore.
     *
     * @param mixed $item
     * @return boolean
     */
    private function isHexadecimal($item)
    {
        if (is_string($item) && preg_match('/^0x(.*)$/', $item, $matches)) {
            return ctype_xdigit($matches[1]);
        }

        return false;
    }

    /**
     * Is the value a numeric?
     *
     * @factory
     */
    public static function numericValue()
    {
        return new self;
    }
}