79 lines
2.5 KiB
PHP
79 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Integrations\SnipeIt\Data;
|
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
final readonly class AssetData
|
|
{
|
|
public function __construct(
|
|
public int $snipeAssetId,
|
|
public string $assetTag,
|
|
public string $name,
|
|
public ?string $serial,
|
|
public ?int $modelId,
|
|
public ?string $modelName,
|
|
public ?int $categoryId,
|
|
public ?string $categoryName,
|
|
public ?string $manufacturer,
|
|
public ?int $statusId,
|
|
public ?string $statusName,
|
|
public ?int $locationId,
|
|
public ?string $locationName,
|
|
public bool $requestable,
|
|
public ?string $assignedType,
|
|
public ?int $assignedId,
|
|
public ?string $imageUrl,
|
|
public ?CarbonImmutable $sourceUpdatedAt,
|
|
) {}
|
|
|
|
public function isAssigned(): bool
|
|
{
|
|
return $this->assignedType !== null || $this->assignedId !== null;
|
|
}
|
|
|
|
public function isAssignedToUser(int $userId): bool
|
|
{
|
|
if ($this->assignedId !== $userId || $this->assignedType === null) {
|
|
return false;
|
|
}
|
|
|
|
$normalizedType = strtolower(str_replace(['\\', '_', '-', ' '], '', $this->assignedType));
|
|
|
|
return str_ends_with($normalizedType, 'user');
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
public function toModelAttributes(): array
|
|
{
|
|
return [
|
|
'snipe_asset_id' => $this->snipeAssetId,
|
|
'asset_tag' => $this->assetTag,
|
|
'name' => $this->name,
|
|
'serial' => $this->serial,
|
|
'model_id' => $this->modelId,
|
|
'model_name' => $this->modelName,
|
|
'category_id' => $this->categoryId,
|
|
'category_name' => $this->categoryName,
|
|
'manufacturer' => $this->manufacturer,
|
|
'status_id' => $this->statusId,
|
|
'status_name' => $this->statusName,
|
|
'location_id' => $this->locationId,
|
|
'location_name' => $this->locationName,
|
|
'requestable' => $this->requestable,
|
|
'assigned_type' => $this->assignedType,
|
|
'assigned_id' => $this->assignedId,
|
|
'image_url' => $this->imageUrl,
|
|
'source_updated_at' => $this->sourceUpdatedAt,
|
|
];
|
|
}
|
|
|
|
public function contentHash(): string
|
|
{
|
|
$attributes = $this->toModelAttributes();
|
|
$attributes['source_updated_at'] = $this->sourceUpdatedAt?->toIso8601String();
|
|
|
|
return hash('sha256', json_encode($attributes, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE));
|
|
}
|
|
}
|