HostnameCollection   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 3 1
A count() 0 3 1
A toString() 0 3 1
A toLocal() 0 8 2
A fromScalarArray() 0 8 1
A __construct() 0 4 1
A fromString() 0 3 1
A __toString() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Cocotte\DigitalOcean;
4
5
use ArrayIterator;
6
use Assert\Assertion;
7
use IteratorAggregate;
8
9
final class HostnameCollection implements IteratorAggregate, \Countable
10
{
11
    private $hostnames;
12
13
    public function __construct(Hostname ...$hostnames)
14
    {
15
        Assertion::greaterThan($hostnames, 0, "There is no hostname");
16
        $this->hostnames = $hostnames;
17
    }
18
19
    public static function fromScalarArray(array $value): self
20
    {
21
        return new self(
22
            ...array_map(
23
                function (string $host) {
24
                    return Hostname::parse($host);
25
                },
26
                $value
27
            )
28
        );
29
    }
30
31
    public static function fromString(string $string): self
32
    {
33
        return self::fromScalarArray(array_map('trim', explode(',', $string)));
34
    }
35
36
    public function toString(): string
37
    {
38
        return implode(',', $this->hostnames);
39
    }
40
41
    public function __toString()
42
    {
43
        return $this->toString();
44
    }
45
46
    public function toLocal(): HostnameCollection
47
    {
48
        $localHosts = [];
49
        foreach ($this->hostnames as $value) {
50
            $localHosts[] = $value->toLocal();
51
        }
52
53
        return new self(...$localHosts);
54
    }
55
56
    public function getIterator()
57
    {
58
        return new ArrayIterator($this->hostnames);
59
    }
60
61
    public function count()
62
    {
63
        return count($this->hostnames);
64
    }
65
66
}
67