Skip to content

Commit 10a4ddc

Browse files
committed
Introduce domain exceptions for decoding and encoding
1 parent 5615cc4 commit 10a4ddc

File tree

7 files changed

+70
-4
lines changed

7 files changed

+70
-4
lines changed

src/Codec/DecodeIfSupported.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MongoDB\Codec;
44

5+
use MongoDB\Exception\UndecodableValueException;
6+
57
/**
68
* @psalm-template BSONType
79
* @psalm-template NativeType
@@ -19,6 +21,7 @@ abstract public function canDecode($value): bool;
1921
* @psalm-param BSONType $value
2022
* @return mixed
2123
* @psalm-return NativeType
24+
* @throws UndecodableValueException if the decoder does not support the value
2225
*/
2326
abstract public function decode($value);
2427

src/Codec/Decoder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MongoDB\Codec;
44

5-
use MongoDB\Exception\InvalidArgumentException;
5+
use MongoDB\Exception\UndecodableValueException;
66

77
/**
88
* @psalm-template BSONType
@@ -26,7 +26,7 @@ public function canDecode($value): bool;
2626
* @psalm-param BSONType $value
2727
* @return mixed
2828
* @psalm-return NativeType
29-
* @throws InvalidArgumentException if the decoder does not support the value
29+
* @throws UndecodableValueException if the decoder does not support the value
3030
*/
3131
public function decode($value);
3232

src/Codec/DocumentCodec.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace MongoDB\Codec;
44

55
use MongoDB\BSON\Document;
6+
use MongoDB\Exception\UndecodableValueException;
7+
use MongoDB\Exception\UnencodableValueException;
68

79
/**
810
* The DocumentCodec interface allows decoding BSON document data to native PHP
@@ -17,12 +19,14 @@ interface DocumentCodec extends Codec
1719
* @param mixed $value
1820
* @psalm-param Document $value
1921
* @psalm-return ObjectType
22+
* @throws UndecodableValueException if the decoder does not support the value
2023
*/
2124
public function decode($value): object;
2225

2326
/**
2427
* @param mixed $value
2528
* @psalm-param ObjectType $value
29+
* @throws UnencodableValueException if the encoder does not support the value
2630
*/
2731
public function encode($value): Document;
2832
}

src/Codec/EncodeIfSupported.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MongoDB\Codec;
44

5+
use MongoDB\Exception\UnencodableValueException;
6+
57
/**
68
* @psalm-template BSONType
79
* @psalm-template NativeType
@@ -19,6 +21,7 @@ abstract public function canEncode($value): bool;
1921
* @psalm-param NativeType $value
2022
* @return mixed
2123
* @psalm-return BSONType
24+
* @throws UnencodableValueException if the encoder does not support the value
2225
*/
2326
abstract public function encode($value);
2427

src/Codec/Encoder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MongoDB\Codec;
44

5-
use MongoDB\Exception\InvalidArgumentException;
5+
use MongoDB\Exception\UnencodableValueException;
66

77
/**
88
* @psalm-template BSONType
@@ -26,7 +26,7 @@ public function canEncode($value): bool;
2626
* @psalm-param NativeType $value
2727
* @return mixed
2828
* @psalm-return BSONType
29-
* @throws InvalidArgumentException if the decoder does not support the value
29+
* @throws UnencodableValueException if the encoder does not support the value
3030
*/
3131
public function encode($value);
3232

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace MongoDB\Exception;
4+
5+
use InvalidArgumentException;
6+
7+
use function get_debug_type;
8+
use function sprintf;
9+
10+
class UndecodableValueException extends InvalidArgumentException implements Exception
11+
{
12+
/** @var mixed */
13+
private $value;
14+
15+
/** @param mixed $value */
16+
public function __construct($value)
17+
{
18+
parent::__construct(sprintf('Could not decode value of type "%s".', get_debug_type($value)));
19+
20+
$this->value = $value;
21+
}
22+
23+
/** @return mixed */
24+
public function getValue()
25+
{
26+
return $this->value;
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace MongoDB\Exception;
4+
5+
use InvalidArgumentException;
6+
7+
use function get_debug_type;
8+
use function sprintf;
9+
10+
class UnencodableValueException extends InvalidArgumentException implements Exception
11+
{
12+
/** @var mixed */
13+
private $value;
14+
15+
/** @param mixed $value */
16+
public function __construct($value)
17+
{
18+
parent::__construct(sprintf('Could not encode value of type "%s".', get_debug_type($value)));
19+
20+
$this->value = $value;
21+
}
22+
23+
/** @return mixed */
24+
public function getValue()
25+
{
26+
return $this->value;
27+
}
28+
}

0 commit comments

Comments
 (0)