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.
 
 
 
 

72 lines
1.7 KiB

<?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();
});
}
}