mirror of
https://github.com/fzaninotto/Faker.git
synced 2025-03-23 00:39:47 +01:00
added Mandango populator
This commit is contained in:
parent
94a289c020
commit
45e4112bbb
35
src/Faker/ORM/Mandango/ColumnTypeGuesser.php
Normal file
35
src/Faker/ORM/Mandango/ColumnTypeGuesser.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Faker\ORM\Mandango;
|
||||
|
||||
class ColumnTypeGuesser
|
||||
{
|
||||
protected $generator;
|
||||
|
||||
public function __construct(\Faker\Generator $generator)
|
||||
{
|
||||
$this->generator = $generator;
|
||||
}
|
||||
|
||||
public function guessFormat($field)
|
||||
{
|
||||
$generator = $this->generator;
|
||||
switch ($field['type']) {
|
||||
case 'boolean':
|
||||
return function() use ($generator) { return $generator->boolean; };
|
||||
case 'integer':
|
||||
return function() { return mt_rand(0,4294967295); };
|
||||
case 'float':
|
||||
return function() { return mt_rand(0,4294967295)/mt_rand(1,4294967295); };
|
||||
case 'string':
|
||||
return function() use ($generator) { return $generator->text(255); };
|
||||
case 'string':
|
||||
return function() use ($generator) { return $generator->text; };
|
||||
case 'date':
|
||||
return function() use ($generator) { return $generator->datetime; };
|
||||
default:
|
||||
// no smart way to guess what the user expects here
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
112
src/Faker/ORM/Mandango/EntityPopulator.php
Normal file
112
src/Faker/ORM/Mandango/EntityPopulator.php
Normal file
@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace Faker\ORM\Mandango;
|
||||
|
||||
use Mandango\Mandango;
|
||||
|
||||
/**
|
||||
* Service class for populating a table through a Mandango ActiveRecord class.
|
||||
*/
|
||||
class EntityPopulator
|
||||
{
|
||||
protected $class;
|
||||
protected $columnFormatters = array();
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param string $class A Mandango ActiveRecord classname
|
||||
*/
|
||||
public function __construct($class)
|
||||
{
|
||||
$this->class = $class;
|
||||
}
|
||||
|
||||
public function getClass()
|
||||
{
|
||||
return $this->class;
|
||||
}
|
||||
|
||||
public function setColumnFormatters($columnFormatters)
|
||||
{
|
||||
$this->columnFormatters = $columnFormatters;
|
||||
}
|
||||
|
||||
public function getColumnFormatters()
|
||||
{
|
||||
return $this->columnFormatters;
|
||||
}
|
||||
|
||||
public function mergeColumnFormattersWith($columnFormatters)
|
||||
{
|
||||
$this->columnFormatters = array_merge($this->columnFormatters, $columnFormatters);
|
||||
}
|
||||
|
||||
public function guessColumnFormatters(\Faker\Generator $generator, Mandango $mandango)
|
||||
{
|
||||
$formatters = array();
|
||||
$nameGuesser = new \Faker\Guesser\Name($generator);
|
||||
$columnTypeGuesser = new \Faker\ORM\Mandango\ColumnTypeGuesser($generator);
|
||||
|
||||
$metadata = $mandango->getMetadata($this->class);
|
||||
|
||||
// fields
|
||||
foreach ($metadata['fields'] as $fieldName => $field) {
|
||||
if ($formatter = $nameGuesser->guessFormat($fieldName)) {
|
||||
$formatters[$fieldName] = $formatter;
|
||||
continue;
|
||||
}
|
||||
if ($formatter = $columnTypeGuesser->guessFormat($field)) {
|
||||
$formatters[$fieldName] = $formatter;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// references
|
||||
foreach (array_merge($metadata['referencesOne'], $metadata['referencesMany']) as $referenceName => $reference) {
|
||||
if (!isset($reference['class'])) {
|
||||
continue;
|
||||
}
|
||||
$referenceClass = $reference['class'];
|
||||
|
||||
$formatters[$referenceName] = function ($insertedEntities) use ($referenceClass) {
|
||||
if (isset($insertedEntities[$referenceClass])) {
|
||||
return $insertedEntities[$referenceClass][array_rand($insertedEntities[$referenceClass])];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return $formatters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert one new record using the Entity class.
|
||||
*/
|
||||
public function execute(Mandango $mandango, $insertedEntities)
|
||||
{
|
||||
$metadata = $mandango->getMetadata($this->class);
|
||||
|
||||
$obj = $mandango->create($this->class);
|
||||
foreach ($this->columnFormatters as $column => $format) {
|
||||
if (null !== $format) {
|
||||
$value = is_callable($format) ? $format($insertedEntities, $obj) : $format;
|
||||
|
||||
if (
|
||||
isset($metadata['fields'][$column])
|
||||
||
|
||||
isset($metadata['referencesOne'][$column])
|
||||
) {
|
||||
$obj->set($column, $value);
|
||||
}
|
||||
|
||||
if (isset($metadata['referencesMany'][$column])) {
|
||||
$adder = 'add'.ucfirst($column);
|
||||
$obj->$adder($value);
|
||||
}
|
||||
}
|
||||
}
|
||||
$mandango->persist($obj);
|
||||
|
||||
return $obj;
|
||||
}
|
||||
}
|
61
src/Faker/ORM/Mandango/Populator.php
Normal file
61
src/Faker/ORM/Mandango/Populator.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Faker\ORM\Mandango;
|
||||
|
||||
use Mandango\Mandango;
|
||||
|
||||
/**
|
||||
* Service class for populating a database using Mandango.
|
||||
* A Populator can populate several tables using ActiveRecord classes.
|
||||
*/
|
||||
class Populator
|
||||
{
|
||||
protected $generator;
|
||||
protected $mandango;
|
||||
protected $entities = array();
|
||||
protected $quantities = array();
|
||||
|
||||
public function __construct(\Faker\Generator $generator, Mandango $mandango)
|
||||
{
|
||||
$this->generator = $generator;
|
||||
$this->mandango = $mandango;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an order for the generation of $number records for $entity.
|
||||
*
|
||||
* @param mixed $entity A Propel ActiveRecord classname, or a \Faker\ORM\Propel\EntityPopulator instance
|
||||
* @param int $number The number of entities to populate
|
||||
*/
|
||||
public function addEntity($entity, $number, $customColumnFormatters = array())
|
||||
{
|
||||
if (!$entity instanceof \Faker\ORM\Mandango\EntityPopulator) {
|
||||
$entity = new \Faker\ORM\Mandango\EntityPopulator($entity);
|
||||
}
|
||||
$entity->setColumnFormatters($entity->guessColumnFormatters($this->generator, $this->mandango));
|
||||
if ($customColumnFormatters) {
|
||||
$entity->mergeColumnFormattersWith($customColumnFormatters);
|
||||
}
|
||||
$class = $entity->getClass();
|
||||
$this->entities[$class] = $entity;
|
||||
$this->quantities[$class] = $number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate the database using all the Entity classes previously added.
|
||||
*
|
||||
* @return array A list of the inserted entities.
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$insertedEntities = array();
|
||||
foreach ($this->quantities as $class => $number) {
|
||||
for ($i=0; $i < $number; $i++) {
|
||||
$insertedEntities[$class][]= $this->entities[$class]->execute($this->mandango, $insertedEntities);
|
||||
}
|
||||
}
|
||||
$this->mandango->flush();
|
||||
|
||||
return $insertedEntities;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user