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