Files
jobifygarden/app/Services/JobApi/Transformers/RemotiveTransformer.php
T
2026-04-04 18:19:34 +03:00

145 lines
4.7 KiB
PHP

<?php
namespace App\Services\JobApi\Transformers;
use App\Services\JobApi\DTO\JobData;
use App\Services\JobApi\Exceptions\TransformationException;
use DateTime;
class RemotiveTransformer implements JobTransformer
{
public function transform(array $data): JobData
{
try {
return new JobData(
externalId: (string) $data['id'],
title: $data['title'] ?? 'Untitled',
company: $data['company_name'] ?? 'Unknown Company',
description: $this->sanitizeDescription($data['description'] ?? ''),
location: $this->parseLocation($data),
salaryMin: $this->parseSalary($data['salary'] ?? null, 'min'),
salaryMax: $this->parseSalary($data['salary'] ?? null, 'max'),
salaryCurrency: 'USD',
jobType: $this->mapJobType($data['job_type'] ?? null),
sourceUrl: $data['url'] ?? null,
applyUrl: $data['url'] ?? null,
postedAt: $this->parseDate($data['publication_date'] ?? null),
expiresAt: null,
remoteAllowed: true, // Remotive only has remote jobs
experienceLevel: $this->mapExperienceLevel($data['candidate_required_location'] ?? null),
category: $data['category'] ?? null,
rawData: $data,
);
} catch (\Throwable $e) {
throw new TransformationException(
'Remotive',
'Failed to transform Remotive job data: ' . $e->getMessage(),
['data' => $data, 'error' => $e->getMessage()]
);
}
}
public function transformMany(array $jobs): array
{
return array_map(fn($job) => $this->transform($job), $jobs);
}
private function sanitizeDescription(string $description): string
{
// Remove HTML tags and clean up whitespace
$clean = strip_tags($description);
$clean = html_entity_decode($clean, ENT_QUOTES | ENT_HTML5, 'UTF-8');
$clean = preg_replace('/\s+/', ' ', $clean);
return trim($clean);
}
private function parseLocation(array $data): ?string
{
$location = $data['candidate_required_location'] ?? null;
if (empty($location)) {
return 'Remote';
}
return $location;
}
private function parseSalary(?string $salary, string $type): ?float
{
if (empty($salary)) {
return null;
}
// Normalize the salary string
$salary = strtolower($salary);
// Check if it's hourly rate - if so, convert to annual (assuming 40hr/week * 52 weeks = 2080 hours/year)
$isHourly = str_contains($salary, '/hour') || str_contains($salary, 'per hour') || str_contains($salary, '/hr');
// Try to extract numbers with optional k/K suffix
// Pattern: number followed by optional comma-separated digits, then optional 'k'
if (preg_match_all('/\$?\s*([\d,]+)\s*([kK])?/', $salary, $matches, PREG_SET_ORDER)) {
$numbers = [];
foreach ($matches as $match) {
$number = (float) str_replace(',', '', $match[1]);
// If there's a 'k' suffix, multiply by 1000
if (!empty($match[2]) && strtolower($match[2]) === 'k') {
$number *= 1000;
}
// If it's hourly, convert to annual
if ($isHourly) {
$number *= 2080; // 40 hours/week * 52 weeks
}
$numbers[] = $number;
}
if (count($numbers) >= 2) {
return $type === 'min' ? min($numbers) : max($numbers);
} elseif (count($numbers) === 1) {
return $numbers[0];
}
}
return null;
}
private function parseDate(?string $date): ?DateTime
{
if (empty($date)) {
return null;
}
try {
return new DateTime($date);
} catch (\Exception $e) {
return null;
}
}
private function mapJobType(?string $type): ?string
{
if (empty($type)) {
return 'full-time';
}
return match (strtolower($type)) {
'full_time', 'full-time' => 'full-time',
'part_time', 'part-time' => 'part-time',
'contract' => 'contract',
'freelance' => 'freelance',
default => 'full-time',
};
}
private function mapExperienceLevel(?string $location): ?string
{
// Remotive doesn't provide experience level directly
// This is a placeholder that could be enhanced with ML/NLP
return null;
}
}