1
1
<?php
2
2
3
- declare (strict_types=1 );
4
-
5
3
namespace Nejcc \PhpDatatypes \Composite \Struct ;
6
4
7
5
use InvalidArgumentException ;
6
+ use Nejcc \PhpDatatypes \Abstract \BaseStruct ;
8
7
9
- final class Struct
8
+ final class Struct extends BaseStruct
10
9
{
11
- /**
12
-
13
- * @var string
14
- */
15
- public string $ name ;
16
- /**
17
- * The array that stores field definitions and their values.
18
- *
19
- * @var array<string, array{type: string, value: mixed}>
20
- */
21
- private array $ fields ;
22
-
23
10
/**
24
11
* Struct constructor.
25
12
*
26
- * Initializes the struct with the provided fields and types.
27
- *
28
13
* @param array<string, string> $fields Array of field names and their expected types.
29
14
*/
30
15
public function __construct (array $ fields )
31
16
{
32
- $ this ->fields = [];
33
17
foreach ($ fields as $ name => $ type ) {
34
18
$ this ->addField ($ name , $ type );
35
19
}
36
20
}
37
21
38
22
/**
39
-
40
- * Add a new field to the struct.
41
- *
42
- * Adds a field to the struct with its specified type and initializes it with a null value.
43
- *
44
- * @param string $name The name of the field.
45
- * @param string $type The expected type of the field.
46
- * @return void
47
- */
48
- private function addField (string $ name , string $ type ): void
49
- {
50
- if (isset ($ this ->fields [$ name ])) {
51
- throw new InvalidArgumentException ("Field ' $ name' already exists in the struct. " );
52
- }
53
-
54
- $ this ->fields [$ name ] = [
55
- 'type ' => $ type ,
56
- 'value ' => null
57
- ];
58
- }
59
-
60
- /**
61
- * Set the value of a field, ensuring it matches the expected type.
62
- *
63
- * Validates that the provided value matches the expected type of the field.
64
- *
65
- * @param string $name The field name.
66
- * @param mixed $value The value to set.
67
- * @return void
68
- *
69
- * @throws InvalidArgumentException if the field doesn't exist or the value type doesn't match.
23
+ * {@inheritDoc}
70
24
*/
71
25
public function set (string $ name , mixed $ value ): void
72
26
{
@@ -85,7 +39,6 @@ public function set(string $name, mixed $value): void
85
39
86
40
$ baseType = $ this ->stripNullable ($ expectedType );
87
41
88
- // Strict type check with class inheritance support
89
42
if ($ actualType !== $ baseType && !is_subclass_of ($ value , $ baseType )) {
90
43
throw new InvalidArgumentException ("Field ' $ name' expects type ' $ expectedType', but got ' $ actualType'. " );
91
44
}
@@ -94,14 +47,7 @@ public function set(string $name, mixed $value): void
94
47
}
95
48
96
49
/**
97
- * Get the value of a field.
98
- *
99
- * Retrieves the value of the field, throwing an exception if the field doesn't exist.
100
- *
101
- * @param string $name The field name.
102
- * @return mixed The field value.
103
- *
104
- * @throws InvalidArgumentException if the field doesn't exist.
50
+ * {@inheritDoc}
105
51
*/
106
52
public function get (string $ name ): mixed
107
53
{
@@ -113,50 +59,16 @@ public function get(string $name): mixed
113
59
}
114
60
115
61
/**
116
-
117
- * Get all fields in the struct.
118
- *
119
- * Returns the entire set of fields in the struct along with their types and values.
120
- *
121
- * @return array<string, array{type: string, value: mixed}> The fields with their respective types and values.
122
-
62
+ * {@inheritDoc}
123
63
*/
124
64
public function getFields (): array
125
65
{
126
66
return $ this ->fields ;
127
67
}
128
68
129
- /**
130
- * Check if the type is nullable (e.g., `?string`).
131
- *
132
- * Determines if the field type allows null values.
133
- *
134
- * @param string $type The field type.
135
- * @return bool True if the type is nullable, false otherwise.
136
- */
137
- private function isNullable (string $ type ): bool
138
- {
139
- return str_starts_with ($ type , '? ' );
140
- }
141
-
142
- /**
143
- * Strip the nullable symbol (`?`) from a type.
144
- *
145
- * Removes the nullable marker from the type to check the base type.
146
- *
147
- * @param string $type The field type.
148
- * @return string The base type.
149
- */
150
- private function stripNullable (string $ type ): string
151
- {
152
- return ltrim ($ type , '? ' );
153
- }
154
-
155
69
/**
156
70
* Magic method for accessing fields like object properties.
157
71
*
158
- * This method allows accessing fields as if they were public properties.
159
- *
160
72
* @param string $name The field name.
161
73
* @return mixed The field value.
162
74
*
@@ -170,8 +82,6 @@ public function __get(string $name): mixed
170
82
/**
171
83
* Magic method for setting fields like object properties.
172
84
*
173
- * This method allows setting fields as if they were public properties.
174
- *
175
85
* @param string $name The field name.
176
86
* @param mixed $value The field value.
177
87
* @return void
0 commit comments