Skip to content

Commit 5a585d1

Browse files
committed
update on struct
1 parent e3aa440 commit 5a585d1

File tree

3 files changed

+90
-95
lines changed

3 files changed

+90
-95
lines changed

src/Abstract/BaseStruct.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Nejcc\PhpDatatypes\Abstract;
4+
5+
use InvalidArgumentException;
6+
use Nejcc\PhpDatatypes\Interfaces\StructInterface;
7+
8+
abstract class BaseStruct implements StructInterface
9+
{
10+
/**
11+
* @var array<string, array{type: string, value: mixed}>
12+
*/
13+
protected array $fields = [];
14+
15+
/**
16+
* Add a new field to the struct.
17+
*
18+
* @param string $name The name of the field.
19+
* @param string $type The expected type of the field.
20+
* @return void
21+
*/
22+
protected function addField(string $name, string $type): void
23+
{
24+
if (isset($this->fields[$name])) {
25+
throw new InvalidArgumentException("Field '$name' already exists in the struct.");
26+
}
27+
28+
$this->fields[$name] = [
29+
'type' => $type,
30+
'value' => null
31+
];
32+
}
33+
34+
/**
35+
* Check if the type is nullable (e.g., `?string`).
36+
*
37+
* @param string $type The field type.
38+
* @return bool True if the type is nullable, false otherwise.
39+
*/
40+
protected function isNullable(string $type): bool
41+
{
42+
return str_starts_with($type, '?');
43+
}
44+
45+
/**
46+
* Strip the nullable symbol (`?`) from a type.
47+
*
48+
* @param string $type The field type.
49+
* @return string The base type.
50+
*/
51+
protected function stripNullable(string $type): string
52+
{
53+
return ltrim($type, '?');
54+
}
55+
}

src/Composite/Struct/Struct.php

+5-95
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,26 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
namespace Nejcc\PhpDatatypes\Composite\Struct;
64

75
use 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

src/Interfaces/StructInterface.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Nejcc\PhpDatatypes\Interfaces;
4+
5+
interface StructInterface
6+
{
7+
/**
8+
* Set the value of a field.
9+
*
10+
* @param string $name The field name.
11+
* @param mixed $value The value to set.
12+
* @return void
13+
*/
14+
public function set(string $name, mixed $value): void;
15+
16+
/**
17+
* Get the value of a field.
18+
*
19+
* @param string $name The field name.
20+
* @return mixed The field value.
21+
*/
22+
public function get(string $name): mixed;
23+
24+
/**
25+
* Get all fields in the struct.
26+
*
27+
* @return array<string, array{type: string, value: mixed}> The fields with their respective types and values.
28+
*/
29+
public function getFields(): array;
30+
}

0 commit comments

Comments
 (0)