Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/QueryParameters/GetAllocationsParameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Spatie\FloatSdk\QueryParameters;

use Spatie\FloatSdk\Concerns\HasPaginationAndSort;

class GetAllocationsParameters
{
use HasPaginationAndSort;

/**
* @param ?array<string> $fields
* @param ?array<string> $expand
*/
public function __construct(
public ?int $clientId = null,
public ?int $projectId = null,
public ?int $phaseId = null,
public ?int $taskMetaId = null,
public ?int $peopleId = null,
public ?string $startDate = null,
public ?string $endDate = null,
public ?int $billable = null,
public ?int $status = null,
public ?int $repeatState = null,
public ?string $modifiedSince = null,
public ?array $fields = null,
public ?array $expand = null,
?int $page = null,
?int $perPage = null,
?string $sort = null,
) {
$this->page = $page;
$this->perPage = $perPage;
$this->sort = $sort;
}

/**
* @return array<string, string|int|null>
*/
public function toArray(): array
{
return array_filter([
'client_id' => $this->clientId,
'project_id' => $this->projectId,
'phase_id' => $this->phaseId,
'task_meta_id' => $this->taskMetaId,
'people_id' => $this->peopleId,
'start_date' => $this->startDate,
'end_date' => $this->endDate,
'billable' => $this->billable,
'status' => $this->status,
'repeat_state' => $this->repeatState,
'modified_since' => $this->modifiedSince,
'fields' => $this->fields ? implode(',', $this->fields) : null,
'expand' => $this->expand ? implode(',', $this->expand) : null,
'page' => $this->page,
'per-page' => $this->perPage,
'sort' => $this->sort,
], fn ($value) => $value !== null);
}
}
25 changes: 25 additions & 0 deletions src/Requests/GetAllocation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Spatie\FloatSdk\Requests;

use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Spatie\FloatSdk\Resources\AllocationResource;

class GetAllocation extends Request
{
protected Method $method = Method::GET;

public function __construct(protected int $id) {}

public function resolveEndpoint(): string
{
return "/tasks/{$this->id}";
}

public function createDtoFromResponse(Response $response): AllocationResource
{
return AllocationResource::createFromResponse($response->json());
}
}
37 changes: 37 additions & 0 deletions src/Requests/GetAllocations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Spatie\FloatSdk\Requests;

use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Spatie\FloatSdk\QueryParameters\GetAllocationsParameters;
use Spatie\FloatSdk\Resources\TaskResource;

class GetAllocations extends Request
{
protected Method $method = Method::GET;

public function __construct(
protected ?GetAllocationsParameters $parameters = null
) {}

protected function defaultQuery(): array
{
return $this->parameters ? $this->parameters->toArray() : [];
}

public function resolveEndpoint(): string
{
return '/tasks';
}

/** @return array<int, TaskResource> */
public function createDtoFromResponse(Response $response): array
{
return array_map(
fn (array $object) => TaskResource::createFromResponse($object),
$response->json()
);
}
}
62 changes: 62 additions & 0 deletions src/Resources/AllocationResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Spatie\FloatSdk\Resources;

/**
* Note: The API refers to these as "tasks", but they model project allocations.
*/
class AllocationResource
{
public function __construct(
public ?int $taskId,
public ?int $rootTaskId,
public ?int $parentTaskId,
public ?int $projectId,
public ?int $phaseId,
public ?string $startDate,
public ?string $endDate,
public ?string $startTime,
public ?float $hours,
public ?int $peopleId,
/** @var string[] */
public array $peopleIds,
public ?int $status,
public ?int $billable,
public ?string $name,
public ?string $notes,
public ?int $repeatState,
public ?string $repeatEndDate,
public ?int $createdBy,
public ?string $created,
public ?int $modifiedBy,
public ?string $modified,
) {}

/** @param array<string, mixed> $data */
public static function createFromResponse(array $data): self
{
return new self(
taskId: $data['task_id'] ?? null,
rootTaskId: $data['root_task_id'] ?? null,
parentTaskId: $data['parent_task_id'] ?? null,
projectId: $data['project_id'] ?? null,
phaseId: $data['phase_id'] ?? null,
startDate: $data['start_date'] ?? null,
endDate: $data['end_date'] ?? null,
startTime: $data['start_time'] ?? null,
hours: $data['hours'] ?? null,
peopleId: $data['people_id'] ?? null,
peopleIds: $data['people_ids'] ?? [],
status: $data['status'] ?? null,
billable: $data['billable'] ?? null,
name: $data['name'] ?? null,
notes: $data['notes'] ?? null,
repeatState: $data['repeat_state'] ?? null,
repeatEndDate: $data['repeat_end_date'] ?? null,
createdBy: $data['created_by'] ?? null,
created: $data['created'] ?? null,
modifiedBy: $data['modified_by'] ?? null,
modified: $data['modified'] ?? null,
);
}
}