Skip to content

Commit

Permalink
Introduce domain exceptions for decoding and encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Jul 12, 2023
1 parent 5615cc4 commit 86a68e1
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 24 deletions.
9 changes: 3 additions & 6 deletions src/Codec/CodecLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
namespace MongoDB\Codec;

use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnexpectedValueException;

use function get_debug_type;
use function sprintf;
use MongoDB\Exception\UnsupportedValueException;

class CodecLibrary implements Codec
{
Expand Down Expand Up @@ -114,7 +111,7 @@ final public function decode($value)
}
}

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

/**
Expand All @@ -129,6 +126,6 @@ final public function encode($value)
}
}

throw new UnexpectedValueException(sprintf('No encoder found for value of type "%s"', get_debug_type($value)));
throw UnsupportedValueException::invalidEncodableValue($value);
}
}
3 changes: 3 additions & 0 deletions src/Codec/DecodeIfSupported.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MongoDB\Codec;

use MongoDB\Exception\UnsupportedValueException;

/**
* @psalm-template BSONType
* @psalm-template NativeType
Expand All @@ -19,6 +21,7 @@ abstract public function canDecode($value): bool;
* @psalm-param BSONType $value
* @return mixed
* @psalm-return NativeType
* @throws UnsupportedValueException if the decoder does not support the value
*/
abstract public function decode($value);

Expand Down
4 changes: 2 additions & 2 deletions src/Codec/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MongoDB\Codec;

use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedValueException;

/**
* @psalm-template BSONType
Expand All @@ -26,7 +26,7 @@ public function canDecode($value): bool;
* @psalm-param BSONType $value
* @return mixed
* @psalm-return NativeType
* @throws InvalidArgumentException if the decoder does not support the value
* @throws UnsupportedValueException if the decoder does not support the value
*/
public function decode($value);

Expand Down
3 changes: 3 additions & 0 deletions src/Codec/DocumentCodec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MongoDB\Codec;

use MongoDB\BSON\Document;
use MongoDB\Exception\UnsupportedValueException;

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

/**
* @param mixed $value
* @psalm-param ObjectType $value
* @throws UnsupportedValueException if the encoder does not support the value
*/
public function encode($value): Document;
}
3 changes: 3 additions & 0 deletions src/Codec/EncodeIfSupported.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MongoDB\Codec;

use MongoDB\Exception\UnsupportedValueException;

/**
* @psalm-template BSONType
* @psalm-template NativeType
Expand All @@ -19,6 +21,7 @@ abstract public function canEncode($value): bool;
* @psalm-param NativeType $value
* @return mixed
* @psalm-return BSONType
* @throws UnsupportedValueException if the encoder does not support the value
*/
abstract public function encode($value);

Expand Down
4 changes: 2 additions & 2 deletions src/Codec/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MongoDB\Codec;

use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedValueException;

/**
* @psalm-template BSONType
Expand All @@ -26,7 +26,7 @@ public function canEncode($value): bool;
* @psalm-param NativeType $value
* @return mixed
* @psalm-return BSONType
* @throws InvalidArgumentException if the decoder does not support the value
* @throws UnsupportedValueException if the encoder does not support the value
*/
public function encode($value);

Expand Down
40 changes: 40 additions & 0 deletions src/Exception/UnsupportedValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace MongoDB\Exception;

use InvalidArgumentException;

use function get_debug_type;
use function sprintf;

class UnsupportedValueException extends InvalidArgumentException implements Exception
{
/** @var mixed */
private $value;

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

/** @param mixed $value */
public static function invalidDecodableValue($value): self
{
return new self(sprintf('Could not decode value of type "%s".', get_debug_type($value)), $value);
}

/** @param mixed $value */
public static function invalidEncodableValue($value): self
{
return new self(sprintf('Could not encode value of type "%s".', get_debug_type($value)), $value);
}

/** @param mixed $value */
private function __construct(string $message, $value)
{
parent::__construct($message);

$this->value = $value;
}
}
20 changes: 6 additions & 14 deletions tests/Codec/CodecLibraryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use MongoDB\Codec\DecodeIfSupported;
use MongoDB\Codec\EncodeIfSupported;
use MongoDB\Codec\KnowsCodecLibrary;
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedValueException;
use MongoDB\Tests\TestCase;

class CodecLibraryTest extends TestCase
Expand Down Expand Up @@ -36,17 +36,13 @@ public function testDecodeNull(): void

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

$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('No decoder found for value of type "null"');

$this->assertNull($codec->decode(null));
$this->expectExceptionObject(UnsupportedValueException::invalidDecodableValue(null));
$codec->decode(null);
}

public function testDecodeUnsupportedValue(): void
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('No decoder found for value of type "string"');

$this->expectExceptionObject(UnsupportedValueException::invalidDecodableValue('foo'));
$this->getCodecLibrary()->decode('foo');
}

Expand Down Expand Up @@ -74,17 +70,13 @@ public function testEncodeNull(): void

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

$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('No encoder found for value of type "null"');

$this->expectExceptionObject(UnsupportedValueException::invalidEncodableValue(null));
$codec->encode(null);
}

public function testEncodeUnsupportedValue(): void
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('No encoder found for value of type "string"');

$this->expectExceptionObject(UnsupportedValueException::invalidEncodableValue('foo'));
$this->getCodecLibrary()->encode('foo');
}

Expand Down

0 comments on commit 86a68e1

Please sign in to comment.