Skip to content

Commit 2b94c9c

Browse files
committed
wip update
1 parent 9d85232 commit 2b94c9c

File tree

6 files changed

+247
-12
lines changed

6 files changed

+247
-12
lines changed

index.php

+27-3
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44
use Nejcc\PhpDatatypes\Composite\Arrays\FloatArray;
55
use Nejcc\PhpDatatypes\Composite\Arrays\IntArray;
66
use Nejcc\PhpDatatypes\Composite\Arrays\StringArray;
7+
use Nejcc\PhpDatatypes\Composite\Dictionary;
8+
use Nejcc\PhpDatatypes\Composite\ListData;
9+
use Nejcc\PhpDatatypes\Composite\Struct\Struct;
710
use Nejcc\PhpDatatypes\Scalar\Byte;
11+
use Nejcc\PhpDatatypes\Scalar\Char;
812
use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;
913
use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64;
10-
use Nejcc\PhpDatatypes\Scalar\Char;
11-
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int16;
1214
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8;
1315
use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt32;
1416

1517
require_once __DIR__ . '/vendor/autoload.php';
1618

17-
class TestExamples
19+
final class TestExamples
1820
{
1921
private Float32 $account_balance;
2022
private Float64 $investment_amount;
@@ -26,6 +28,9 @@ class TestExamples
2628
private ByteSlice $data;
2729
private Int8 $years;
2830
private UInt32 $account_number;
31+
private ListData $listData;
32+
private Dictionary $dictionary;
33+
private Struct $struct;
2934

3035
public function __construct()
3136
{
@@ -43,6 +48,21 @@ public function __construct()
4348
$this->weights = new FloatArray([60.5, 72.3, 88.9]);
4449
$this->data = new ByteSlice([255, 128, 0]);
4550

51+
$this->listData = new ListData(['apple', 'banana', 'orange']);
52+
$this->dictionary = new Dictionary(['name' => 'Nejc', 'age' => 99, 'country' => 'Slovenia']);
53+
54+
$this->struct = new Struct([
55+
'name' => 'string',
56+
'age' => 'int',
57+
'balance' => 'float'
58+
]);
59+
60+
// Setting field values
61+
$this->struct->set('name', 'Nejc');
62+
$this->struct->set('age', 30);
63+
$this->struct->set('balance', 250.75);
64+
65+
4666
}
4767

4868
public function getExamples(): array
@@ -108,6 +128,10 @@ public function getExamples(): array
108128
'scores' => $this->scores,
109129
'weights' => $this->weights,
110130
'data' => $this->data,
131+
'listData' => $this->listData,
132+
'dictionary' => $this->dictionary,
133+
'struct' => $this->struct,
134+
'struct_all' => $this->struct->getFields(),
111135
];
112136
}
113137
}

src/Composite/Dictionary.php

+71
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,78 @@
22

33
namespace Nejcc\PhpDatatypes\Composite;
44

5+
use InvalidArgumentException;
6+
use OutOfBoundsException;
7+
58
class Dictionary
69
{
10+
private array $elements;
11+
12+
public function __construct(array $elements = [])
13+
{
14+
// Validate that the initial elements are associative
15+
foreach ($elements as $key => $value) {
16+
if (is_int($key)) {
17+
throw new InvalidArgumentException("Dictionary keys must be non-integer (string) keys.");
18+
}
19+
}
20+
21+
$this->elements = $elements;
22+
}
23+
24+
// Add a key-value pair to the dictionary
25+
public function add(string $key, $value): void
26+
{
27+
$this->elements[$key] = $value;
28+
}
29+
30+
// Get the value associated with a key
31+
public function get(string $key)
32+
{
33+
if (!isset($this->elements[$key])) {
34+
throw new OutOfBoundsException("Key '$key' does not exist in the dictionary.");
35+
}
36+
37+
return $this->elements[$key];
38+
}
39+
40+
// Remove a key-value pair by the key
41+
public function remove(string $key): void
42+
{
43+
if (!isset($this->elements[$key])) {
44+
throw new OutOfBoundsException("Key '$key' does not exist in the dictionary.");
45+
}
46+
47+
unset($this->elements[$key]);
48+
}
49+
50+
// Check if a key exists in the dictionary
51+
public function containsKey(string $key): bool
52+
{
53+
return array_key_exists($key, $this->elements);
54+
}
55+
56+
// Get all keys in the dictionary
57+
public function getKeys(): array
58+
{
59+
return array_keys($this->elements);
60+
}
61+
62+
// Get all values in the dictionary
63+
public function getValues(): array
64+
{
65+
return array_values($this->elements);
66+
}
67+
68+
// Get the size of the dictionary
69+
public function size(): int
70+
{
71+
return count($this->elements);
72+
}
773

74+
// Clear the dictionary
75+
public function clear(): void
76+
{
77+
$this->elements = [];
78+
}
879
}

