Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions src/Composite/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

class Dictionary
{
private array $elements;
/**
* @var array
*/
private array $elements = [];

public function __construct(array $elements = [])
{
Expand All @@ -21,13 +24,22 @@ public function __construct(array $elements = [])
$this->elements = $elements;
}

// Add a key-value pair to the dictionary

/**
* @param string $key
* @param $value
* @return void
*/
public function add(string $key, $value): void
{
$this->elements[$key] = $value;
}

// Get the value associated with a key

/**
* @param string $key
* @return mixed
*/
public function get(string $key)
{
if (!isset($this->elements[$key])) {
Expand All @@ -37,7 +49,11 @@ public function get(string $key)
return $this->elements[$key];
}

// Remove a key-value pair by the key

/**
* @param string $key
* @return void
*/
public function remove(string $key): void
{
if (!isset($this->elements[$key])) {
Expand All @@ -47,31 +63,45 @@ public function remove(string $key): void
unset($this->elements[$key]);
}

// Check if a key exists in the dictionary
/**
* @param string $key
* @return bool
*/
public function containsKey(string $key): bool
{
return array_key_exists($key, $this->elements);
}

// Get all keys in the dictionary

/**
* @return array
*/
public function getKeys(): array
{
return array_keys($this->elements);
}

// Get all values in the dictionary

/**
* @return array
*/
public function getValues(): array
{
return array_values($this->elements);
}

// Get the size of the dictionary
/**
* @return int
*/
public function size(): int
{
return count($this->elements);
}

// Clear the dictionary

/**
* @return void
*/
public function clear(): void
{
$this->elements = [];
Expand Down
26 changes: 22 additions & 4 deletions src/Composite/Struct/Struct.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

final class Struct
{
/**
* @var array
*/
private array $fields = [];

public function __construct(array $fields)
Expand All @@ -15,7 +18,11 @@ public function __construct(array $fields)
}
}

// Add a field with a specific type
/**
* @param string $name
* @param string $type
* @return void
*/
public function addField(string $name, string $type): void
{
$this->fields[$name] = [
Expand All @@ -24,7 +31,12 @@ public function addField(string $name, string $type): void
];
}

// Set the value of a field, ensuring it matches the type

/**
* @param string $name
* @param $value
* @return void
*/
public function set(string $name, $value): void
{
if (!isset($this->fields[$name])) {
Expand All @@ -42,7 +54,11 @@ public function set(string $name, $value): void
$this->fields[$name]['value'] = $value;
}

// Get the value of a field

/**
* @param string $name
* @return mixed
*/
public function get(string $name)
{
if (!isset($this->fields[$name])) {
Expand All @@ -52,7 +68,9 @@ public function get(string $name)
return $this->fields[$name]['value'];
}

// Get all fields
/**
* @return array
*/
public function getFields(): array
{
return $this->fields;
Expand Down
24 changes: 21 additions & 3 deletions src/Composite/Union/Union.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,46 @@

final class Union
{
/**
* @var mixed
*/
private mixed $value;

/**
* @var array
*/
private array $allowedTypes;

/**
* @param array $allowedTypes
*/
public function __construct(array $allowedTypes)
{
$this->allowedTypes = $allowedTypes;
}

// Set a value of one of the allowed types
/**
* @param mixed $value
* @return void
*/
public function setValue(mixed $value): void
{
$this->validateType($value);
$this->value = $value;
}

// Get the value
/**
* @return mixed
*/
public function getValue(): mixed
{
return $this->value;
}

// Validate that the value matches one of the allowed types
/**
* @param mixed $value
* @return void
*/
private function validateType(mixed $value): void
{
$type = gettype($value);
Expand Down
51 changes: 40 additions & 11 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,62 +21,91 @@
use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt32;
use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8;

/**
* @param int $value
* @return Int8
*/
function int8(int $value): Int8
{
return new Int8($value);
}

/**
* @param int $value
* @return Int16
*/
function int16(int $value): Int16
{
return new Int16($value);
}

/**
* @param int $value
* @return Int32
*/
function int32(int $value): Int32
{
return new Int32($value);
}

/**
* @param int $value
* @return Int64
*/
function int64(int $value): Int64
{
return new Int64($value);
}

/**
* @param int $value
* @return Int128
*/
function int128(int $value): Int128
{
return new Int128($value);
}

/**
* @param int $value
* @return UInt8
*/
function uint8(int $value): UInt8
{
return new UInt8($value);
}

/**
* @param int $value
* @return UInt16
*/
function uint16(int $value): UInt16
{
return new UInt16($value);
}

/**
* @param int $value
* @return UInt32
*/
function uint32(int $value): UInt32
{
return new UInt32($value);
}

//
//function uint64(int $value): Int64
//{
// return new UInt64($value);
//}
//
//function uint128(int $value): Int128
//{
// return new UInt128($value);
//}

/**
* @param float $value
* @return Float32
*/
function float32(float $value): Float32
{
return new Float32($value);
}

/**
* @param float $value
* @return Float64
*/
function float64(float $value): Float64
{
return new Float64($value);
Expand Down
Loading