Skip to content

Commit

Permalink
Fix localization issue
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Aug 30, 2024
1 parent 5fd4b11 commit 59ca67f
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function make(array $attributes = []): Collection|Entry|Term
}

if ($this->count === null) {
return tap($this->makeInstance(), function ($instance) {
return tap($this->state($attributes)->makeInstance(), function ($instance) {
$this->callAfterMaking(collect([$instance]));
});
}
Expand All @@ -92,10 +92,13 @@ public function make(array $attributes = []): Collection|Entry|Term
return collect();
}

return collect()
->range(1, $this->count)
->map(fn () => $this->makeInstance())
->tap(fn ($instances) => $this->callAfterMaking($instances));
$instances = collect(array_map(function () use ($attributes) {
return $this->state($attributes)->makeInstance();
}, range(1, $this->count)));

$this->callAfterMaking($instances);

return $instances;
}

public function createOne($attributes = []): Entry|Term
Expand Down Expand Up @@ -161,7 +164,7 @@ protected function getExpandedAttributes(): array
protected function getRawAttributes(): array
{
return $this->states
->tap($this->setFakerLocaleAccordingToSite(...))
->pipe($this->getSiteFromStatesAndSetFakerLocaleAccordingly(...))
->reduce(function (array $carry, $state) {
if ($state instanceof Closure) {
$state = $state->bindTo($this);
Expand All @@ -171,15 +174,26 @@ protected function getRawAttributes(): array
}, $this->definition());
}

protected function setFakerLocaleAccordingToSite(): void
protected function getSiteFromStatesAndSetFakerLocaleAccordingly(Collection $states): Collection
{
$site = $this->states
->flatMap(fn ($state) => (clone $state)())
->get('site');
$evaluatedClosure = $states
->map(fn ($state) => (clone $state)()) /* Clone the closure so that we don't run into issues when evaluating the same closure later. Needed for sequences to work correctly. */
->filter(fn ($state) => isset($state['site']))
->flatMap(fn ($state, $index) => array_merge(['index' => $index], $state));

$site = $evaluatedClosure->get('site');

/**
* Replace the unevaluated site closure with one that contains the evaluated site.
* This ensures that we'll save the content in the same site that we are using for Faker.
*/
$states = $states->put($evaluatedClosure->get('index'), fn () => ['site' => $site]);

$locale = Site::get($site)?->locale() ?? Site::default()->locale();

$this->faker = $this->withFaker($locale);

return $states;
}

protected function expandAttributes(array $definition)
Expand Down

0 comments on commit 59ca67f

Please sign in to comment.