You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.4 KiB
72 lines
2.4 KiB
<?php
|
|
|
|
namespace App\Domains\Academic\Integration\Mappers;
|
|
|
|
use App\Domains\Academic\Actions\Calendar\InitializeAcademicTerm;
|
|
use App\Domains\Academic\Actions\Calendar\ModifyTermDate;
|
|
use App\Domains\Academic\DTOs\Calendar\InitializeAcademicTermDTO;
|
|
use App\Domains\Academic\DTOs\Calendar\ModifyTermDateDTO;
|
|
use App\Domains\Academic\Models\Term;
|
|
use App\Domains\System\Support\Integration\DataPayloadMapper;
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class TermDataMapper implements DataPayloadMapper
|
|
{
|
|
public function __construct(
|
|
protected InitializeAcademicTerm $initializeAcademicTerm,
|
|
protected ModifyTermDate $modifyTermDate,
|
|
)
|
|
{
|
|
// Inject required Domain and Cross-Domain Actions via constructor composition
|
|
}
|
|
|
|
public function getLookupKey(): string
|
|
{
|
|
// Return the unique string key used to identify existing records (e.g., 'email')
|
|
return 'code';
|
|
}
|
|
|
|
public function transform(array $rawData): array
|
|
{
|
|
// Normalize incoming data array formats into an internal domain-safe layout
|
|
return [
|
|
'name' => $rawData['nama_semester'],
|
|
'code' => $rawData['id_semester'],
|
|
'year_study' => $rawData['id_tahun_ajaran'],
|
|
'semester' => $rawData['semester'],
|
|
'date_start' => $rawData['tanggal_mulai'],
|
|
'date_end' => $rawData['tanggal_selesai'],
|
|
];
|
|
}
|
|
|
|
public function updateOrCreateDomainState(array $payload, ?Model $existingModel = null): void
|
|
{
|
|
/** @var Term $term */
|
|
$term = $existingModel;
|
|
|
|
if(isset($term)) {
|
|
$this->modifyTermDate->execute($term, new ModifyTermDateDTO(
|
|
startDate: CarbonImmutable::parse($payload['date_start']),
|
|
endDate: CarbonImmutable::parse($payload['date_end']),
|
|
));
|
|
} else {
|
|
$this->initializeAcademicTerm->execute(new InitializeAcademicTermDTO(
|
|
name: $payload['name'],
|
|
code: $payload['code'],
|
|
yearStudy: $payload['year_study'],
|
|
semester: $payload['semester'],
|
|
startDate: CarbonImmutable::parse($payload['date_start']),
|
|
endDate: CarbonImmutable::parse($payload['date_end']),
|
|
));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return class-string
|
|
*/
|
|
public function getModelClass(): string
|
|
{
|
|
return Term::class;
|
|
}
|
|
}
|
|
|