You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.1 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. <?php
  2. namespace Database\Factories;
  3. use App\Image\ImageProcessor;
  4. use Illuminate\Support\Str;
  5. trait BaseFactory
  6. {
  7. public function smash($key)
  8. {
  9. $field = explode(':', $key)[0];
  10. $methodName = '__' . explode(':', $key)[1];
  11. $param = explode(':', $key)[2] ?? [];
  12. $param = is_array($param) ? $param : [$param];
  13. $param = array_merge( [$field], is_array($param) ? $param : [$param]);
  14. return $this->state(function (array $attributes) use ($field, $methodName, $param) {
  15. $states[$field] = call_user_func_array([static::class, $methodName], $param);
  16. return $states;
  17. });
  18. }
  19. protected function __null($field = null)
  20. {
  21. return null;
  22. }
  23. protected function __string($field = null, $length = 16): string
  24. {
  25. return Str::random($length);
  26. }
  27. protected function __gtString($field = null, $min = 256)
  28. {
  29. return $this->__string($field, rand($min + 1, $min + 1000));
  30. }
  31. protected function __ltString($field = null, $max = 3)
  32. {
  33. return $this->__string($field, $max - 1);
  34. }
  35. protected function __unique($field)
  36. {
  37. return $this->model::factory()->createQuietly()->{$field};
  38. }
  39. protected function __numeric($field)
  40. {
  41. return rand(100, 200);
  42. }
  43. protected function __lt($field = null, $max = 4)
  44. {
  45. return rand($max - 100, $max - 1);
  46. }
  47. protected function __gt($field = null, $min = 4)
  48. {
  49. return rand($min + 1, $min + 100);
  50. }
  51. public function withDependency()
  52. {
  53. return $this->state(function (array $attributes) {
  54. return $this->dependencyProvider();
  55. });
  56. }
  57. public function withImage()
  58. {
  59. return null;
  60. }
  61. public function createImage($path, $saveOptions = ['Q' => 100],$options = ["w" => null, "h" => null, "r" => null,"flip" => null , "canv" => null,"rotation" => null])
  62. {
  63. $imageProcessor = new ImageProcessor;
  64. $image = $imageProcessor->process(storage_path('stub') . '/image.png',$path,$options,$saveOptions);
  65. return $this;
  66. }
  67. }