PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/dmCorePlugin/lib/doctrine/loremizer/dmRecordLoremizer.php

http://github.com/diem-project/diem
PHP | 263 lines | 216 code | 37 blank | 10 comment | 25 complexity | bf030d3117cdeac716ce623aba459bcb MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, ISC
  1. <?php
  2. class dmRecordLoremizer extends dmConfigurable
  3. {
  4. protected
  5. $record,
  6. $table;
  7. public function __construct(array $options = array())
  8. {
  9. $this->initialize($options);
  10. }
  11. public function getDefaultOptions()
  12. {
  13. return array(
  14. 'override' => false,
  15. 'create_associations' => true
  16. );
  17. }
  18. protected function initialize(array $options)
  19. {
  20. $this->configure($options);
  21. }
  22. public function execute(dmDoctrineRecord $record)
  23. {
  24. $this->record = $record;
  25. $this->table = $record->getTable();
  26. if($this->table->hasI18n())
  27. {
  28. $this->record->getCurrentTranslation();
  29. }
  30. // $this->record->clearRelated();
  31. foreach($this->getLoremizableColumns() as $columnName => $column)
  32. {
  33. $this->loremizeColumn($columnName, $column);
  34. }
  35. if ($this->getOption('create_associations'))
  36. {
  37. $this->loremizeAssociations();
  38. }
  39. return $this->record;
  40. }
  41. protected function loremizeColumn($columnName, array $column)
  42. {
  43. if ($this->table->isI18nColumn($columnName))
  44. {
  45. $defaultValue = $this->table->getI18nTable()->getDefaultValueOf($columnName);
  46. }
  47. else
  48. {
  49. $defaultValue = $this->table->getDefaultValueOf($columnName);
  50. }
  51. /*
  52. * Non override on existing records
  53. * pass if columns value is different than its default value
  54. */
  55. if (!$this->getOption('override') && (!$this->record->isNew() || $this->record->get($columnName) !== $defaultValue))
  56. {
  57. return;
  58. }
  59. // skip auto-generated slug
  60. if ($columnName === 'slug' && $this->table->hasTemplate('Sluggable'))
  61. {
  62. return;
  63. }
  64. // skip i18n lang
  65. if ($columnName === 'lang' && $this->table->hasI18n())
  66. {
  67. return;
  68. }
  69. // if the field can be null, set it to null sometimes
  70. if (!dmArray::get($column, 'notnull') && !dmArray::get($column, 'unique') && !mt_rand(0, 3))
  71. {
  72. $val = null;
  73. }
  74. // handle local keys
  75. elseif ($localRelation = $this->table->getRelationHolder()->getLocalByColumnName($columnName))
  76. {
  77. $val = $this->getRandomId($localRelation->getTable());
  78. }
  79. // handle i18n local keys
  80. elseif ($this->table->hasI18n() && ($localRelation = $this->table->getI18nTable()->getRelationHolder()->getLocalByColumnName($columnName)))
  81. {
  82. $val = $this->getRandomId($localRelation->getTable());
  83. }
  84. else
  85. {
  86. $val = $this->getRandomValue($columnName, $column);
  87. }
  88. $this->record->set($columnName, $val);
  89. }
  90. protected function loremizeAssociations()
  91. {
  92. foreach($this->table->getRelationHolder()->getAssociations() as $relation)
  93. {
  94. $this->record->clearRelated($relation->getAlias());
  95. if (mt_rand(0, 4))
  96. {
  97. $ids = (array) dmDb::query($relation->getClass().' t')
  98. ->select('t.id, RANDOM() AS rand')
  99. ->orderBy('rand')
  100. ->limit(mt_rand(1, 6))
  101. ->fetchFlat();
  102. $this->record->link($relation->getAlias(), array_unique($ids));
  103. }
  104. }
  105. }
  106. protected function getLoremizableColumns()
  107. {
  108. return $this->table->getHumanColumns();
  109. }
  110. protected function getRandomValue($columnName, array $column)
  111. {
  112. $column['name'] = $columnName;
  113. switch($column['type'])
  114. {
  115. case 'string':
  116. $val = $this->getStringValForColumn($column);
  117. break;
  118. case 'boolean':
  119. if('is_active' === $columnName)
  120. {
  121. $val = (bool)mt_rand(0, 2);
  122. }
  123. else
  124. {
  125. $val = (bool)mt_rand(0, 1);
  126. }
  127. break;
  128. case 'blob':
  129. case 'clob':
  130. if ($this->table->isMarkdownColumn($columnName))
  131. {
  132. $val = dmLorem::getMarkdownLorem(1);
  133. }
  134. else
  135. {
  136. $val = dmLorem::getBigLorem();
  137. }
  138. break;
  139. case 'time':
  140. case 'timestamp':
  141. case 'date':
  142. $val = date("Y-m-d H:i:s", mt_rand(strtotime('-10 year') , time()));
  143. break;
  144. case 'year':
  145. $val = date("Y", mt_rand(strtotime('-10 year') , time()));
  146. break;
  147. case 'enum':
  148. $val = $column['values'][array_rand($column['values'])];
  149. break;
  150. case 'integer':
  151. case 'int':
  152. $val = mt_rand(0, pow(10, $column['length']) - 1);
  153. break;
  154. case 'float':
  155. case 'decimal':
  156. case 'double':
  157. $val = mt_rand(0, pow(10, $column['length']) - 1)/pow(10, dmArray::get($column, 'scale', 2));
  158. break;
  159. default:
  160. throw new dmException(sprintf('Diem can not generate random content for %s column', $columnName));
  161. }
  162. return $val;
  163. }
  164. protected function getStringValForColumn($column)
  165. {
  166. if (dmArray::get($column, 'email'))
  167. {
  168. return dmString::random(mt_rand(4, 30)).'@localhost.com';
  169. }
  170. if ($this->table->isLinkColumn($column['name']))
  171. {
  172. return $this->getRandomLink();
  173. }
  174. if($column['length'] < 40)
  175. {
  176. $nbCarac = mt_rand(1, $column['length']);
  177. }
  178. else
  179. {
  180. $maxLen = $column['length'] > 80 ? $column['length'] / 2 : $column['length'];
  181. $nbCarac = mt_rand(min(4, $column['length']), max(40, $maxLen));
  182. }
  183. $val = trim(dmLorem::getLittleLorem($nbCarac));
  184. if (dmArray::get($column, 'unique'))
  185. {
  186. $val = substr(dmString::random(4).' '.$val, 0, $column['length']);
  187. }
  188. if (dmArray::get($column, 'nospace'))
  189. {
  190. $val = str_replace(' ', '-', $val);
  191. }
  192. return $val;
  193. }
  194. protected function getRandomLink()
  195. {
  196. if(mt_rand(0, 1))
  197. {
  198. return sprintf('http://'.dmString::random().'.random.com');
  199. }
  200. if(!class_exists('DmPageTranslation', false))
  201. {
  202. new DmPage();
  203. }
  204. $page = dmDb::query('DmPageTranslation p')
  205. ->select('p.id, p.name, p.lang, RANDOM() AS rand')
  206. ->where('p.lang = ?', dmDoctrineRecord::getDefaultCulture())
  207. ->orderBy('rand')
  208. ->limit(1)
  209. ->fetchPDO();
  210. return sprintf('page:%d %s', $page[0][0], $page[0][1]);
  211. }
  212. protected function getRandomId(dmDoctrineTable $table)
  213. {
  214. $id = $table->createQuery('t')
  215. ->select('t.id, RANDOM() AS rand')
  216. ->orderBy('rand')
  217. ->limit(1)
  218. ->fetchValue();
  219. if (!$id)
  220. {
  221. $recordLoremizer = new self($this->getOptions());
  222. $id = $recordLoremizer->execute($table->create())->saveGet()->get('id');
  223. }
  224. return $id;
  225. }
  226. }