Completed
Push — master ( 539390...056db3 )
by Günter
02:42
created

AddrTrait::set()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
nc 1
1
<?php
2
3
/**
4
 * This file is part of the php-epp2 library.
5
 *
6
 * (c) Gunter Grodotzki <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE file
9
 * that was distributed with this source code.
10
 */
11
12
namespace AfriCC\EPP;
13
14
use Exception;
15
16
/**
17
 * a IP address trait to give common functionality needed to for addr elements
18
 */
19
trait AddrTrait
20
{
21
    protected $host_attr_index = 0;
22
23
    abstract public function set($path = null, $value = null);
24
25
    protected function appendAddr($path, $ip)
26
    {
27
        // validate IP
28
        $ip_type = Validator::getIPType($ip);
29
        if ($ip_type === false) {
30
            throw new Exception(sprintf('%s is not a valid IP address', $ip));
31
        }
32
33
        $node = $this->set($path, $ip);
34
35
        if ($ip_type === Validator::TYPE_IPV4) {
36
            $node->setAttribute('ip', 'v4');
37
        } elseif ($ip_type === Validator::TYPE_IPV6) {
38
            $node->setAttribute('ip', 'v6');
39
        }
40
    }
41
42
    protected function appendHostAttr($base_path, $host, $ips = null)
43
    {
44
        if (!Validator::isHostname($host)) {
45
            throw new Exception(sprintf('%s is not a valid host name', $host));
46
        }
47
48
        $base_path = sprintf($base_path, $this->host_attr_index);
49
50
        $this->set($base_path . '/domain:hostName', $host);
51
52
        if (!empty($ips) && is_array($ips)) {
53
            foreach ($ips as $ip) {
54
                $ip_type = Validator::getIPType($ip);
55
                if ($ip_type === false) {
56
                    throw new Exception(sprintf('%s is not a valid IP address', $ip));
57
                } elseif ($ip_type === Validator::TYPE_IPV4) {
58
                    $this->set($base_path . '/domain:hostAddr[@ip=\'v4\']', $ip);
59
                } elseif ($ip_type === Validator::TYPE_IPV6) {
60
                    $this->set($base_path . '/domain:hostAddr[@ip=\'v6\']', $ip);
61
                }
62
            }
63
        }
64
65
        ++$this->host_attr_index;
66
    }
67
}
68