1 | <?php |
||
2 | |||
3 | namespace Zenstruck\Foundry; |
||
4 | |||
5 | /** |
||
6 | * @template TObject of object |
||
7 | * |
||
8 | * @author Kevin Bond <[email protected]> |
||
9 | */ |
||
10 | final class FactoryCollection |
||
11 | { |
||
12 | /** @var Factory<TObject> */ |
||
13 | private $factory; |
||
14 | |||
15 | /** @var int */ |
||
16 | private $min; |
||
17 | |||
18 | /** @var int */ |
||
19 | private $max; |
||
20 | |||
21 | /** |
||
22 | * @param int|null $max If set, when created, the collection will be a random size between $min and $max |
||
23 | * |
||
24 | * @psalm-param Factory<TObject> $factory |
||
25 | */ |
||
26 | 270 | public function __construct(Factory $factory, int $min, ?int $max = null) |
|
27 | { |
||
28 | 270 | if ($max && $min > $max) { |
|
0 ignored issues
–
show
|
|||
29 | 10 | throw new \InvalidArgumentException('Min must be less than max.'); |
|
30 | } |
||
31 | |||
32 | 260 | $this->factory = $factory; |
|
33 | 260 | $this->min = $min; |
|
34 | 260 | $this->max = $max ?? $min; |
|
35 | 260 | } |
|
36 | |||
37 | /** |
||
38 | * @param array|callable $attributes |
||
39 | * |
||
40 | * @return list<TObject&Proxy<TObject>> |
||
0 ignored issues
–
show
The type
Zenstruck\Foundry\list was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
41 | * |
||
42 | * @psalm-suppress InvalidReturnType |
||
43 | * @psalm-return list<Proxy<TObject>> |
||
44 | */ |
||
45 | 220 | public function create($attributes = []): array |
|
46 | { |
||
47 | 220 | return \array_map( |
|
0 ignored issues
–
show
|
|||
48 | static function(Factory $factory) use ($attributes) { |
||
49 | 220 | return $factory->create($attributes); |
|
50 | 220 | }, |
|
51 | 220 | $this->all() |
|
52 | ); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @return Factory[] |
||
57 | * |
||
58 | * @psalm-return list<Factory<TObject>> |
||
59 | */ |
||
60 | 260 | public function all(): array |
|
61 | { |
||
62 | /** @psalm-suppress TooManyArguments */ |
||
63 | 260 | return \array_map( |
|
64 | function() { |
||
65 | 260 | return clone $this->factory; |
|
66 | 260 | }, |
|
67 | 260 | \array_fill(0, \random_int($this->min, $this->max), null) |
|
68 | ); |
||
69 | } |
||
70 | |||
71 | 50 | public function factory(): Factory |
|
72 | { |
||
73 | 50 | return $this->factory; |
|
74 | } |
||
75 | } |
||
76 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: