-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathFactoryCollection.php
More file actions
65 lines (56 loc) · 1.37 KB
/
FactoryCollection.php
File metadata and controls
65 lines (56 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace Zenstruck\Foundry;
/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
final class FactoryCollection
{
/** @var Factory */
private $factory;
/** @var int */
private $min;
/** @var int */
private $max;
/**
* @param int|null $max If set, when created, the collection will be a random size between $min and $max
*/
public function __construct(Factory $factory, int $min, ?int $max = null)
{
if ($max && $min > $max) {
throw new \InvalidArgumentException('Min must be less than max.');
}
$this->factory = $factory;
$this->min = $min;
$this->max = $max ?? $min;
}
/**
* @param array|callable $attributes
*
* @return Proxy[]|object[]
*/
public function create($attributes = []): array
{
return \array_map(
static function(Factory $factory) use ($attributes) {
return $factory->create($attributes);
},
$this->all()
);
}
/**
* @return Factory[]
*/
public function all(): array
{
return \array_map(
function() {
return clone $this->factory;
},
\array_fill(0, \random_int($this->min, $this->max), null)
);
}
public function factory(): Factory
{
return $this->factory;
}
}