generated from liutk/owl-admin-base
73 lines
2.2 KiB
PHP
73 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services\Train;
|
|
|
|
use App\Admin\Filters\TrianBookFilter;
|
|
use App\Models\Train\Book;
|
|
use App\Admin\Services\BaseService;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\{Validator, Storage};
|
|
|
|
class BookService extends BaseService
|
|
{
|
|
protected array $withRelationships = ['category'];
|
|
|
|
protected string $modelName = Book::class;
|
|
|
|
protected string $modelFilterName = TrianBookFilter::class;
|
|
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
if (isset($data['cover_image']) && $data['cover_image']) {
|
|
$image = $data['cover_image'];
|
|
$data['cover_image'] = $this->formatUrl($image);
|
|
}
|
|
if (isset($data['video']) && $data['video']) {
|
|
$video = $data['video'];
|
|
$data['video'] = $this->formatUrl($video);
|
|
}
|
|
if (isset($data['files']) && $data['files']) {
|
|
$files = [];
|
|
foreach ($data['files'] as $key => $value) {
|
|
if (is_array($value)) {
|
|
$path = data_get($value, 'value');
|
|
$value['url'] = $this->formatUrl($path);
|
|
array_push($files, $value);
|
|
} else {
|
|
array_push($files, [
|
|
'id' => $key + 1,
|
|
'name' => $value,
|
|
'url' => $this->formatUrl($value),
|
|
'state' => 'uploaded'
|
|
]);
|
|
}
|
|
}
|
|
$data['files'] = $files;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
protected function formatUrl($str)
|
|
{
|
|
return Str::startsWith($str, ['http://', 'https://']) ? $str : Storage::url($str);
|
|
}
|
|
|
|
public function validate($data, $model = null)
|
|
{
|
|
$createRules = [
|
|
'title' => ['required'],
|
|
'category_id' => ['required'],
|
|
'type' => ['required'],
|
|
'files' => ['array'],
|
|
];
|
|
$updateRules = [
|
|
'files' => ['array'],
|
|
];
|
|
$validator = Validator::make($data, $model ? $updateRules : $createRules);
|
|
if ($validator->fails()) {
|
|
return $validator->errors()->first();
|
|
}
|
|
return true;
|
|
}
|
|
}
|