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

/src/Faker/ORM/Propel2/EntityPopulator.php

http://github.com/fzaninotto/Faker
PHP | 192 lines | 140 code | 19 blank | 33 comment | 12 complexity | e14b1a65e4f7c8bc9b667c1edb1eaaa3 MD5 | raw file
  1. <?php
  2. namespace Faker\ORM\Propel2;
  3. use \Faker\Provider\Base;
  4. use \Propel\Runtime\Map\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::TABLE_MAP;
  53. $tableMap = $peerClass::getTableMap();
  54. $nameGuesser = new \Faker\Guesser\Name($generator);
  55. $columnTypeGuesser = new \Faker\ORM\Propel2\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. $relatedClass = trim($relatedClass, "\\");
  65. return isset($inserted[$relatedClass]) ? $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)] : null;
  66. };
  67. continue;
  68. }
  69. if ($columnMap->isPrimaryKey()) {
  70. continue;
  71. }
  72. if ($formatter = $nameGuesser->guessFormat($columnMap->getPhpName(), $columnMap->getSize())) {
  73. $formatters[$columnMap->getPhpName()] = $formatter;
  74. continue;
  75. }
  76. if ($formatter = $columnTypeGuesser->guessFormat($columnMap)) {
  77. $formatters[$columnMap->getPhpName()] = $formatter;
  78. continue;
  79. }
  80. }
  81. return $formatters;
  82. }
  83. /**
  84. * @param ColumnMap $columnMap
  85. * @return bool
  86. */
  87. protected function isColumnBehavior(ColumnMap $columnMap)
  88. {
  89. foreach ($columnMap->getTable()->getBehaviors() as $name => $params) {
  90. $columnName = Base::toLower($columnMap->getName());
  91. switch ($name) {
  92. case 'nested_set':
  93. $columnNames = array($params['left_column'], $params['right_column'], $params['level_column']);
  94. if (in_array($columnName, $columnNames)) {
  95. return true;
  96. }
  97. break;
  98. case 'timestampable':
  99. $columnNames = array($params['create_column'], $params['update_column']);
  100. if (in_array($columnName, $columnNames)) {
  101. return true;
  102. }
  103. break;
  104. }
  105. }
  106. return false;
  107. }
  108. public function setModifiers($modifiers)
  109. {
  110. $this->modifiers = $modifiers;
  111. }
  112. /**
  113. * @return array
  114. */
  115. public function getModifiers()
  116. {
  117. return $this->modifiers;
  118. }
  119. public function mergeModifiersWith($modifiers)
  120. {
  121. $this->modifiers = array_merge($this->modifiers, $modifiers);
  122. }
  123. /**
  124. * @param \Faker\Generator $generator
  125. * @return array
  126. */
  127. public function guessModifiers(\Faker\Generator $generator)
  128. {
  129. $modifiers = array();
  130. $class = $this->class;
  131. $peerClass = $class::TABLE_MAP;
  132. $tableMap = $peerClass::getTableMap();
  133. foreach ($tableMap->getBehaviors() as $name => $params) {
  134. switch ($name) {
  135. case 'nested_set':
  136. $modifiers['nested_set'] = function ($obj, $inserted) use ($class, $generator) {
  137. if (isset($inserted[$class])) {
  138. $queryClass = $class . 'Query';
  139. $parent = $queryClass::create()->findPk($generator->randomElement($inserted[$class]));
  140. $obj->insertAsLastChildOf($parent);
  141. } else {
  142. $obj->makeRoot();
  143. }
  144. };
  145. break;
  146. case 'sortable':
  147. $modifiers['sortable'] = function ($obj, $inserted) use ($class) {
  148. $maxRank = isset($inserted[$class]) ? count($inserted[$class]) : 0;
  149. $obj->insertAtRank(mt_rand(1, $maxRank + 1));
  150. };
  151. break;
  152. }
  153. }
  154. return $modifiers;
  155. }
  156. /**
  157. * Insert one new record using the Entity class.
  158. */
  159. public function execute($con, $insertedEntities)
  160. {
  161. $obj = new $this->class();
  162. foreach ($this->getColumnFormatters() as $column => $format) {
  163. if (null !== $format) {
  164. $obj->setByName($column, is_callable($format) ? $format($insertedEntities, $obj) : $format);
  165. }
  166. }
  167. foreach ($this->getModifiers() as $modifier) {
  168. $modifier($obj, $insertedEntities);
  169. }
  170. $obj->save($con);
  171. return $obj->getPrimaryKey();
  172. }
  173. }