|
|
<?php
namespace Database\Factories;
use App\Image\ImageProcessor; use Illuminate\Support\Str;
trait BaseFactory { public function smash($key) { $field = explode(':', $key)[0]; $methodName = '__' . explode(':', $key)[1]; $param = explode(':', $key)[2] ?? []; $param = is_array($param) ? $param : [$param]; $param = array_merge( [$field], is_array($param) ? $param : [$param]);
return $this->state(function (array $attributes) use ($field, $methodName, $param) { $states[$field] = call_user_func_array([static::class, $methodName], $param); return $states; }); }
protected function __null($field = null) { return null; }
protected function __string($field = null, $length = 16): string { return Str::random($length); }
protected function __gtString($field = null, $min = 256) { return $this->__string($field, rand($min + 1, $min + 1000)); }
protected function __ltString($field = null, $max = 3) { return $this->__string($field, $max - 1); }
protected function __unique($field) { return $this->model::factory()->createQuietly()->{$field}; }
protected function __numeric($field) { return rand(100, 200); }
protected function __lt($field = null, $max = 4) { return rand($max - 100, $max - 1); }
protected function __gt($field = null, $min = 4) { return rand($min + 1, $min + 100); }
public function withDependency() { return $this->state(function (array $attributes) { return $this->dependencyProvider(); });
}
public function withImage() { return null; }
public function createImage($path, $saveOptions = ['Q' => 100],$options = ["w" => null, "h" => null, "r" => null,"flip" => null , "canv" => null,"rotation" => null]) { $imageProcessor = new ImageProcessor; $image = $imageProcessor->process(storage_path('stub') . '/image.png',$path,$options,$saveOptions); return $this; } }
|