Passed
Pull Request — master (#38)
by Kevin
17:07
created

FactoryCollection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 8
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Zenstruck\Foundry;
4
5
/**
6
 * @author Kevin Bond <[email protected]>
7
 */
8
final class FactoryCollection
9
{
10
    /** @var Factory */
11
    private $factory;
12
13
    /** @var int */
14
    private $count;
15
16
    public function __construct(Factory $factory, int $count)
17
    {
18
        if ($count < 1) {
19
            throw new \InvalidArgumentException('Count must be greater than 0.');
20
        }
21
22
        $this->factory = $factory;
23
        $this->count = $count;
24
    }
25
26
    /**
27
     * @param array|callable $attributes
28
     *
29
     * @return Proxy[]|object[]
30
     */
31
    public function create($attributes = []): array
32
    {
33
        return $this->factory->createMany($this->count, $attributes);
34
    }
35
36
    /**
37
     * @return Factory[]
38
     */
39
    public function all(): array
40
    {
41
        return \array_map(
42
            function() {
43
                return clone $this->factory;
44
            },
45
            \array_fill(0, $this->count, null)
46
        );
47
    }
48
49
    public function factory(): Factory
50
    {
51
        return $this->factory;
52
    }
53
}
54