PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

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