Passed
Push — 2.x ( d7bcf9...40bb65 )
by Aleksei
36:03 queued 16:02
created

LoophpCollectionFactory::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Collection;
6
7
use Cycle\ORM\Exception\CollectionFactoryException;
8
use loophp\collection\Collection;
9
10
/**
11
 * @template TCollection of Collection
12
 * @template-implements CollectionFactoryInterface<TCollection>
13
 */
14
final class LoophpCollectionFactory implements CollectionFactoryInterface
15
{
16
    /** @var class-string<TCollection> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<TCollection> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<TCollection>.
Loading history...
17
    private string $class = Collection::class;
18
19
    public function __construct()
20
    {
21
        if (!\class_exists(Collection::class, true)) {
22
            throw new CollectionFactoryException(
23
                \sprintf(
24
                    'There is no %s class. To resolve this issue you can install `loophp/collection` package.',
25
                    Collection::class
26
                )
27
            );
28
        }
29
    }
30
31
    public function getInterface(): string
32
    {
33
        return Collection::class;
34
    }
35
36
    public function withCollectionClass(string $class): static
37
    {
38
        if ($class !== Collection::class) {
39
            throw new CollectionFactoryException(\sprintf(
40
                'Unsupported collection class `%s`.',
41
                $class
42
            ));
43
        }
44
        return clone $this;
45
    }
46
47
    public function collect(iterable $data): Collection
48
    {
49
        return $this->class::fromIterable($data);
50
    }
51
}
52