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.

111 lines
4.1 KiB

20 hours ago
<?php
namespace App\Http\Resources\Mobile;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* Public representation of a course.
*
* Do not return Course models directly from public mobile endpoints. Course
* has computed appends and relations that contain internal and personal data.
*/
class PublicCourseResource extends JsonResource
{
public function toArray($request)
{
$course = $this->resource;
return [
'id' => (int) $this->id,
'name' => $this->name,
'start_date' => $this->start_date,
'end_date' => $this->end_date,
'content' => $this->content,
'publicize_content' => $this->publicize_content,
'type' => $this->type === null ? null : (int) $this->type,
'is_fee' => $this->is_fee === null ? null : (int) $this->is_fee,
'total' => $this->total === null ? null : (int) $this->total,
'show_txl' => $this->show_txl === null ? null : (int) $this->show_txl,
'show_mobile' => $this->show_mobile === null ? null : (int) $this->show_mobile,
'url' => $this->url,
'url_title' => $this->url_title,
'status' => (int) $this->status,
'course_status' => (int) $this->course_status,
'sign_status' => (int) $this->sign_status,
'my_user' => isset($this->my_user) ? (int) $this->my_user : 0,
'type_detail' => $this->whenLoaded('typeDetail', function () {
if (!$this->typeDetail) {
return null;
}
return [
'id' => (int) $this->typeDetail->id,
'name' => $this->typeDetail->name,
];
}),
'image' => $this->whenLoaded('image', function () {
if (!$this->image) {
return null;
}
return [
'id' => (int) $this->image->id,
'url' => $this->image->url,
];
}),
'qun_image_id' => $this->qun_image_id === null ? null : (int) $this->qun_image_id,
'qun_image' => $this->whenLoaded('qunImage', function () {
return $this->safeUpload($this->qunImage);
}),
'publicize' => $course->relationLoaded('publicAssets')
? $course->getRelation('publicAssets')->map(fn ($upload) => $this->safeUpload($upload))->values()->all()
: [],
'teacher' => $this->whenLoaded('teacher', function () {
if (!$this->teacher) {
return null;
}
return [
'id' => (int) $this->teacher->id,
'name' => $this->teacher->name,
'introduce' => $this->teacher->introduce,
];
}),
'course_forms' => $this->whenLoaded('courseForms', function () {
return $this->courseForms->map(function ($form) {
// Keep only fields needed to render the public sign-up form.
return [
'id' => (int) $form->id,
'name' => $form->name,
'field' => $form->field,
'edit_input' => $form->edit_input,
'rule' => $form->rule,
'sort' => (int) $form->sort,
'help' => $form->help,
'select_item' => $form->select_item,
'need_fill' => (bool) $form->need_fill,
'belong_user' => (bool) $form->belong_user,
];
})->values()->all();
}),
'course_signs_count' => $this->when(
isset($this->course_signs_count),
(int) $this->course_signs_count
),
];
}
private function safeUpload($upload): ?array
{
if (!$upload) {
return null;
}
return [
'id' => (int) $upload->id,
'url' => $upload->url,
];
}
}