Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.24% |
20 / 21 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ParameterResource | |
95.24% |
20 / 21 |
|
50.00% |
1 / 2 |
9 | |
0.00% |
0 / 1 |
| toArray | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| getValueType | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
8.03 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Resources\v2; |
| 4 | |
| 5 | use Illuminate\Http\Request; |
| 6 | use Illuminate\Http\Resources\Json\JsonResource; |
| 7 | |
| 8 | /** |
| 9 | * Resource for transforming Parameter models for API responses. |
| 10 | * |
| 11 | * @property string $_id The parameter ID |
| 12 | * @property string $name The parameter name/key |
| 13 | * @property mixed $value The parameter value (polymorphic) |
| 14 | * @property \Carbon\Carbon|null $created_at When the parameter was created |
| 15 | * @property \Carbon\Carbon|null $updated_at When the parameter was last updated |
| 16 | */ |
| 17 | class ParameterResource extends JsonResource |
| 18 | { |
| 19 | /** |
| 20 | * Transform the resource into an array. |
| 21 | * |
| 22 | * @return array<string, mixed> |
| 23 | */ |
| 24 | public function toArray(Request $request): array |
| 25 | { |
| 26 | return [ |
| 27 | 'id' => (string) $this->_id, |
| 28 | 'name' => $this->name, |
| 29 | 'value' => $this->value, |
| 30 | 'value_type' => $this->getValueType($this->value), |
| 31 | 'created_at' => $this->created_at?->timestamp, |
| 32 | 'updated_at' => $this->updated_at?->timestamp, |
| 33 | ]; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Determine the type of the value for frontend type handling. |
| 38 | * |
| 39 | * @param mixed $value The value to check |
| 40 | * @return string The type name (string, number, boolean, array, object, null) |
| 41 | */ |
| 42 | private function getValueType(mixed $value): string |
| 43 | { |
| 44 | if (is_null($value)) { |
| 45 | return 'null'; |
| 46 | } |
| 47 | |
| 48 | if (is_bool($value)) { |
| 49 | return 'boolean'; |
| 50 | } |
| 51 | |
| 52 | if (is_int($value) || is_float($value)) { |
| 53 | return 'number'; |
| 54 | } |
| 55 | |
| 56 | if (is_string($value)) { |
| 57 | return 'string'; |
| 58 | } |
| 59 | |
| 60 | if (is_array($value)) { |
| 61 | // Check if it's an associative array (object) or indexed array |
| 62 | if (array_is_list($value)) { |
| 63 | return 'array'; |
| 64 | } |
| 65 | |
| 66 | return 'object'; |
| 67 | } |
| 68 | |
| 69 | return 'string'; |
| 70 | } |
| 71 | } |