Completed
Pull Request — master (#15)
by James
02:25
created

Changes::withAddedChange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Roave\ApiCompare;
5
6
use ArrayIterator;
7
use Assert\Assert;
8
use IteratorAggregate;
9
10
final class Changes implements IteratorAggregate
11
{
12
    /** @var Change[] */
13
    private $changes = [];
14
15
    private function __construct()
16
    {
17
    }
18
19
    public static function new(): self
20
    {
21
        return new self();
22
    }
23
24
    public static function fromArray(array $changes): self
25
    {
26
        Assert::that($changes)->all()->isInstanceOf(Change::class);
27
        $instance = self::new();
28
        $instance->changes = $changes;
29
        return $instance;
30
    }
31
32
    public function withAddedChange(Change $change): self
33
    {
34
        $new = clone $this;
35
        $new->changes[] = $change;
36
        return $new;
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function getIterator() : ArrayIterator
43
    {
44
        return new ArrayIterator($this->changes);
45
    }
46
}
47