11<?php
22
3- declare (strict_types=1 );
4-
53namespace Nejcc \PhpDatatypes \Composite \Struct ;
64
75use InvalidArgumentException ;
6+ use Nejcc \PhpDatatypes \Abstract \BaseStruct ;
87
9- final class Struct
8+ final class Struct extends BaseStruct
109{
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-
2310 /**
2411 * Struct constructor.
2512 *
26- * Initializes the struct with the provided fields and types.
27- *
2813 * @param array<string, string> $fields Array of field names and their expected types.
2914 */
3015 public function __construct (array $ fields )
3116 {
32- $ this ->fields = [];
3317 foreach ($ fields as $ name => $ type ) {
3418 $ this ->addField ($ name , $ type );
3519 }
3620 }
3721
3822 /**
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}
7024 */
7125 public function set (string $ name , mixed $ value ): void
7226 {
@@ -85,7 +39,6 @@ public function set(string $name, mixed $value): void
8539
8640 $ baseType = $ this ->stripNullable ($ expectedType );
8741
88- // Strict type check with class inheritance support
8942 if ($ actualType !== $ baseType && !is_subclass_of ($ value , $ baseType )) {
9043 throw new InvalidArgumentException ("Field ' $ name' expects type ' $ expectedType', but got ' $ actualType'. " );
9144 }
@@ -94,14 +47,7 @@ public function set(string $name, mixed $value): void
9447 }
9548
9649 /**
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}
10551 */
10652 public function get (string $ name ): mixed
10753 {
@@ -113,50 +59,16 @@ public function get(string $name): mixed
11359 }
11460
11561 /**
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}
12363 */
12464 public function getFields (): array
12565 {
12666 return $ this ->fields ;
12767 }
12868
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-
15569 /**
15670 * Magic method for accessing fields like object properties.
15771 *
158- * This method allows accessing fields as if they were public properties.
159- *
16072 * @param string $name The field name.
16173 * @return mixed The field value.
16274 *
@@ -170,8 +82,6 @@ public function __get(string $name): mixed
17082 /**
17183 * Magic method for setting fields like object properties.
17284 *
173- * This method allows setting fields as if they were public properties.
174- *
17585 * @param string $name The field name.
17686 * @param mixed $value The field value.
17787 * @return void
0 commit comments