|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Transformers\Api\Client; |
| 4 | + |
| 5 | +use Pterodactyl\Models\Task; |
| 6 | +use Pterodactyl\Models\Schedule; |
| 7 | +use Illuminate\Database\Eloquent\Model; |
| 8 | + |
| 9 | +class ScheduleTransformer extends BaseClientTransformer |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var array |
| 13 | + */ |
| 14 | + protected $availableIncludes = ['tasks']; |
| 15 | + |
| 16 | + /** |
| 17 | + * {@inheritdoc} |
| 18 | + */ |
| 19 | + public function getResourceName(): string |
| 20 | + { |
| 21 | + return Schedule::RESOURCE_NAME; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Returns a transformed schedule model such that a client can view the information. |
| 26 | + * |
| 27 | + * @param \Pterodactyl\Models\Schedule $model |
| 28 | + * @return array |
| 29 | + */ |
| 30 | + public function transform(Schedule $model) |
| 31 | + { |
| 32 | + return [ |
| 33 | + 'id' => $model->id, |
| 34 | + 'name' => $model->name, |
| 35 | + 'cron' => [ |
| 36 | + 'day_of_week' => $model->cron_day_of_week, |
| 37 | + 'day_of_month' => $model->cron_day_of_month, |
| 38 | + 'hour' => $model->cron_hour, |
| 39 | + 'minute' => $model->cron_minute, |
| 40 | + ], |
| 41 | + 'is_active' => $model->is_active, |
| 42 | + 'is_processing' => $model->is_processing, |
| 43 | + 'last_run_at' => $model->last_run_at ? $model->last_run_at->toIso8601String() : null, |
| 44 | + 'next_run_at' => $model->next_run_at ? $model->next_run_at->toIso8601String() : null, |
| 45 | + 'created_at' => $model->created_at->toIso8601String(), |
| 46 | + 'updated_at' => $model->updated_at->toIso8601String(), |
| 47 | + ]; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Allows attaching the tasks specific to the schedule in the response. |
| 52 | + * |
| 53 | + * @param \Pterodactyl\Models\Schedule $model |
| 54 | + * @return \League\Fractal\Resource\Collection |
| 55 | + * |
| 56 | + * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException |
| 57 | + */ |
| 58 | + public function includeTasks(Schedule $model) |
| 59 | + { |
| 60 | + return $this->collection( |
| 61 | + $model->tasks, $this->makeTransformer(TaskTransformer::class), Task::RESOURCE_NAME |
| 62 | + ); |
| 63 | + } |
| 64 | +} |
0 commit comments