src/Composite/ListData.php

+57
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,64 @@
22

33
namespace Nejcc\PhpDatatypes\Composite;
44

5+
use OutOfBoundsException;
6+
57
class ListData
68
{
9+
private array $elements;
10+
11+
public function __construct(array $elements = [])
12+
{
13+
$this->elements = $elements;
14+
}
15+
16+
// Add an element to the list
17+
public function add($element): void
18+
{
19+
$this->elements[] = $element;
20+
}
21+
22+
// Remove an element by its index
23+
public function remove(int $index): void
24+
{
25+
if (!isset($this->elements[$index])) {
26+
throw new OutOfBoundsException("No element at index $index.");
27+
}
28+
29+
array_splice($this->elements, $index, 1);
30+
}
31+
32+
// Get an element by its index
33+
public function get(int $index)
34+
{
35+
if (!isset($this->elements[$index])) {
36+
throw new OutOfBoundsException("No element at index $index.");
37+
}
38+
39+
return $this->elements[$index];
40+
}
41+
42+
// Get the entire list as an array
43+
public function getAll(): array
44+
{
45+
return $this->elements;
46+
}
47+
48+
// Check if the list contains an element
49+
public function contains($element): bool
50+
{
51+
return in_array($element, $this->elements, true);
52+
}
53+
54+
// Get the size of the list
55+
public function size(): int
56+
{
57+
return count($this->elements);
58+
}
759

60+
// Clear the list
61+
public function clear(): void
62+
{
63+
$this->elements = [];
64+
}
865
}

src/Composite/Struct/Struct.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Nejcc\PhpDatatypes\Composite\Struct;
4+
5+
use InvalidArgumentException;
6+
7+
final class Struct
8+
{
9+
private array $fields = [];
10+
11+
public function __construct(array $fields)
12+
{
13+
foreach ($fields as $name => $type) {
14+
$this->addField($name, $type);
15+
}
16+
}
17+
18+
// Add a field with a specific type
19+
public function addField(string $name, string $type): void
20+
{
21+
$this->fields[$name] = [
22+
'type' => $type,
23+
'value' => null
24+
];
25+
}
26+
27+
// Set the value of a field, ensuring it matches the type
28+
public function set(string $name, $value): void
29+
{
30+
if (!isset($this->fields[$name])) {
31+
throw new InvalidArgumentException("Field $name does not exist in the struct.");
32+
}
33+
34+
$expectedType = $this->fields[$name]['type'];
35+
$actualType = get_debug_type($value);
36+
37+
// PHP 8.3 has a more robust `get_debug_type()` that supports more specific types.
38+
if ($actualType !== $expectedType && !is_subclass_of($actualType, $expectedType)) {
39+
throw new InvalidArgumentException("Field $name expects type $expectedType, but got $actualType.");
40+
}
41+
42+
$this->fields[$name]['value'] = $value;
43+
}
44+
45+
// Get the value of a field
46+
public function get(string $name)
47+
{
48+
if (!isset($this->fields[$name])) {
49+
throw new InvalidArgumentException("Field $name does not exist in the struct.");
50+
}
51+
52+
return $this->fields[$name]['value'];
53+
}
54+
55+
// Get all fields
56+
public function getFields(): array
57+
{
58+
return $this->fields;
59+
}
60+
}

src/Composite/Union/Struct.php

-8
This file was deleted.

src/Composite/Union/Union.php

+32-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,38 @@
22

33
namespace Nejcc\PhpDatatypes\Composite\Union;
44

5-
class Union
5+
use InvalidArgumentException;
6+
7+
final class Union
68
{
9+
private mixed $value;
10+
private array $allowedTypes;
11+
12+
public function __construct(array $allowedTypes)
13+
{
14+
$this->allowedTypes = $allowedTypes;
15+
}
16+
17+
// Set a value of one of the allowed types
18+
public function setValue(mixed $value): void
19+
{
20+
$this->validateType($value);
21+
$this->value = $value;
22+
}
23+
24+
// Get the value
25+
public function getValue(): mixed
26+
{
27+
return $this->value;
28+
}
29+
30+
// Validate that the value matches one of the allowed types
31+
private function validateType(mixed $value): void
32+
{
33+
$type = gettype($value);
734

35+
if (!in_array($type, $this->allowedTypes, true)) {
36+
throw new InvalidArgumentException("Invalid type: $type. Allowed types: " . implode(', ', $this->allowedTypes));
37+
}
38+
}
839
}

0 commit comments

Comments
 (0)