Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of sebastian/code-unit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianBergmann\CodeUnit;
11
 
12
use function array_merge;
13
use function count;
14
use Countable;
15
use IteratorAggregate;
16
 
17
final class CodeUnitCollection implements Countable, IteratorAggregate
18
{
19
    /**
20
     * @psalm-var list<CodeUnit>
21
     */
22
    private $codeUnits = [];
23
 
24
    /**
25
     * @psalm-param list<CodeUnit> $items
26
     */
27
    public static function fromArray(array $items): self
28
    {
29
        $collection = new self;
30
 
31
        foreach ($items as $item) {
32
            $collection->add($item);
33
        }
34
 
35
        return $collection;
36
    }
37
 
38
    public static function fromList(CodeUnit ...$items): self
39
    {
40
        return self::fromArray($items);
41
    }
42
 
43
    private function __construct()
44
    {
45
    }
46
 
47
    /**
48
     * @psalm-return list<CodeUnit>
49
     */
50
    public function asArray(): array
51
    {
52
        return $this->codeUnits;
53
    }
54
 
55
    public function getIterator(): CodeUnitCollectionIterator
56
    {
57
        return new CodeUnitCollectionIterator($this);
58
    }
59
 
60
    public function count(): int
61
    {
62
        return count($this->codeUnits);
63
    }
64
 
65
    public function isEmpty(): bool
66
    {
67
        return empty($this->codeUnits);
68
    }
69
 
70
    public function mergeWith(self $other): self
71
    {
72
        return self::fromArray(
73
            array_merge(
74
                $this->asArray(),
75
                $other->asArray()
76
            )
77
        );
78
    }
79
 
80
    private function add(CodeUnit $item): void
81
    {
82
        $this->codeUnits[] = $item;
83
    }
84
}