Skip to content

Commit

Permalink
Improve check of a type of field
Browse files Browse the repository at this point in the history
  • Loading branch information
idmarinas committed Dec 4, 2024
1 parent 3456235 commit 1a45fca
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Traits/Tool/FakerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Copyright 2023-2024 (C) IDMarinas - All Rights Reserved
*
* Last modified by "IDMarinas" on 03/12/2024, 21:33
* Last modified by "IDMarinas" on 04/12/2024, 14:39
*
* @project IDMarinas Common Bundle
* @see https://github.com/idmarinas/common-bundle
Expand All @@ -24,6 +24,7 @@
use Faker\Generator;
use LogicException;
use ReflectionClass;
use ReflectionException;
use ReflectionProperty;
use Symfony\Component\Uid\Uuid;

Expand Down Expand Up @@ -56,6 +57,9 @@ public function populateEntity (object $entity): object
return $entity;
}

/**
* @throws ReflectionException
*/
private function fakerValueFromProperty (ReflectionProperty $property): mixed
{
return match ($property->getName()) {
Expand All @@ -70,6 +74,9 @@ private function fakerValueFromProperty (ReflectionProperty $property): mixed
};
}

/**
* @throws ReflectionException
*/
private function fakerValueForType (ReflectionProperty $property): mixed
{
$attribute = $property->getAttributes(Column::class)[0];
Expand All @@ -78,6 +85,13 @@ private function fakerValueForType (ReflectionProperty $property): mixed
$type = $arguments['type'] ?? $property->getType()?->getName() ?: $property->getName();
$length = ($arguments['length'] ?? 10) - 0.9;

// Check if is a namespace
if (str_contains($type, '\\')) {
$ref = new ReflectionClass($type);
$type = $ref->isAbstract() ? 'continue' : 'object';
$object = $ref->isAbstract() ?: $ref->newInstance();
}

return match ($type) {
'text' => $this->faker()->text(),
'integer' => $this->faker()->randomNumber(327539810),
Expand All @@ -97,6 +111,8 @@ private function fakerValueForType (ReflectionProperty $property): mixed
'string' => $this->faker()->text($length),
'createdFromIp',
'updatedFromIp' => $this->faker()->ipv4(),
'continue' => 'continue',
'object' => $this->populateEntity($object),
default => $this->faker()->sentence()
};
}
Expand Down

0 comments on commit 1a45fca

Please sign in to comment.