示例#1
0
 /**
  * @param \Faker\Generator $generator
  * @param Mandango $mandango
  * @return array
  */
 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 Base::randomElement($insertedEntities[$referenceClass]);
             }
         };
     }
     return $formatters;
 }
 /**
  * @param \Faker\Generator $generator
  * @return array
  */
 public function guessColumnFormatters(\Faker\Generator $generator)
 {
     $formatters = array();
     $class = $this->class;
     $peerClass = $class::PEER;
     $tableMap = $peerClass::getTableMap();
     $nameGuesser = new \Faker\Guesser\Name($generator);
     $columnTypeGuesser = new \Faker\ORM\Propel\ColumnTypeGuesser($generator);
     foreach ($tableMap->getColumns() as $columnMap) {
         // skip behavior columns, handled by modifiers
         if ($this->isColumnBehavior($columnMap)) {
             continue;
         }
         if ($columnMap->isForeignKey()) {
             $relatedClass = $columnMap->getRelation()->getForeignTable()->getClassname();
             $formatters[$columnMap->getPhpName()] = function ($inserted) use($relatedClass) {
                 return isset($inserted[$relatedClass]) ? $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)] : null;
             };
             continue;
         }
         if ($columnMap->isPrimaryKey()) {
             continue;
         }
         if ($formatter = $nameGuesser->guessFormat($columnMap->getPhpName(), $columnMap->getSize())) {
             $formatters[$columnMap->getPhpName()] = $formatter;
             continue;
         }
         if ($formatter = $columnTypeGuesser->guessFormat($columnMap)) {
             $formatters[$columnMap->getPhpName()] = $formatter;
             continue;
         }
     }
     return $formatters;
 }
示例#3
0
 public function guessColumnFormatters(\Faker\Generator $generator)
 {
     $formatters = array();
     $class = $this->class;
     $nameGuesser = new \Faker\Guesser\Name($generator);
     $columnTypeGuesser = new ColumnTypeGuesser($generator);
     foreach ($this->class->getFieldNames() as $fieldName) {
         if ($this->class->isIdentifier($fieldName) || !$this->class->hasField($fieldName)) {
             continue;
         }
         if ($formatter = $nameGuesser->guessFormat($fieldName)) {
             $formatters[$fieldName] = $formatter;
             continue;
         }
         if ($formatter = $columnTypeGuesser->guessFormat($fieldName, $this->class)) {
             $formatters[$fieldName] = $formatter;
             continue;
         }
     }
     foreach ($this->class->getAssociationNames() as $assocName) {
         if (!$this->class->isIdentifier($assocName) || !$this->class->isCollectionValuedAssociation($assocName)) {
             continue;
         }
         $relatedClass = $this->class->getAssociationTargetClass($assocName);
         $formatters[$assocName] = function ($inserted) use($relatedClass) {
             return isset($inserted[$relatedClass]) ? $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)] : null;
         };
     }
     return $formatters;
 }
