Completed
Push — 1.1 ( a4c925...1876b7 )
by Daniel
07:54 queued 04:30
created

WildcardDomainField::checkHostname()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * A text field that accepts only valid domain names, but allows the wildcard (*) character
5
 */
6
class WildcardDomainField extends TextField
7
{
8
    /**
9
     * Validate this field as a valid hostname
10
     *
11
     * @param Validator $validator
12
     * @return bool
13
     */
14
    public function validate($validator)
15
    {
16
        if ($this->checkHostname($this->Value())) {
17
            return true;
18
        }
19
20
        $validator->validationError(
21
            $this->getName(),
22
            _t("DomainNameField.INVALID_DOMAIN", "Invalid domain name"),
23
            "validation"
24
        );
25
        return false;
26
    }
27
28
    /**
29
     * Check if the given hostname is valid.
30
     *
31
     * @param string $hostname
32
     * @return bool True if this hostname is valid
33
     */
34
    public function checkHostname($hostname)
35
    {
36
        return (bool)preg_match('/^([a-z0-9\*]+[\-\.])*([a-z0-9\*]+)$/', $hostname);
37
    }
38
39
    public function Type()
40
    {
41
        return 'text wildcarddomain';
42
    }
43
}
44