Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add withCount support #920

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Attributes/ResponseFromApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct(
public ?int $paginate = null,
public ?int $simplePaginate = null,
public array $additional = [],
public array $withCount = [],
SergeAntonets marked this conversation as resolved.
Show resolved Hide resolved
)
{
}
Expand Down
14 changes: 9 additions & 5 deletions src/Extracting/InstantiatesExampleModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ trait InstantiatesExampleModels
*/
protected function instantiateExampleModel(
?string $type = null, array $factoryStates = [],
array $relations = [], ?ReflectionFunctionAbstract $transformationMethod = null
array $relations = [], ?ReflectionFunctionAbstract $transformationMethod = null, array $withCount = [],
)
{
// If the API Resource uses an empty resource, there won't be an example model
Expand All @@ -42,7 +42,7 @@ protected function instantiateExampleModel(
$configuredStrategies = $this->config->get('examples.models_source', ['factoryCreate', 'factoryMake', 'databaseFirst']);

$strategies = [
'factoryCreate' => fn() => $this->getExampleModelFromFactoryCreate($type, $factoryStates, $relations),
'factoryCreate' => fn() => $this->getExampleModelFromFactoryCreate($type, $factoryStates, $relations, $withCount),
'factoryMake' => fn() => $this->getExampleModelFromFactoryMake($type, $factoryStates, $relations),
'databaseFirst' => fn() => $this->getExampleModelFromDatabaseFirst($type, $relations),
];
Expand All @@ -63,13 +63,17 @@ protected function instantiateExampleModel(
/**
* @param class-string $type
* @param string[] $factoryStates
* @param string[] $relations
* @param string[] $withCount
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
protected function getExampleModelFromFactoryCreate(string $type, array $factoryStates = [], array $relations = [])
protected function getExampleModelFromFactoryCreate(string $type, array $factoryStates = [], array $relations = [], array $withCount = [])
{
$factory = Utils::getModelFactory($type, $factoryStates, $relations);
return $factory->create()->refresh()->load($relations);
$mergeAll = array_unique(array_merge($relations, $withCount));

$factory = Utils::getModelFactory($type, $factoryStates, $mergeAll);
return $factory->create()->refresh()->load($relations)->loadCount($withCount);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function getApiResourceResponse(ResponseFromApiResource $attributeInst
);
$modelInstantiator = null;
} else {
$modelInstantiator = fn() => $this->instantiateExampleModel($modelToBeTransformed, $attributeInstance->factoryStates, $attributeInstance->with);
$modelInstantiator = fn() => $this->instantiateExampleModel($modelToBeTransformed, $attributeInstance->factoryStates, $attributeInstance->with, null, $attributeInstance->withCount);
}

$pagination = [];
Expand Down