Completed
Pull Request — master (#10)
by
unknown
01:00
created

Domain::validate()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.9777
c 0
b 0
f 0
cc 6
nc 4
nop 0
1
<?php
2
3
namespace Safelist\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
		// None of the parts in the domain name can contain invalid characters 
22
		// or begin/end with a dash
23
		foreach (explode('.', $this->_definition) as $part)
24
		{
25
			if (!preg_match('/^[a-zA-Z0-9-\.]+$/', $part) ||
26
					substr($part, 0, 1) === '-' ||
27
					substr($part, -1) === '-')
28
			{
29
				return false;
30
			}
31
		}
32
33
		return true;
34
	}
35
36
	public function match($value)
37
	{
38
		return $this->_definition === $value;
39
	}
40
41
}
42