HostName::isValid()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 3
eloc 4
nc 3
nop 1
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