Passed
Pull Request — 1.0.0 (#2)
by
unknown
08:36
created

StringCollection::assertAllStrings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
4
namespace StraTDeS\VO\Collection;
5
6
7
use InvalidArgumentException;
8
9
class StringCollection
10
{
11
    /** @var string[] */
12
    private array $array;
13
14
    private function __construct(array $array)
15
    {
16
        $this->assertAllStrings($array);
17
18
        $this->array = $array;
19
    }
20
21
    public static function fromArray(array $array): self
22
    {
23
        return new static($array);
24
    }
25
26
    public function toArray(): array
27
    {
28
        return $this->array;
29
    }
30
31
    private function assertAllStrings(array $array): void
32
    {
33
        foreach ($array as $item) {
34
            if (is_string($item) !== true) {
35
                throw new InvalidArgumentException("The element $item in the array is not a string");
36
            }
37
        }
38
    }
39
}