Skip to content

Commit 86a68e1

Browse files
committed
Introduce domain exceptions for decoding and encoding
1 parent 5615cc4 commit 86a68e1

File tree

8 files changed

+62
-24
lines changed

8 files changed

+62
-24
lines changed

src/Codec/CodecLibrary.php

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

55
use MongoDB\Exception\InvalidArgumentException;
6-
use MongoDB\Exception\UnexpectedValueException;
7-
8-
use function get_debug_type;
9-
use function sprintf;
6+
use MongoDB\Exception\UnsupportedValueException;
107

118
class CodecLibrary implements Codec
129
{
@@ -114,7 +111,7 @@ final public function decode($value)
114111
}
115112
}
116113

117-
throw new UnexpectedValueException(sprintf('No decoder found for value of type "%s"', get_debug_type($value)));
114+
throw UnsupportedValueException::invalidDecodableValue($value);
118115
}
119116

120117
/**
@@ -129,6 +126,6 @@ final public function encode($value)
129126
}
130127
}
131128

132-
throw new UnexpectedValueException(sprintf('No encoder found for value of type "%s"', get_debug_type($value)));
129+
throw UnsupportedValueException::invalidEncodableValue($value);
133130
}
134131
}

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\UnsupportedValueException;
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 UnsupportedValueException 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\UnsupportedValueException;
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 UnsupportedValueException if the decoder does not support the value
3030
*/
3131
public function decode($value);
3232

src/Codec/DocumentCodec.php

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

55
use MongoDB\BSON\Document;
6+
use MongoDB\Exception\UnsupportedValueException;
67

78
/**
89
* The DocumentCodec interface allows decoding BSON document data to native PHP
@@ -17,12 +18,14 @@ interface DocumentCodec extends Codec
1718
* @param mixed $value
1819
* @psalm-param Document $value
1920
* @psalm-return ObjectType
21+
* @throws UnsupportedValueException if the decoder does not support the value
2022
*/
2123
public function decode($value): object;
2224

2325
/**
2426
* @param mixed $value
2527
* @psalm-param ObjectType $value
28+
* @throws UnsupportedValueException if the encoder does not support the value
2629
*/
2730
public function encode($value): Document;
2831
}

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\UnsupportedValueException;
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 UnsupportedValueException 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\UnsupportedValueException;
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 UnsupportedValueException if the encoder does not support the value
3030
*/
3131
public function encode($value);
3232

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 UnsupportedValueException extends InvalidArgumentException implements Exception
11+
{
12+
/** @var mixed */
13+
private $value;
14+
15+
/** @return mixed */
16+
public function getValue()
17+
{
18+
return $this->value;
19+
}
20+
21+
/** @param mixed $value */
22+
public static function invalidDecodableValue($value): self
23+
{
24+
return new self(sprintf('Could not decode value of type "%s".', get_debug_type($value)), $value);
25+
}
26+
27+
/** @param mixed $value */
28+
public static function invalidEncodableValue($value): self
29+
{
30+
return new self(sprintf('Could not encode value of type "%s".', get_debug_type($value)), $value);
31+
}
32+
33+
/** @param mixed $value */
34+
private function __construct(string $message, $value)
35+
{
36+
parent::__construct($message);
37+
38+
$this->value = $value;
39+
}
40+
}

tests/Codec/CodecLibraryTest.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use MongoDB\Codec\DecodeIfSupported;
88
use MongoDB\Codec\EncodeIfSupported;
99
use MongoDB\Codec\KnowsCodecLibrary;
10-
use MongoDB\Exception\UnexpectedValueException;
10+
use MongoDB\Exception\UnsupportedValueException;
1111
use MongoDB\Tests\TestCase;
1212

1313
class CodecLibraryTest extends TestCase
@@ -36,17 +36,13 @@ public function testDecodeNull(): void
3636

3737
$this->assertFalse($codec->canDecode(null));
3838

39-
$this->expectException(UnexpectedValueException::class);
40-
$this->expectExceptionMessage('No decoder found for value of type "null"');
41-
42-
$this->assertNull($codec->decode(null));
39+
$this->expectExceptionObject(UnsupportedValueException::invalidDecodableValue(null));
40+
$codec->decode(null);
4341
}
4442

4543
public function testDecodeUnsupportedValue(): void
4644
{
47-
$this->expectException(UnexpectedValueException::class);
48-
$this->expectExceptionMessage('No decoder found for value of type "string"');
49-
45+
$this->expectExceptionObject(UnsupportedValueException::invalidDecodableValue('foo'));
5046
$this->getCodecLibrary()->decode('foo');
5147
}
5248

@@ -74,17 +70,13 @@ public function testEncodeNull(): void
7470

7571
$this->assertFalse($codec->canEncode(null));
7672

77-
$this->expectException(UnexpectedValueException::class);
78-
$this->expectExceptionMessage('No encoder found for value of type "null"');
79-
73+
$this->expectExceptionObject(UnsupportedValueException::invalidEncodableValue(null));
8074
$codec->encode(null);
8175
}
8276

8377
public function testEncodeUnsupportedValue(): void
8478
{
85-
$this->expectException(UnexpectedValueException::class);
86-
$this->expectExceptionMessage('No encoder found for value of type "string"');
87-
79+
$this->expectExceptionObject(UnsupportedValueException::invalidEncodableValue('foo'));
8880
$this->getCodecLibrary()->encode('foo');
8981
}
9082

0 commit comments

Comments
 (0)