Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
43.48% |
10 / 23 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| SystemSettingData | |
43.48% |
10 / 23 |
|
0.00% |
0 / 3 |
12.50 | |
0.00% |
0 / 1 |
| __construct | |
76.92% |
10 / 13 |
|
0.00% |
0 / 1 |
4.20 | |||
| getValue | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| jsonSerialize | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Domain\SystemSettings\Data; |
| 6 | |
| 7 | use App\Support\Row; |
| 8 | use JsonSerializable; |
| 9 | |
| 10 | /** |
| 11 | * Data transfer object for system settings. |
| 12 | */ |
| 13 | final class SystemSettingData implements JsonSerializable |
| 14 | { |
| 15 | public int $settingId; |
| 16 | |
| 17 | public string $settingKey; |
| 18 | |
| 19 | /** @var array<mixed> */ |
| 20 | public array $settingValue; |
| 21 | |
| 22 | public ?string $description; |
| 23 | |
| 24 | public ?int $updatedBy; |
| 25 | |
| 26 | public string $createdAt; |
| 27 | |
| 28 | public string $updatedAt; |
| 29 | |
| 30 | /** |
| 31 | * @param array<mixed> $data |
| 32 | */ |
| 33 | public function __construct(array $data) |
| 34 | { |
| 35 | $this->settingId = Row::int($data, 'settingId'); |
| 36 | $this->settingKey = Row::string($data, 'settingKey'); |
| 37 | |
| 38 | // Handle JSONB - might be a string (raw from PDO) or already decoded |
| 39 | $value = $data['settingValue'] ?? null; |
| 40 | if (is_string($value)) { |
| 41 | $decoded = json_decode($value, true); |
| 42 | $this->settingValue = is_array($decoded) ? $decoded : ['value' => $value]; |
| 43 | } elseif (is_array($value)) { |
| 44 | $this->settingValue = $value; |
| 45 | } else { |
| 46 | $this->settingValue = ['value' => $value]; |
| 47 | } |
| 48 | |
| 49 | $this->description = Row::nullableString($data, 'description'); |
| 50 | $this->updatedBy = Row::nullableInt($data, 'updatedBy'); |
| 51 | $this->createdAt = Row::string($data, 'createdAt'); |
| 52 | $this->updatedAt = Row::string($data, 'updatedAt'); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get the primary value from the JSONB setting. |
| 57 | */ |
| 58 | public function getValue(): mixed |
| 59 | { |
| 60 | return $this->settingValue['value'] ?? null; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return array<string, mixed> |
| 65 | */ |
| 66 | public function jsonSerialize(): array |
| 67 | { |
| 68 | return [ |
| 69 | 'settingId' => $this->settingId, |
| 70 | 'settingKey' => $this->settingKey, |
| 71 | 'settingValue' => $this->settingValue, |
| 72 | 'description' => $this->description, |
| 73 | 'updatedBy' => $this->updatedBy, |
| 74 | 'createdAt' => $this->createdAt, |
| 75 | 'updatedAt' => $this->updatedAt, |
| 76 | ]; |
| 77 | } |
| 78 | } |