示例#4
0
 /**
  * @param \Faker\Generator $generator
  * @return array
  */
 public function guessColumnFormatters(\Faker\Generator $generator)
 {
     $formatters = array();
     $nameGuesser = new \Faker\Guesser\Name($generator);
     $columnTypeGuesser = new ColumnTypeGuesser($generator);
     foreach ($this->class->getFieldNames() as $fieldName) {
         if ($this->class->isIdentifier($fieldName) || !$this->class->hasField($fieldName)) {
             continue;
         }
         $size = isset($this->class->fieldMappings[$fieldName]['length']) ? $this->class->fieldMappings[$fieldName]['length'] : null;
         if ($formatter = $nameGuesser->guessFormat($fieldName, $size)) {
             $formatters[$fieldName] = $formatter;
             continue;
         }
         if ($formatter = $columnTypeGuesser->guessFormat($fieldName, $this->class)) {
             $formatters[$fieldName] = $formatter;
             continue;
         }
     }
     foreach ($this->class->getAssociationNames() as $assocName) {
         if ($this->class->isCollectionValuedAssociation($assocName)) {
             continue;
         }
         $relatedClass = $this->class->getAssociationTargetClass($assocName);
         $unique = $optional = false;
         $mappings = $this->class->getAssociationMappings();
         foreach ($mappings as $mapping) {
             if ($mapping['targetEntity'] == $relatedClass) {
                 if ($mapping['type'] == ClassMetadata::ONE_TO_ONE) {
                     $unique = true;
                     $optional = isset($mapping['joinColumns'][0]['nullable']) ? $mapping['joinColumns'][0]['nullable'] : false;
                     break;
                 }
             }
         }
         $index = 0;
         $formatters[$assocName] = function ($inserted) use($relatedClass, &$index, $unique, $optional) {
             if (isset($inserted[$relatedClass])) {
                 if ($unique) {
                     $related = null;
                     if (isset($inserted[$relatedClass][$index]) || !$optional) {
                         $related = $inserted[$relatedClass][$index];
                     }
                     $index++;
                     return $related;
                 }
                 return $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)];
             }
             return null;
         };
     }
     return $formatters;
 }
 public function guessColumnFormatters(\Faker\Generator $generator)
 {
     $formatters = array();
     $nameGuesser = new \Faker\Guesser\Name($generator);
     $columnTypeGuesser = new ColumnTypeGuesser($generator);
     foreach ($this->class->getFieldNames() as $fieldName) {
         if ($this->class->isIdentifier($fieldName) || !$this->class->hasField($fieldName)) {
             continue;
         }
         if ($formatter = $nameGuesser->guessFormat($fieldName)) {
             $formatters[$fieldName] = $formatter;
             continue;
         }
         if ($formatter = $columnTypeGuesser->guessFormat($fieldName, $this->class)) {
             $formatters[$fieldName] = $formatter;
             continue;
         }
     }
     foreach ($this->class->getAssociationNames() as $assocName) {
         if ($this->class->isAssociationInverseSide($assocName)) {
             continue;
         }
         $relatedClass = $this->class->getAssociationTargetClass($assocName);
         $unique = $optional = false;
         $mappings = $this->class->getAssociationMappings();
         foreach ($mappings as $mapping) {
             if ($mapping['targetEntity'] == $relatedClass) {
                 if ($mapping['type'] == ClassMetadata::ONE_TO_ONE) {
                     $unique = true;
                     $optional = isset($mapping['joinColumns'][0]['nullable']) ? $mapping['joinColumns'][0]['nullable'] : false;
                     break;
                 }
             }
         }
         $index = 0;
         $isCollection = $this->class->isCollectionValuedAssociation($assocName);
         $formatters[$assocName] = function ($inserted) use($relatedClass, &$index, $unique, $optional, $isCollection) {
             if ($unique && isset($inserted[$relatedClass])) {
                 $related = null;
                 if (isset($inserted[$relatedClass][$index]) || !$optional) {
                     $related = $inserted[$relatedClass][$index];
                 }
                 $index++;
                 $result = $isCollection ? new ArrayCollection(array($related)) : $related;
             } elseif (isset($inserted[$relatedClass])) {
                 if ($isCollection) {
                     $result = new ArrayCollection();
                     $needInsertItemsCount = mt_rand(0, 5);
                     $alreadyInsertedId = [];
                     for ($i = 0; $i < $needInsertItemsCount; $i++) {
                         $itemToInsertIndex = mt_rand(0, count($inserted[$relatedClass]) - 1);
                         if (in_array($itemToInsertIndex, $alreadyInsertedId)) {
                             continue;
                         }
                         $itemToInsert = $inserted[$relatedClass][$itemToInsertIndex];
                         $result->add($itemToInsert);
                         $alreadyInsertedId[] = $itemToInsertIndex;
                     }
                 } else {
                     $result = $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)];
                 }
             } else {
                 return null;
             }
             return $result;
         };
     }
     return $formatters;
 }