Files
jobifygarden/app/Http/Resources/JobResource.php
T

72 lines
2.3 KiB
PHP
Raw Normal View History

2026-04-03 01:04:19 +03:00
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class JobResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'external_id' => $this->external_id,
'title' => $this->title,
'company' => $this->company,
'description' => $this->description,
'location' => $this->location,
'salary' => [
'min' => $this->salary_min,
'max' => $this->salary_max,
'currency' => $this->salary_currency,
'formatted' => $this->formatSalary(),
],
'job_type' => $this->job_type,
'remote_allowed' => $this->remote_allowed,
'experience_level' => $this->experience_level,
'source' => new JobSourceResource($this->whenLoaded('source')),
'category' => new JobCategoryResource($this->whenLoaded('category')),
'job_location' => new JobLocationResource($this->whenLoaded('jobLocation')),
'urls' => [
'source' => $this->source_url,
'apply' => $this->apply_url,
],
'dates' => [
'posted_at' => $this->posted_at?->toIso8601String(),
'expires_at' => $this->expires_at?->toIso8601String(),
'created_at' => $this->created_at->toIso8601String(),
'updated_at' => $this->updated_at->toIso8601String(),
],
];
}
/**
* Format salary range for display
*/
private function formatSalary(): ?string
{
if (!$this->salary_min && !$this->salary_max) {
return null;
}
$currency = $this->salary_currency ?? 'USD';
$symbol = $currency === 'USD' ? '$' : $currency . ' ';
if ($this->salary_min && $this->salary_max) {
return $symbol . number_format($this->salary_min, 0) . ' - ' . $symbol . number_format($this->salary_max, 0);
}
if ($this->salary_min) {
return $symbol . number_format($this->salary_min, 0) . '+';
}
return 'Up to ' . $symbol . number_format($this->salary_max, 0);
}
}