Skip to content

Commit fa35f67

Browse files
author
nejc
committed
Add core interfaces and exceptions for type system
1 parent 00d3fcb commit fa35f67

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Nejcc\PhpDatatypes\Exceptions;
4+
5+
use InvalidArgumentException as BaseInvalidArgumentException;
6+
7+
class InvalidArgumentException extends BaseInvalidArgumentException
8+
{
9+
/**
10+
* Create a new invalid argument exception
11+
*
12+
* @param string $message The exception message
13+
* @param int $code The exception code
14+
* @param \Throwable|null $previous The previous exception
15+
*/
16+
public function __construct(string $message = "", int $code = 0, ?\Throwable $previous = null)
17+
{
18+
parent::__construct($message, $code, $previous);
19+
}
20+
21+
/**
22+
* Create an exception for invalid component count
23+
*
24+
* @param int $expected Expected number of components
25+
* @param int $actual Actual number of components
26+
* @return self
27+
*/
28+
public static function invalidComponentCount(int $expected, int $actual): self
29+
{
30+
return new self(sprintf(
31+
'Invalid number of components. Expected %d, got %d',
32+
$expected,
33+
$actual
34+
));
35+
}
36+
37+
/**
38+
* Create an exception for non-numeric components
39+
*
40+
* @param array $components The invalid components
41+
* @return self
42+
*/
43+
public static function nonNumericComponents(array $components): self
44+
{
45+
return new self(sprintf(
46+
'All components must be numeric. Got: %s',
47+
implode(', ', array_map('gettype', $components))
48+
));
49+
}
50+
51+
/**
52+
* Create an exception for different dimensions
53+
*
54+
* @param int $expected Expected dimension
55+
* @param int $actual Actual dimension
56+
* @return self
57+
*/
58+
public static function differentDimensions(int $expected, int $actual): self
59+
{
60+
return new self(sprintf(
61+
'Vectors must have the same dimension. Expected %d, got %d',
62+
$expected,
63+
$actual
64+
));
65+
}
66+
}

src/Interfaces/DataTypeInterface.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Nejcc\PhpDatatypes\Interfaces;
4+
5+
interface DataTypeInterface
6+
{
7+
/**
8+
* Get the value of the data type
9+
*
10+
* @return mixed
11+
*/
12+
public function getValue(): mixed;
13+
14+
/**
15+
* Set the value of the data type
16+
*
17+
* @param mixed $value
18+
* @return void
19+
*/
20+
public function setValue(mixed $value): void;
21+
22+
/**
23+
* Convert the data type to a string representation
24+
*
25+
* @return string
26+
*/
27+
public function __toString(): string;
28+
29+
/**
30+
* Check if this data type equals another data type
31+
*
32+
* @param DataTypeInterface $other
33+
* @return bool
34+
*/
35+
public function equals(DataTypeInterface $other): bool;
36+
}

0 commit comments

Comments
 (0)