1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Transformers/TaskTransformer.php

141 lines
4.1 KiB
PHP
Raw Normal View History

2020-01-21 01:32:34 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2020-01-21 01:32:34 +01:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2020-01-21 01:32:34 +01:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-01-21 01:32:34 +01:00
*/
namespace App\Transformers;
2023-08-06 04:20:18 +02:00
use App\Models\Client;
2023-10-26 04:57:44 +02:00
use App\Models\Document;
2023-08-06 04:20:18 +02:00
use App\Models\Invoice;
use App\Models\Project;
2023-10-26 04:57:44 +02:00
use App\Models\Task;
use App\Models\TaskStatus;
2023-10-26 04:57:44 +02:00
use App\Models\User;
2020-01-21 01:32:34 +01:00
use App\Utils\Traits\MakesHash;
2022-07-04 21:39:45 +02:00
use League\Fractal\Resource\Item;
2020-01-21 01:32:34 +01:00
/**
* class TaskTransformer.
2020-01-21 01:32:34 +01:00
*/
class TaskTransformer extends EntityTransformer
{
use MakesHash;
2023-08-21 03:29:31 +02:00
protected array $defaultIncludes = [
'documents',
2023-08-18 13:36:50 +02:00
'project',
2020-01-21 01:32:34 +01:00
];
/**
* @var array
*/
2023-08-21 03:29:31 +02:00
protected array $availableIncludes = [
2022-07-04 21:39:45 +02:00
'client',
2023-06-09 07:38:56 +02:00
'status',
'project',
2023-06-23 03:09:40 +02:00
'user',
2023-07-18 10:21:12 +02:00
'invoice',
2020-01-21 01:32:34 +01:00
];
2020-10-12 01:27:38 +02:00
public function includeDocuments(Task $task)
2020-10-11 23:36:59 +02:00
{
$transformer = new DocumentTransformer($this->serializer);
return $this->includeCollection($task->documents, $transformer, Document::class);
}
2023-07-18 10:21:12 +02:00
public function includeInvoice(Task $task): ?Item
{
$transformer = new InvoiceTransformer($this->serializer);
2023-09-25 07:56:32 +02:00
if (!$task->invoice) {
2023-07-18 10:21:12 +02:00
return null;
}
return $this->includeItem($task->invoice, $transformer, Invoice::class);
}
2023-06-23 03:09:40 +02:00
public function includeUser(Task $task): ?Item
{
$transformer = new UserTransformer($this->serializer);
if (!$task->user) {
return null;
}
return $this->includeItem($task->user, $transformer, User::class);
}
public function includeClient(Task $task): ?Item
2022-07-04 21:39:45 +02:00
{
$transformer = new ClientTransformer($this->serializer);
2023-02-16 02:36:09 +01:00
if (!$task->client) {
return null;
2023-02-16 02:36:09 +01:00
}
2022-07-04 21:39:45 +02:00
return $this->includeItem($task->client, $transformer, Client::class);
}
public function includeStatus(Task $task): ?Item
{
$transformer = new TaskStatusTransformer($this->serializer);
2023-02-16 02:36:09 +01:00
if (!$task->status) {
return null;
2023-02-16 02:36:09 +01:00
}
return $this->includeItem($task->status, $transformer, TaskStatus::class);
}
2023-06-09 07:38:56 +02:00
public function includeProject(Task $task): ?Item
{
$transformer = new ProjectTransformer($this->serializer);
if (!$task->project) {
return null;
}
return $this->includeItem($task->project, $transformer, Project::class);
}
2020-01-21 01:32:34 +01:00
public function transform(Task $task)
{
return [
'id' => (string) $this->encodePrimaryKey($task->id),
2020-10-12 22:42:02 +02:00
'user_id' => (string) $this->encodePrimaryKey($task->user_id),
'assigned_user_id' => (string) $this->encodePrimaryKey($task->assigned_user_id),
'number' => (string) $task->number ?: '',
2020-10-26 22:54:59 +01:00
'description' => (string) $task->description ?: '',
'duration' => (int) $task->duration ?: 0,
2020-10-26 20:10:04 +01:00
'rate' => (float) $task->rate ?: 0,
'created_at' => (int) $task->created_at,
'updated_at' => (int) $task->updated_at,
'archived_at' => (int) $task->deleted_at,
2020-10-26 22:54:59 +01:00
'invoice_id' => $this->encodePrimaryKey($task->invoice_id) ?: '',
'client_id' => $this->encodePrimaryKey($task->client_id) ?: '',
'project_id' => $this->encodePrimaryKey($task->project_id) ?: '',
2020-01-21 01:32:34 +01:00
'is_deleted' => (bool) $task->is_deleted,
'time_log' => $task->time_log ?: '',
2021-01-15 12:01:44 +01:00
'is_running' => (bool) $task->is_running, //@deprecate
2020-01-21 01:32:34 +01:00
'custom_value1' => $task->custom_value1 ?: '',
'custom_value2' => $task->custom_value2 ?: '',
'custom_value3' => $task->custom_value3 ?: '',
'custom_value4' => $task->custom_value4 ?: '',
2020-10-26 22:54:59 +01:00
'status_id' => $this->encodePrimaryKey($task->status_id) ?: '',
2021-01-06 00:36:20 +01:00
'status_sort_order' => (int) $task->status_sort_order, //deprecated 5.0.34
2020-12-14 22:52:14 +01:00
'is_date_based' => (bool) $task->is_date_based,
'status_order' => is_null($task->status_order) ? null : (int) $task->status_order,
2023-07-19 11:25:01 +02:00
'date' => $task->calculated_start_date ?: '',
2020-01-21 01:32:34 +01:00
];
}
}