PHP code example of torqnorth / object-generator-bundle
1. Go to this page and download the library: Download torqnorth/object-generator-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
torqnorth / object-generator-bundle example snippets
// define your DTO
class MyDto {
public ?int $id;
public ?string $name;
...
}
// Inside a command, or whatever script you use to generate your data
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use TorqNorth\ObjectGeneratorBundle\Service\FakeObjectFactory\FakeObjectFactory;
class GenerateDataCommand extends Command {
// dependency inject the factory
public function __construct(private FakeObjectFactory $fakeObjectFactory) {}
public function execute(InputInterface $input, OutputInterface $output)
{
// optionally define your config (see FakeObjectFactory::DEFAULT_CONFIG)
$config = ['useConstructor' => false];
// generate your dto
$dto = $this->fakeObjectFactory->generateValue(MyDto::class, $config);
// do whatever with the generated data, likely create a data object with it.
$object = new DataObject();
$object->setId($dto->id);
$object->setName($dto->name);
return self::SUCCESS;
}
}
// You can override the automatic type checking by using #[GenerateAs]
use TorqNorth\ObjectGeneratorBundle\Service\FakeObjectFactory\GenerateAs;
use TorqNorth\ObjectGeneratorBundle\Service\DataGenerator\NameGenerator;
class AnotherDto {
public ?int $id;
// pass the class of the generator you want to use
#[GenerateAs(NameGenerator::class)]
public ?string $name;
}
use TorqNorth\ObjectGeneratorBundle\Service\DataGenerator\AbstractDataGenerator;
use TorqNorth\ObjectGeneratorBundle\Service\DataGenerator\DataGeneratorInterface;
// Recommend extending AbstractDataGenerator to get utilities
class MyEmojiGenerator extends AbstractDataGenerator implements DataGeneratorInterface {
// $config is passed through from factory->generateValue, maps to faker fn arguments
public function generateValue(string $type, ?string $name = null, array $config = [])
{
// getConfigParser convenience utility comes from AbstractDataGenerator
$parser = $this->getConfigParser($config);
return $this->faker->emoji(...$parser('emoji'));
}
// Used to finely filter out generators per parameter during generation
public function supportsGeneration(string $type, ?string $name = null): bool
{
return $type === 'string';
}
// Used to broadly filter generators prior to generation
public function getSupportedTypes(): array
{
// Expected types found here: https://www.php.net/manual/en/reflectionnamedtype.getname.php
return ['string'];
}
// Used to pick which generator from those where supportsGeneration is true and getSupportedTypes matches
public function getPriority(): int
{
// default is 0, use positive values to increase priority, negative to decrease
return 0;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.