11<?php
22
3+ declare (strict_types=1 );
4+
35use Nejcc \PhpDatatypes \Composite \Arrays \ByteSlice ;
46use Nejcc \PhpDatatypes \Composite \Arrays \FloatArray ;
57use Nejcc \PhpDatatypes \Composite \Arrays \IntArray ;
@@ -32,133 +34,163 @@ final class TestExamples
3234 private Dictionary $ dictionary ;
3335 private Struct $ struct ;
3436
37+ /**
38+ * Create a new TestExamples instance.
39+ * This constructor initializes all data fields by calling setup methods.
40+ * Each field represents different data types for practical examples.
41+ *
42+ * @return void
43+ */
3544 public function __construct ()
3645 {
46+ $ this ->initScalarTypes ();
47+ $ this ->initCompositeTypes ();
48+ $ this ->initStruct ();
49+ }
50+
51+ /**
52+ * Initialize scalar data types.
53+ * Scalar types represent individual values like numbers, bytes, and characters.
54+ * They are used to handle simple and atomic values within the system.
55+ *
56+ * @return void
57+ */
58+ private function initScalarTypes (): void
59+ {
60+ /**
61+ * 8-bit signed integer
62+ * Represents the number of years, restricted to small values.
63+ * Useful for handling small numeric ranges.
64+ */
3765 $ this ->years = int8 (33 );
66+
67+ /**
68+ * 32-bit unsigned integer
69+ * Holds large positive numbers like account numbers.
70+ * Commonly used in systems where IDs need strict type enforcement.
71+ */
3872 $ this ->account_number = uint32 (343233 );
39- // Scalar Types
73+
74+ /**
75+ * 32-bit floating-point number
76+ * Stores monetary values with less precision.
77+ * Suitable for balances that don't require high precision.
78+ */
4079 $ this ->account_balance = float32 (1234.56 );
80+
81+ /**
82+ * 64-bit floating-point number
83+ * High-precision number used for large financial figures like investments.
84+ * Ensures accurate representation of complex decimals.
85+ */
4186 $ this ->investment_amount = float64 (78910.12345 );
42- $ this ->grade = new Char ('A ' );
43- $ this ->age = new Byte (25 );
4487
45- // Composite Types (Arrays)
46- $ this ->names = new StringArray (['John ' , 'Jane ' , 'Doe ' ]);
47- $ this ->scores = new IntArray ([100 , 95 , 87 ]);
48- $ this ->weights = new FloatArray ([60.5 , 72.3 , 88.9 ]);
49- $ this ->data = new ByteSlice ([255 , 128 , 0 ]);
88+ /**
89+ * Single character type
90+ * Holds a grade or other single character values.
91+ * Useful in cases where just one symbol needs storage.
92+ */
93+ $ this ->grade = char ('A ' );
94+
95+ /**
96+ * Byte (unsigned 8-bit integer)
97+ * Represents an age or other small number in a compact form.
98+ * Often used in systems with limited memory or for byte operations.
99+ */
100+ $ this ->age = byte (25 );
101+ }
102+
103+ /**
104+ * Initialize composite data types (arrays, lists, dictionaries).
105+ * Composite types represent collections of multiple elements.
106+ * They are used to store structured or related data, like names or scores.
107+ *
108+ * @return void
109+ */
110+ private function initCompositeTypes (): void
111+ {
112+ /**
113+ * Array of strings
114+ * Stores multiple names, useful for handling collections of textual data.
115+ * Commonly used to represent names in contact lists or similar collections.
116+ */
117+ $ this ->names = stringArray (['John ' , 'Jane ' , 'Doe ' ]);
118+
119+ /**
120+ * Array of integers
121+ * Holds multiple scores, useful for exam or performance evaluations.
122+ * Ensures all elements in the array are integers.
123+ */
124+ $ this ->scores = intArray ([100 , 95 , 87 ]);
50125
51- $ this ->listData = new ListData (['apple ' , 'banana ' , 'orange ' ]);
52- $ this ->dictionary = new Dictionary (['name ' => 'Nejc ' , 'age ' => 99 , 'country ' => 'Slovenia ' ]);
126+ /**
127+ * Array of floats
128+ * Contains floating-point values like weights.
129+ * Useful in systems that require collections of decimal numbers.
130+ */
131+ $ this ->weights = floatArray ([60.5 , 72.3 , 88.9 ]);
132+
133+ /**
134+ * Byte array
135+ * Holds a sequence of raw bytes.
136+ * Often used for storing binary data, such as file contents or encoded data.
137+ */
138+ $ this ->data = byteSlice ([255 , 128 , 0 ]);
139+
140+ /**
141+ * List of strings
142+ * Stores elements like fruit names, representing simple ordered collections.
143+ * Useful for maintaining ordered lists in a system.
144+ */
145+ $ this ->listData = listData (['apple ' , 'banana ' , 'orange ' ]);
146+
147+ /**
148+ * Dictionary with key-value pairs
149+ * Stores user information like name, age, and country.
150+ * Used when you need to access data by keys rather than indexes.
151+ */
152+ $ this ->dictionary = dictionary ([
153+ 'name ' => 'Nejc ' ,
154+ 'age ' => 99 ,
155+ 'country ' => 'Slovenia '
156+ ]);
157+ }
53158
54- $ this ->struct = new Struct ([
159+ /**
160+ * Initialize structured data types (Struct).
161+ * Structs allow for grouping data into named fields with specific types.
162+ * They are useful for representing records or objects with fixed attributes.
163+ *
164+ * @return void
165+ */
166+ private function initStruct (): void
167+ {
168+ /**
169+ * Struct definition with named fields
170+ * Used to group related fields into a single entity (e.g., a user profile).
171+ * Each field has a specific type, ensuring type-safety across attributes.
172+ */
173+ $ this ->struct = struct ([
55174 'name ' => 'string ' ,
56175 'age ' => 'int ' ,
57176 'balance ' => 'float '
58177 ]);
59178
60- // Setting field values
179+ // Set initial values for struct fields.
61180 $ this ->struct ->set ('name ' , 'Nejc ' );
62181 $ this ->struct ->set ('age ' , 30 );
63182 $ this ->struct ->set ('balance ' , 250.75 );
64-
65-
66183 }
67184
185+ /**
186+ * Retrieve all example data as an array.
187+ * This method returns the initialized scalar, composite, and structured data.
188+ * Can be used to display or process the data in various parts of the system.
189+ *
190+ * @return array
191+ */
68192 public function getExamples (): array
69193 {
70- /*
71- * Test only
72-
73- array (size=14)
74- 'years' =>
75- object(Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8)[4]
76- protected readonly int 'value' => int 33
77- 'account_number' =>
78- object(Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt32)[5]
79- protected readonly int 'value' => int 343233
80- 'account_balance' =>
81- object(Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32)[6]
82- protected readonly float 'value' => float 1234.56
83- 'investment_amount' =>
84- object(Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64)[7]
85- protected readonly float 'value' => float 78910.12345
86- 'grade' =>
87- object(Nejcc\PhpDatatypes\Scalar\Char)[8]
88- private string 'value' => string 'A' (length=1)
89- 'age' =>
90- object(Nejcc\PhpDatatypes\Scalar\Byte)[9]
91- private int 'value' => int 25
92- 'names' =>
93- object(Nejcc\PhpDatatypes\Composite\Arrays\StringArray)[10]
94- private array 'value' =>
95- array (size=3)
96- 0 => string 'John' (length=4)
97- 1 => string 'Jane' (length=4)
98- 2 => string 'Doe' (length=3)
99- 'scores' =>
100- object(Nejcc\PhpDatatypes\Composite\Arrays\IntArray)[11]
101- private array 'value' =>
102- array (size=3)
103- 0 => int 100
104- 1 => int 95
105- 2 => int 87
106- 'weights' =>
107- object(Nejcc\PhpDatatypes\Composite\Arrays\FloatArray)[12]
108- private array 'value' =>
109- array (size=3)
110- 0 => float 60.5
111- 1 => float 72.3
112- 2 => float 88.9
113- 'data' =>
114- object(Nejcc\PhpDatatypes\Composite\Arrays\ByteSlice)[13]
115- private array 'value' =>
116- array (size=3)
117- 0 => int 255
118- 1 => int 128
119- 2 => int 0
120- 'listData' =>
121- object(Nejcc\PhpDatatypes\Composite\ListData)[14]
122- private array 'elements' =>
123- array (size=3)
124- 0 => string 'apple' (length=5)
125- 1 => string 'banana' (length=6)
126- 2 => string 'orange' (length=6)
127- 'dictionary' =>
128- object(Nejcc\PhpDatatypes\Composite\Dictionary)[15]
129- private array 'elements' =>
130- array (size=3)
131- 'name' => string 'Nejc' (length=4)
132- 'age' => int 99
133- 'country' => string 'Slovenia' (length=8)
134- 'struct' =>
135- object(Nejcc\PhpDatatypes\Composite\Struct\Struct)[16]
136- private array 'fields' =>
137- array (size=3)
138- 'name' =>
139- array (size=2)
140- ...
141- 'age' =>
142- array (size=2)
143- ...
144- 'balance' =>
145- array (size=2)
146- ...
147- 'struct_all' =>
148- array (size=3)
149- 'name' =>
150- array (size=2)
151- 'type' => string 'string' (length=6)
152- 'value' => string 'Nejc' (length=4)
153- 'age' =>
154- array (size=2)
155- 'type' => string 'int' (length=3)
156- 'value' => int 30
157- 'balance' =>
158- array (size=2)
159- 'type' => string 'float' (length=5)
160- 'value' => float 250.75
161- */
162194 return [
163195 'years ' => $ this ->years ,
164196 'account_number ' => $ this ->account_number ,
@@ -173,11 +205,14 @@ public function getExamples(): array
173205 'listData ' => $ this ->listData ,
174206 'dictionary ' => $ this ->dictionary ,
175207 'struct ' => $ this ->struct ,
176- 'struct_all ' => $ this ->struct ->getFields (),
208+ 'struct_all ' => $ this ->struct ->getFields (), // All fields in the struct
177209 ];
178210 }
179211}
180212
181213// Instantiate the class and invoke the examples
182214$ example = new TestExamples ();
215+
216+ // Display the example data
183217var_dump ($ example ->getExamples ());
218+
0 commit comments