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.
 
 
 
 

48 lines
1.3 KiB

<?php
namespace Tests\Base;
use App\Documents\ModelDocument;
use App\Documents\PolicyDocument;
use Illuminate\Support\Str;
trait DynamicPolicyAndModelTrait
{
protected function makeModel($modelName, $policy = null)
{
$data['name'] = $modelName;
$data['index_policy'] = $policy?->id;
$data['store_policy'] = $policy?->id;
$data['update_policy'] = $policy?->id;
$data['get_policy'] = $policy?->id;
$data['delete_policy'] = $policy?->id;
$data['restore_policy'] = $policy?->id;
$data['trash_policy'] = $policy?->id;
return ModelDocument::factory()->create($data);
}
protected function guessModelName($name): string
{
$name = explode('.', $name);
$name = count($name) > 2 ? $name[1] : $name[0];
return Str::singular($name);
}
protected function provideDynamicModel(string|null $modelName = null, array $needs = [])
{
throw_if(empty($modelName), 'name must be provided.');
return $this->makeModel(Str::singular($modelName), $this->one(PolicyDocument::class, ['needs' => [$needs]]));
}
protected function modelWithPolicy($modelName = null, $needs)
{
$this->provideDynamicModel($modelName, $needs);
return $this;
}
}