Skip to content

Commit fb680b5

Browse files
Implement Custom Exception classes
1 parent 89794fd commit fb680b5

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

src/Exception/DivisionByZero.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace NAL\TimeTracker\Exception;
4+
5+
use InvalidArgumentException;
6+
7+
class DivisionByZero extends InvalidArgumentException
8+
{
9+
public function __construct()
10+
{
11+
parent::__construct("Division by zero in unit conversion.");
12+
}
13+
}

src/Exception/InvalidUnitName.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace NAL\TimeTracker\Exception;
4+
5+
use InvalidArgumentException;
6+
7+
class InvalidUnitName extends InvalidArgumentException
8+
{
9+
public function __construct(string $unit)
10+
{
11+
parent::__construct(
12+
empty($unit)
13+
? "The unit name cannot be empty."
14+
: "The unit name '{$unit}' is invalid or already exists."
15+
);
16+
}
17+
}

src/Exception/TimerNotStarted.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace NAL\TimeTracker\Exception;
4+
5+
use BadMethodCallException;
6+
7+
class TimerNotStarted extends BadMethodCallException
8+
{
9+
public function __construct(string $id)
10+
{
11+
parent::__construct("The timer with ID '{$id}' has not been started.");
12+
}
13+
}

src/Exception/UnknownUnit.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace NAL\TimeTracker\Exception;
4+
5+
use InvalidArgumentException;
6+
7+
class UnknownUnit extends InvalidArgumentException
8+
{
9+
public function __construct($unit, array $supported)
10+
{
11+
parent::__construct('Unsupported unit: ' . $unit . '. Use' . implode(', ', array_map(fn ($v) => "'$v'", $supported)) . 'instead.');
12+
}
13+
}

src/Exception/UnsupportedLogic.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace NAL\TimeTracker\Exception;
4+
5+
use LogicException;
6+
7+
class UnsupportedLogic extends LogicException
8+
{
9+
public function __construct(string $message)
10+
{
11+
parent::__construct($message);
12+
}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace NAL\TimeTracker\Exception;
4+
5+
class UnsupportedOperator extends UnsupportedLogic
6+
{
7+
public function __construct(string $operator)
8+
{
9+
parent::__construct("The operator '{$operator}' is unsupported. Supported operators are '+', '-', '*', '/'.");
10+
}
11+
}

0 commit comments

Comments
 (0)