Domain::validate()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 18
rs 9.2222
1
<?php
2
3
namespace Whitelist\Definition;
4
5
/**
6
 * Represents a domain definition
7
 *
8
 * @author Sam Stenvall <[email protected]>
9
 * @copyright Copyright &copy; Sam Stenvall 2014-
10
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
11
 */
12
class Domain extends Definition
13
{
14
15
    public function validate()
16
    {
17
        // The domain name cannot be empty
18
        if (strlen($this->_definition) === 0) {
19
            return false;
20
        }
21
22
        // None of the parts in the domain name can contain invalid characters
23
        // or begin/end with a dash
24
        foreach (explode('.', $this->_definition) as $part) {
25
            if (! preg_match('/^[a-zA-Z0-9-\.]+$/', $part) ||
26
                substr($part, 0, 1) === '-' ||
27
                substr($part, -1) === '-') {
28
                return false;
29
            }
30
        }
31
32
        return true;
33
    }
34
35
    public function match($value)
36
    {
37
        return $this->_definition === $value;
38
    }
39
40
}
41