HostnameCollection::toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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