1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @template TModel of object |
7
|
|
|
* @template-extends Factory<TModel> |
8
|
|
|
* |
9
|
|
|
* @author Kevin Bond <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
final class AnonymousFactory extends Factory implements \Countable, \IteratorAggregate |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @see Factory::__construct() |
15
|
|
|
*/ |
16
|
|
|
public static function new(string $class, $defaultAttributes = []): self |
17
|
|
|
{ |
18
|
|
|
return new self($class, $defaultAttributes); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Try and find existing object for the given $attributes. If not found, |
23
|
|
|
* instantiate and persist. |
24
|
|
|
* |
25
|
|
|
* @return Proxy|object |
26
|
|
|
* |
27
|
|
|
* @psalm-return Proxy<TModel> |
28
|
|
|
*/ |
29
|
|
|
public function findOrCreate(array $attributes): Proxy |
30
|
|
|
{ |
31
|
|
|
if ($found = $this->repository()->find($attributes)) { |
32
|
|
|
return \is_array($found) ? $found[0] : $found; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $this->create($attributes); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @see RepositoryProxy::first() |
40
|
|
|
* |
41
|
|
|
* @throws \RuntimeException If no entities exist |
42
|
|
|
*/ |
43
|
|
|
public function first(string $sortedField = 'id'): Proxy |
44
|
|
|
{ |
45
|
|
|
if (null === $proxy = $this->repository()->first($sortedField)) { |
46
|
|
|
throw new \RuntimeException(\sprintf('No "%s" objects persisted.', $this->class())); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $proxy; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @see RepositoryProxy::last() |
54
|
|
|
* |
55
|
|
|
* @throws \RuntimeException If no entities exist |
56
|
|
|
*/ |
57
|
|
|
public function last(string $sortedField = 'id'): Proxy |
58
|
|
|
{ |
59
|
|
|
if (null === $proxy = $this->repository()->last($sortedField)) { |
60
|
|
|
throw new \RuntimeException(\sprintf('No "%s" objects persisted.', $this->class())); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $proxy; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @see RepositoryProxy::random() |
68
|
|
|
*/ |
69
|
|
|
public function random(array $attributes = []): Proxy |
70
|
|
|
{ |
71
|
|
|
return $this->repository()->random($attributes); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Fetch one random object and create a new object if none exists. |
76
|
|
|
* |
77
|
|
|
* @return Proxy|object |
78
|
|
|
* |
79
|
|
|
* @psalm-return Proxy<TModel> |
80
|
|
|
*/ |
81
|
|
|
public function randomOrCreate(array $attributes = []): Proxy |
82
|
|
|
{ |
83
|
|
|
try { |
84
|
|
|
return $this->repository()->random($attributes); |
85
|
|
|
} catch (\RuntimeException $e) { |
86
|
|
|
return $this->create($attributes); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @see RepositoryProxy::randomSet() |
92
|
|
|
*/ |
93
|
|
|
public function randomSet(int $number, array $attributes = []): array |
94
|
|
|
{ |
95
|
|
|
return $this->repository()->randomSet($number, $attributes); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @see RepositoryProxy::randomRange() |
100
|
|
|
*/ |
101
|
|
|
public function randomRange(int $min, int $max, array $attributes = []): array |
102
|
|
|
{ |
103
|
|
|
return $this->repository()->randomRange($min, $max, $attributes); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @see RepositoryProxy::count() |
108
|
|
|
*/ |
109
|
|
|
public function count(): int |
110
|
|
|
{ |
111
|
|
|
return $this->repository()->count(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getIterator(): \Traversable |
115
|
|
|
{ |
116
|
|
|
return new \ArrayIterator($this->all()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @see RepositoryProxy::truncate() |
121
|
|
|
*/ |
122
|
|
|
public function truncate(): void |
123
|
|
|
{ |
124
|
|
|
$this->repository()->truncate(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @see RepositoryProxy::findAll() |
129
|
|
|
*/ |
130
|
|
|
public function all(): array |
131
|
|
|
{ |
132
|
|
|
return $this->repository()->findAll(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @see RepositoryProxy::find() |
137
|
|
|
* |
138
|
|
|
* @throws \RuntimeException If no entity found |
139
|
|
|
*/ |
140
|
|
|
public function find($criteria): Proxy |
141
|
|
|
{ |
142
|
|
|
if (null === $proxy = $this->repository()->find($criteria)) { |
143
|
|
|
throw new \RuntimeException(\sprintf('Could not find "%s" object.', $this->class())); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $proxy; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @see RepositoryProxy::findBy() |
151
|
|
|
*/ |
152
|
|
|
public function findBy(array $attributes): array |
153
|
|
|
{ |
154
|
|
|
return $this->repository()->findBy($attributes); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function assert(): RepositoryAssertions |
158
|
|
|
{ |
159
|
|
|
return $this->repository()->assert(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @psalm-return RepositoryProxy<TModel> |
164
|
|
|
*/ |
165
|
|
|
public function repository(): RepositoryProxy |
166
|
|
|
{ |
167
|
|
|
return self::configuration()->repositoryFor($this->class()); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|