IPCIDR::validate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 16
rs 10
1
<?php
2
3
namespace Whitelist\Definition;
4
5
use Exception;
6
use IpUtils\Expression\ExpressionInterface;
7
use IpUtils\Expression\Subnet;
8
use IpUtils\Factory;
9
10
/**
11
 * Represents a CIDR notation
12
 *
13
 * @author Sam Stenvall <[email protected]>
14
 * @copyright Copyright &copy; Sam Stenvall 2014-
15
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
16
 */
17
abstract class IPCIDR extends Definition
18
{
19
20
    /**
21
     * @var ExpressionInterface
22
     */
23
    protected $_subnet;
24
25
    public function validate()
26
    {
27
        try {
28
            $this->_subnet = Factory::getExpression($this->_definition);
29
30
            // Check that we got a subnet expression and not something else
31
            if (! $this->_subnet instanceof Subnet) {
32
                return false;
33
            }
34
        } catch (Exception $e) {
35
            unset($e);
36
37
            return false;
38
        }
39
40
        return true;
41
    }
42
43
    public function match($value)
44
    {
45
        try {
46
            $address = Factory::getAddress($value);
47
48
            return $this->_subnet->matches($address);
49
        } catch (Exception $e) {
50
            unset($e);
51
52
            return false;
53
        }
54
    }
55
56
}
57