File tree 3 files changed +65
-14
lines changed
3 files changed +65
-14
lines changed Original file line number Diff line number Diff line change 9
9
final class Struct
10
10
{
11
11
/**
12
+
12
13
* @var string
13
14
*/
14
15
public string $ name ;
@@ -35,6 +36,7 @@ public function __construct(array $fields)
35
36
}
36
37
37
38
/**
39
+
38
40
* Add a new field to the struct.
39
41
*
40
42
* Adds a field to the struct with its specified type and initializes it with a null value.
@@ -111,11 +113,13 @@ public function get(string $name): mixed
111
113
}
112
114
113
115
/**
116
+
114
117
* Get all fields in the struct.
115
118
*
116
119
* Returns the entire set of fields in the struct along with their types and values.
117
120
*
118
121
* @return array<string, array{type: string, value: mixed}> The fields with their respective types and values.
122
+
119
123
*/
120
124
public function getFields (): array
121
125
{
Original file line number Diff line number Diff line change 6
6
7
7
final class Union
8
8
{
9
+ /**
10
+ * @var mixed
11
+ */
9
12
private mixed $ value ;
13
+
14
+ /**
15
+ * @var array
16
+ */
10
17
private array $ allowedTypes ;
11
18
19
+ /**
20
+ * @param array $allowedTypes
21
+ */
12
22
public function __construct (array $ allowedTypes )
13
23
{
14
24
$ this ->allowedTypes = $ allowedTypes ;
15
25
}
16
26
17
- // Set a value of one of the allowed types
27
+ /**
28
+ * @param mixed $value
29
+ * @return void
30
+ */
18
31
public function setValue (mixed $ value ): void
19
32
{
20
33
$ this ->validateType ($ value );
21
34
$ this ->value = $ value ;
22
35
}
23
36
24
- // Get the value
37
+ /**
38
+ * @return mixed
39
+ */
25
40
public function getValue (): mixed
26
41
{
27
42
return $ this ->value ;
28
43
}
29
44
30
- // Validate that the value matches one of the allowed types
45
+ /**
46
+ * @param mixed $value
47
+ * @return void
48
+ */
31
49
private function validateType (mixed $ value ): void
32
50
{
33
51
$ type = gettype ($ value );
Original file line number Diff line number Diff line change 21
21
use Nejcc \PhpDatatypes \Scalar \Integers \Unsigned \UInt32 ;
22
22
use Nejcc \PhpDatatypes \Scalar \Integers \Unsigned \UInt8 ;
23
23
24
+ /**
25
+ * @param int $value
26
+ * @return Int8
27
+ */
24
28
function int8 (int $ value ): Int8
25
29
{
26
30
return new Int8 ($ value );
27
31
}
28
32
33
+ /**
34
+ * @param int $value
35
+ * @return Int16
36
+ */
29
37
function int16 (int $ value ): Int16
30
38
{
31
39
return new Int16 ($ value );
32
40
}
33
41
42
+ /**
43
+ * @param int $value
44
+ * @return Int32
45
+ */
34
46
function int32 (int $ value ): Int32
35
47
{
36
48
return new Int32 ($ value );
37
49
}
38
50
51
+ /**
52
+ * @param int $value
53
+ * @return Int64
54
+ */
39
55
function int64 (int $ value ): Int64
40
56
{
41
57
return new Int64 ($ value );
42
58
}
43
59
60
+ /**
61
+ * @param int $value
62
+ * @return Int128
63
+ */
44
64
function int128 (int $ value ): Int128
45
65
{
46
66
return new Int128 ($ value );
47
67
}
48
68
69
+ /**
70
+ * @param int $value
71
+ * @return UInt8
72
+ */
49
73
function uint8 (int $ value ): UInt8
50
74
{
51
75
return new UInt8 ($ value );
52
76
}
53
77
78
+ /**
79
+ * @param int $value
80
+ * @return UInt16
81
+ */
54
82
function uint16 (int $ value ): UInt16
55
83
{
56
84
return new UInt16 ($ value );
57
85
}
58
86
87
+ /**
88
+ * @param int $value
89
+ * @return UInt32
90
+ */
59
91
function uint32 (int $ value ): UInt32
60
92
{
61
93
return new UInt32 ($ value );
62
94
}
63
95
64
- //
65
- //function uint64(int $value): Int64
66
- //{
67
- // return new UInt64($value);
68
- //}
69
- //
70
- //function uint128(int $value): Int128
71
- //{
72
- // return new UInt128($value);
73
- //}
74
-
96
+ /**
97
+ * @param float $value
98
+ * @return Float32
99
+ */
75
100
function float32 (float $ value ): Float32
76
101
{
77
102
return new Float32 ($ value );
78
103
}
79
104
105
+ /**
106
+ * @param float $value
107
+ * @return Float64
108
+ */
80
109
function float64 (float $ value ): Float64
81
110
{
82
111
return new Float64 ($ value );
You can’t perform that action at this time.
0 commit comments