FactoryBuilder.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace Illuminate\Database\Eloquent;
  3. use Faker\Generator as Faker;
  4. use InvalidArgumentException;
  5. use Illuminate\Support\Traits\Macroable;
  6. class FactoryBuilder
  7. {
  8. use Macroable;
  9. /**
  10. * The model definitions in the container.
  11. *
  12. * @var array
  13. */
  14. protected $definitions;
  15. /**
  16. * The model being built.
  17. *
  18. * @var string
  19. */
  20. protected $class;
  21. /**
  22. * The name of the model being built.
  23. *
  24. * @var string
  25. */
  26. protected $name = 'default';
  27. /**
  28. * The database connection on which the model instance should be persisted.
  29. *
  30. * @var string
  31. */
  32. protected $connection;
  33. /**
  34. * The model states.
  35. *
  36. * @var array
  37. */
  38. protected $states;
  39. /**
  40. * The states to apply.
  41. *
  42. * @var array
  43. */
  44. protected $activeStates = [];
  45. /**
  46. * The Faker instance for the builder.
  47. *
  48. * @var \Faker\Generator
  49. */
  50. protected $faker;
  51. /**
  52. * The number of models to build.
  53. *
  54. * @var int|null
  55. */
  56. protected $amount = null;
  57. /**
  58. * Create an new builder instance.
  59. *
  60. * @param string $class
  61. * @param string $name
  62. * @param array $definitions
  63. * @param array $states
  64. * @param \Faker\Generator $faker
  65. * @return void
  66. */
  67. public function __construct($class, $name, array $definitions, array $states, Faker $faker)
  68. {
  69. $this->name = $name;
  70. $this->class = $class;
  71. $this->faker = $faker;
  72. $this->states = $states;
  73. $this->definitions = $definitions;
  74. }
  75. /**
  76. * Set the amount of models you wish to create / make.
  77. *
  78. * @param int $amount
  79. * @return $this
  80. */
  81. public function times($amount)
  82. {
  83. $this->amount = $amount;
  84. return $this;
  85. }
  86. /**
  87. * Set the states to be applied to the model.
  88. *
  89. * @param array|mixed $states
  90. * @return $this
  91. */
  92. public function states($states)
  93. {
  94. $this->activeStates = is_array($states) ? $states : func_get_args();
  95. return $this;
  96. }
  97. /**
  98. * Set the database connection on which the model instance should be persisted.
  99. *
  100. * @param string $name
  101. * @return $this
  102. */
  103. public function connection($name)
  104. {
  105. $this->connection = $name;
  106. return $this;
  107. }
  108. /**
  109. * Create a model and persist it in the database if requested.
  110. *
  111. * @param array $attributes
  112. * @return \Closure
  113. */
  114. public function lazy(array $attributes = [])
  115. {
  116. return function () use ($attributes) {
  117. return $this->create($attributes);
  118. };
  119. }
  120. /**
  121. * Create a collection of models and persist them to the database.
  122. *
  123. * @param array $attributes
  124. * @return mixed
  125. */
  126. public function create(array $attributes = [])
  127. {
  128. $results = $this->make($attributes);
  129. if ($results instanceof Model) {
  130. $this->store(collect([$results]));
  131. } else {
  132. $this->store($results);
  133. }
  134. return $results;
  135. }
  136. /**
  137. * Set the connection name on the results and store them.
  138. *
  139. * @param \Illuminate\Support\Collection $results
  140. * @return void
  141. */
  142. protected function store($results)
  143. {
  144. $results->each(function ($model) {
  145. if (! isset($this->connection)) {
  146. $model->setConnection($model->newQueryWithoutScopes()->getConnection()->getName());
  147. }
  148. $model->save();
  149. });
  150. }
  151. /**
  152. * Create a collection of models.
  153. *
  154. * @param array $attributes
  155. * @return mixed
  156. */
  157. public function make(array $attributes = [])
  158. {
  159. if ($this->amount === null) {
  160. return $this->makeInstance($attributes);
  161. }
  162. if ($this->amount < 1) {
  163. return (new $this->class)->newCollection();
  164. }
  165. return (new $this->class)->newCollection(array_map(function () use ($attributes) {
  166. return $this->makeInstance($attributes);
  167. }, range(1, $this->amount)));
  168. }
  169. /**
  170. * Create an array of raw attribute arrays.
  171. *
  172. * @param array $attributes
  173. * @return mixed
  174. */
  175. public function raw(array $attributes = [])
  176. {
  177. if ($this->amount === null) {
  178. return $this->getRawAttributes($attributes);
  179. }
  180. if ($this->amount < 1) {
  181. return [];
  182. }
  183. return array_map(function () use ($attributes) {
  184. return $this->getRawAttributes($attributes);
  185. }, range(1, $this->amount));
  186. }
  187. /**
  188. * Get a raw attributes array for the model.
  189. *
  190. * @param array $attributes
  191. * @return mixed
  192. */
  193. protected function getRawAttributes(array $attributes = [])
  194. {
  195. $definition = call_user_func(
  196. $this->definitions[$this->class][$this->name],
  197. $this->faker, $attributes
  198. );
  199. return $this->expandAttributes(
  200. array_merge($this->applyStates($definition, $attributes), $attributes)
  201. );
  202. }
  203. /**
  204. * Make an instance of the model with the given attributes.
  205. *
  206. * @param array $attributes
  207. * @return \Illuminate\Database\Eloquent\Model
  208. *
  209. * @throws \InvalidArgumentException
  210. */
  211. protected function makeInstance(array $attributes = [])
  212. {
  213. return Model::unguarded(function () use ($attributes) {
  214. if (! isset($this->definitions[$this->class][$this->name])) {
  215. throw new InvalidArgumentException("Unable to locate factory with name [{$this->name}] [{$this->class}].");
  216. }
  217. $instance = new $this->class(
  218. $this->getRawAttributes($attributes)
  219. );
  220. if (isset($this->connection)) {
  221. $instance->setConnection($this->connection);
  222. }
  223. return $instance;
  224. });
  225. }
  226. /**
  227. * Apply the active states to the model definition array.
  228. *
  229. * @param array $definition
  230. * @param array $attributes
  231. * @return array
  232. */
  233. protected function applyStates(array $definition, array $attributes = [])
  234. {
  235. foreach ($this->activeStates as $state) {
  236. if (! isset($this->states[$this->class][$state])) {
  237. throw new InvalidArgumentException("Unable to locate [{$state}] state for [{$this->class}].");
  238. }
  239. $definition = array_merge(
  240. $definition,
  241. $this->stateAttributes($state, $attributes)
  242. );
  243. }
  244. return $definition;
  245. }
  246. /**
  247. * Get the state attributes.
  248. *
  249. * @param string $state
  250. * @param array $attributes
  251. * @return array
  252. */
  253. protected function stateAttributes($state, array $attributes)
  254. {
  255. $stateAttributes = $this->states[$this->class][$state];
  256. if (! is_callable($stateAttributes)) {
  257. return $stateAttributes;
  258. }
  259. return call_user_func(
  260. $stateAttributes,
  261. $this->faker, $attributes
  262. );
  263. }
  264. /**
  265. * Expand all attributes to their underlying values.
  266. *
  267. * @param array $attributes
  268. * @return array
  269. */
  270. protected function expandAttributes(array $attributes)
  271. {
  272. foreach ($attributes as &$attribute) {
  273. if (is_callable($attribute) && ! is_string($attribute)) {
  274. $attribute = $attribute($attributes);
  275. }
  276. if ($attribute instanceof static) {
  277. $attribute = $attribute->create()->getKey();
  278. }
  279. if ($attribute instanceof Model) {
  280. $attribute = $attribute->getKey();
  281. }
  282. }
  283. return $attributes;
  284. }
  285. }