HostName   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 32
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 7 3
A __construct() 0 11 2
1
<?php
2
3
/*
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Domain\Web;
13
14
/**
15
 * HostName class.
16
 *
17
 * @author Ivannis Suárez Jerez <[email protected]>
18
 */
19
class HostName extends Host
20
{
21
    /**
22
     * @param string $host
23
     *
24
     * @return bool
25
     */
26
    public static function isValid($host)
27
    {
28
        return preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $host) //valid chars check
29
            && preg_match('/^.{1,253}$/', $host) //overall length check
30
            && preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $host) //length of each label
31
        ;
32
    }
33
34
    /**
35
     * @param string $host
36
     *
37
     * @throws \InvalidArgumentException
38
     */
39
    public function __construct($host)
40
    {
41
        if (!self::isValid($host)) {
42
            throw new \InvalidArgumentException(sprintf(
43
                'Argument "%s" is invalid. Allowed types for argument are "host name".',
44
                $host
45
            ));
46
        }
47
48
        parent::__construct($host);
49
    }
50
}
51