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&TModel |
||
26 | * @psalm-return Proxy<TModel> |
||
27 | */ |
||
28 | public function findOrCreate(array $attributes): Proxy |
||
29 | { |
||
30 | if ($found = $this->repository()->find($attributes)) { |
||
31 | return \is_array($found) ? $found[0] : $found; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
32 | } |
||
33 | |||
34 | return $this->create($attributes); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @see RepositoryProxy::first() |
||
39 | * |
||
40 | * @throws \RuntimeException If no entities exist |
||
41 | */ |
||
42 | public function first(string $sortedField = 'id'): Proxy |
||
43 | { |
||
44 | if (null === $proxy = $this->repository()->first($sortedField)) { |
||
45 | throw new \RuntimeException(\sprintf('No "%s" objects persisted.', $this->class())); |
||
46 | } |
||
47 | |||
48 | return $proxy; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @see RepositoryProxy::last() |
||
53 | * |
||
54 | * @throws \RuntimeException If no entities exist |
||
55 | */ |
||
56 | public function last(string $sortedField = 'id'): Proxy |
||
57 | { |
||
58 | if (null === $proxy = $this->repository()->last($sortedField)) { |
||
59 | throw new \RuntimeException(\sprintf('No "%s" objects persisted.', $this->class())); |
||
60 | } |
||
61 | |||
62 | return $proxy; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @see RepositoryProxy::random() |
||
67 | */ |
||
68 | public function random(array $attributes = []): Proxy |
||
69 | { |
||
70 | return $this->repository()->random($attributes); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Fetch one random object and create a new object if none exists. |
||
75 | * |
||
76 | * @return Proxy&TModel |
||
77 | * @psalm-return Proxy<TModel> |
||
78 | */ |
||
79 | public function randomOrCreate(array $attributes = []): Proxy |
||
80 | { |
||
81 | try { |
||
82 | return $this->repository()->random($attributes); |
||
83 | } catch (\RuntimeException $e) { |
||
84 | return $this->create($attributes); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @see RepositoryProxy::randomSet() |
||
90 | */ |
||
91 | public function randomSet(int $number, array $attributes = []): array |
||
92 | { |
||
93 | return $this->repository()->randomSet($number, $attributes); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @see RepositoryProxy::randomRange() |
||
98 | */ |
||
99 | public function randomRange(int $min, int $max, array $attributes = []): array |
||
100 | { |
||
101 | return $this->repository()->randomRange($min, $max, $attributes); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @see RepositoryProxy::count() |
||
106 | */ |
||
107 | public function count(): int |
||
108 | { |
||
109 | return $this->repository()->count(); |
||
110 | } |
||
111 | |||
112 | public function getIterator(): \Traversable |
||
113 | { |
||
114 | return new \ArrayIterator($this->all()); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @see RepositoryProxy::truncate() |
||
119 | */ |
||
120 | public function truncate(): void |
||
121 | { |
||
122 | $this->repository()->truncate(); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @see RepositoryProxy::findAll() |
||
127 | */ |
||
128 | public function all(): array |
||
129 | { |
||
130 | return $this->repository()->findAll(); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @see RepositoryProxy::find() |
||
135 | * |
||
136 | * @throws \RuntimeException If no entity found |
||
137 | */ |
||
138 | public function find($criteria): Proxy |
||
139 | { |
||
140 | if (null === $proxy = $this->repository()->find($criteria)) { |
||
0 ignored issues
–
show
|
|||
141 | throw new \RuntimeException(\sprintf('Could not find "%s" object.', $this->class())); |
||
142 | } |
||
143 | |||
144 | return $proxy; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @see RepositoryProxy::findBy() |
||
149 | */ |
||
150 | public function findBy(array $attributes): array |
||
151 | { |
||
152 | return $this->repository()->findBy($attributes); |
||
153 | } |
||
154 | |||
155 | public function assert(): RepositoryAssertions |
||
156 | { |
||
157 | return $this->repository()->assert(); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @psalm-return RepositoryProxy<TModel> |
||
162 | */ |
||
163 | public function repository(): RepositoryProxy |
||
164 | { |
||
165 | return self::configuration()->repositoryFor($this->class()); |
||
166 | } |
||
167 | } |
||
168 |