diff --git a/src/Abstract/BaseStruct.php b/src/Abstract/BaseStruct.php new file mode 100644 index 0000000..c15df7b --- /dev/null +++ b/src/Abstract/BaseStruct.php @@ -0,0 +1,55 @@ + + */ + protected array $fields = []; + + /** + * Add a new field to the struct. + * + * @param string $name The name of the field. + * @param string $type The expected type of the field. + * @return void + */ + protected function addField(string $name, string $type): void + { + if (isset($this->fields[$name])) { + throw new InvalidArgumentException("Field '$name' already exists in the struct."); + } + + $this->fields[$name] = [ + 'type' => $type, + 'value' => null + ]; + } + + /** + * Check if the type is nullable (e.g., `?string`). + * + * @param string $type The field type. + * @return bool True if the type is nullable, false otherwise. + */ + protected function isNullable(string $type): bool + { + return str_starts_with($type, '?'); + } + + /** + * Strip the nullable symbol (`?`) from a type. + * + * @param string $type The field type. + * @return string The base type. + */ + protected function stripNullable(string $type): string + { + return ltrim($type, '?'); + } +} diff --git a/src/Composite/Struct/Struct.php b/src/Composite/Struct/Struct.php index e12811b..bd68945 100644 --- a/src/Composite/Struct/Struct.php +++ b/src/Composite/Struct/Struct.php @@ -1,72 +1,26 @@ - */ - private array $fields; - /** * Struct constructor. * - * Initializes the struct with the provided fields and types. - * * @param array $fields Array of field names and their expected types. */ public function __construct(array $fields) { - $this->fields = []; foreach ($fields as $name => $type) { $this->addField($name, $type); } } /** - - * Add a new field to the struct. - * - * Adds a field to the struct with its specified type and initializes it with a null value. - * - * @param string $name The name of the field. - * @param string $type The expected type of the field. - * @return void - */ - private function addField(string $name, string $type): void - { - if (isset($this->fields[$name])) { - throw new InvalidArgumentException("Field '$name' already exists in the struct."); - } - - $this->fields[$name] = [ - 'type' => $type, - 'value' => null - ]; - } - - /** - * Set the value of a field, ensuring it matches the expected type. - * - * Validates that the provided value matches the expected type of the field. - * - * @param string $name The field name. - * @param mixed $value The value to set. - * @return void - * - * @throws InvalidArgumentException if the field doesn't exist or the value type doesn't match. + * {@inheritDoc} */ public function set(string $name, mixed $value): void { @@ -85,7 +39,6 @@ public function set(string $name, mixed $value): void $baseType = $this->stripNullable($expectedType); - // Strict type check with class inheritance support if ($actualType !== $baseType && !is_subclass_of($value, $baseType)) { throw new InvalidArgumentException("Field '$name' expects type '$expectedType', but got '$actualType'."); } @@ -94,14 +47,7 @@ public function set(string $name, mixed $value): void } /** - * Get the value of a field. - * - * Retrieves the value of the field, throwing an exception if the field doesn't exist. - * - * @param string $name The field name. - * @return mixed The field value. - * - * @throws InvalidArgumentException if the field doesn't exist. + * {@inheritDoc} */ public function get(string $name): mixed { @@ -113,50 +59,16 @@ public function get(string $name): mixed } /** - - * Get all fields in the struct. - * - * Returns the entire set of fields in the struct along with their types and values. - * - * @return array The fields with their respective types and values. - + * {@inheritDoc} */ public function getFields(): array { return $this->fields; } - /** - * Check if the type is nullable (e.g., `?string`). - * - * Determines if the field type allows null values. - * - * @param string $type The field type. - * @return bool True if the type is nullable, false otherwise. - */ - private function isNullable(string $type): bool - { - return str_starts_with($type, '?'); - } - - /** - * Strip the nullable symbol (`?`) from a type. - * - * Removes the nullable marker from the type to check the base type. - * - * @param string $type The field type. - * @return string The base type. - */ - private function stripNullable(string $type): string - { - return ltrim($type, '?'); - } - /** * Magic method for accessing fields like object properties. * - * This method allows accessing fields as if they were public properties. - * * @param string $name The field name. * @return mixed The field value. * @@ -170,8 +82,6 @@ public function __get(string $name): mixed /** * Magic method for setting fields like object properties. * - * This method allows setting fields as if they were public properties. - * * @param string $name The field name. * @param mixed $value The field value. * @return void diff --git a/src/Interfaces/StructInterface.php b/src/Interfaces/StructInterface.php new file mode 100644 index 0000000..f8ea059 --- /dev/null +++ b/src/Interfaces/StructInterface.php @@ -0,0 +1,30 @@ + The fields with their respective types and values. + */ + public function getFields(): array; +}