Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| UserPreferenceDTO | |
100.00% |
19 / 19 |
|
100.00% |
3 / 3 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromArray | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
| toArray | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** @license For full copyright and license information, please see the LICENSE.md file. */ |
| 6 | |
| 7 | namespace App\Core\Preference\Application\DTO; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | /** |
| 12 | * Data Transfer Object representing a user preference item. |
| 13 | * |
| 14 | * @package App\Core\Preference\Application\DTO |
| 15 | */ |
| 16 | final readonly class UserPreferenceDTO |
| 17 | { |
| 18 | /** |
| 19 | * UserPreferenceDTO constructor. |
| 20 | * |
| 21 | * @param int $userId User identifier. |
| 22 | * @param string $key Preference key. |
| 23 | * @param mixed $value Preference value. |
| 24 | * @param string|null $deviceFingerprint Optional device fingerprint. |
| 25 | * @param string|null $moduleName Optional module name. |
| 26 | * @param string|null $entityType Optional entity type. |
| 27 | * @param int|null $entityId Optional entity ID. |
| 28 | */ |
| 29 | public function __construct( |
| 30 | public int $userId, |
| 31 | public string $key, |
| 32 | public mixed $value, |
| 33 | public ?string $deviceFingerprint = null, |
| 34 | public ?string $moduleName = null, |
| 35 | public ?string $entityType = null, |
| 36 | public ?int $entityId = null, |
| 37 | ) { |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Creates a DTO from an associative array. |
| 42 | * |
| 43 | * @param array<string, mixed> $data Input data payload. |
| 44 | * @param int $userId Fallback user ID. |
| 45 | * @return self Populated DTO instance. |
| 46 | */ |
| 47 | public static function fromArray(array $data, int $userId): self |
| 48 | { |
| 49 | return new self( |
| 50 | userId: (int) ($data['user_id'] ?? $userId), |
| 51 | key: (string) ($data['preference_key'] ?? $data['key'] ?? ''), |
| 52 | value: $data['preference_value'] ?? $data['value'] ?? null, |
| 53 | deviceFingerprint: isset($data['device_fingerprint']) ? (string) $data['device_fingerprint'] : null, |
| 54 | moduleName: isset($data['module_name']) ? (string) $data['module_name'] : null, |
| 55 | entityType: isset($data['entity_type']) ? (string) $data['entity_type'] : null, |
| 56 | entityId: isset($data['entity_id']) ? (int) $data['entity_id'] : null, |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Converts DTO to array representation. |
| 62 | * |
| 63 | * @return array<string, mixed> Key-value array representation. |
| 64 | */ |
| 65 | public function toArray(): array |
| 66 | { |
| 67 | return [ |
| 68 | 'user_id' => $this->userId, |
| 69 | 'preference_key' => $this->key, |
| 70 | 'preference_value' => $this->value, |
| 71 | 'device_fingerprint' => $this->deviceFingerprint, |
| 72 | 'module_name' => $this->moduleName, |
| 73 | 'entity_type' => $this->entityType, |
| 74 | 'entity_id' => $this->entityId, |
| 75 | ]; |
| 76 | } |
| 77 | } |