PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Faker/ORM/Propel/EntityPopulator.php

https://github.com/gobb/Faker
PHP | 168 lines | 136 code | 20 blank | 12 comment | 12 complexity | bc14f5097b0d17b3fe598a780ed03633 MD5 | raw file
  1. <?php
  2. namespace Faker\ORM\Propel;
  3. use \Faker\Provider\Base;
  4. /**
  5. * Service class for populating a table through a Propel ActiveRecord class.
  6. */
  7. class EntityPopulator
  8. {
  9. protected $class;
  10. protected $columnFormatters = array();
  11. protected $modifiers = array();
  12. /**
  13. * Class constructor.
  14. *
  15. * @param string $class A Propel ActiveRecord classname
  16. */
  17. public function __construct($class)
  18. {
  19. $this->class = $class;
  20. }
  21. public function getClass()
  22. {
  23. return $this->class;
  24. }
  25. public function setColumnFormatters($columnFormatters)
  26. {
  27. $this->columnFormatters = $columnFormatters;
  28. }
  29. public function getColumnFormatters()
  30. {
  31. return $this->columnFormatters;
  32. }
  33. public function mergeColumnFormattersWith($columnFormatters)
  34. {
  35. $this->columnFormatters = array_merge($this->columnFormatters, $columnFormatters);
  36. }
  37. public function guessColumnFormatters(\Faker\Generator $generator)
  38. {
  39. $formatters = array();
  40. $class = $this->class;
  41. $peerClass = $class::PEER;
  42. $tableMap = $peerClass::getTableMap();
  43. $nameGuesser = new \Faker\Guesser\Name($generator);
  44. $columnTypeGuesser = new \Faker\ORM\Propel\ColumnTypeGuesser($generator);
  45. foreach ($tableMap->getColumns() as $columnMap) {
  46. // skip behavior columns, handled by modifiers
  47. if ($this->isColumnBehavior($columnMap)) {
  48. continue;
  49. }
  50. if ($columnMap->isForeignKey()) {
  51. $relatedClass = $columnMap->getRelation()->getForeignTable()->getClassname();
  52. $formatters[$columnMap->getPhpName()] = function($inserted) use ($relatedClass) { return isset($inserted[$relatedClass]) ? $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)] : null; };
  53. continue;
  54. }
  55. if ($columnMap->isPrimaryKey()) {
  56. continue;
  57. }
  58. if ($formatter = $nameGuesser->guessFormat($columnMap->getPhpName())) {
  59. $formatters[$columnMap->getPhpName()] = $formatter;
  60. continue;
  61. }
  62. if ($formatter = $columnTypeGuesser->guessFormat($columnMap)) {
  63. $formatters[$columnMap->getPhpName()] = $formatter;
  64. continue;
  65. }
  66. }
  67. return $formatters;
  68. }
  69. protected function isColumnBehavior($columnMap)
  70. {
  71. foreach ($columnMap->getTable()->getBehaviors() as $name => $params) {
  72. $columnName = Base::toLower($columnMap->getName());
  73. switch ($name) {
  74. case 'nested_set':
  75. $columnNames = array($params['left_column'], $params['right_column'], $params['level_column']);
  76. if (in_array($columnName, $columnNames)) {
  77. return true;
  78. }
  79. break;
  80. case 'timestampable':
  81. $columnNames = array($params['create_column'], $params['update_column']);
  82. if (in_array($columnName, $columnNames)) {
  83. return true;
  84. }
  85. break;
  86. }
  87. }
  88. return false;
  89. }
  90. public function setModifiers($modifiers)
  91. {
  92. $this->modifiers = $modifiers;
  93. }
  94. public function getModifiers()
  95. {
  96. return $this->modifiers;
  97. }
  98. public function mergeModifiersWith($modifiers)
  99. {
  100. $this->modifiers = array_merge($this->modifiers, $modifiers);
  101. }
  102. public function guessModifiers(\Faker\Generator $generator)
  103. {
  104. $modifiers = array();
  105. $class = $this->class;
  106. $peerClass = $class::PEER;
  107. $tableMap = $peerClass::getTableMap();
  108. foreach ($tableMap->getBehaviors() as $name => $params) {
  109. switch ($name) {
  110. case 'nested_set':
  111. $modifiers['nested_set'] = function($obj, $inserted) use ($class, $generator) {
  112. if (isset($inserted[$class])) {
  113. $queryClass = $class . 'Query';
  114. $parent = $queryClass::create()->findPk($generator->randomElement($inserted[$class]));
  115. $obj->insertAsLastChildOf($parent);
  116. } else {
  117. $obj->makeRoot();
  118. }
  119. };
  120. break;
  121. case 'sortable':
  122. $modifiers['sortable'] = function($obj, $inserted) use ($class, $generator) {
  123. $maxRank = isset($inserted[$class]) ? count($inserted[$class]) : 0;
  124. $obj->insertAtRank(mt_rand(1, $maxRank + 1));
  125. };
  126. break;
  127. }
  128. }
  129. return $modifiers;
  130. }
  131. /**
  132. * Insert one new record using the Entity class.
  133. */
  134. public function execute($con, $insertedEntities)
  135. {
  136. $obj = new $this->class();
  137. foreach ($this->getColumnFormatters() as $column => $format) {
  138. if (null !== $format) {
  139. $obj->setByName($column, is_callable($format) ? $format($insertedEntities, $obj) : $format);
  140. }
  141. }
  142. foreach ($this->getModifiers() as $modifier) {
  143. $modifier($obj, $insertedEntities);
  144. }
  145. $obj->save($con);
  146. return $obj->getPrimaryKey();
  147. }
  148. }