Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace MartinGeorgiev\Doctrine\DBAL\Types\Exceptions;

use Doctrine\DBAL\Types\ConversionException;

class InvalidJsonbArrayItemForPHPException extends ConversionException
{
private static function create(string $message, mixed $value): self
{
return new self(\sprintf($message, \var_export($value, true)));
}

public static function forInvalidType(mixed $value): self
{
return self::create('Array values must be valid JSON objects, %s given', $value);
}

public static function forInvalidFormat(mixed $value): self
{
return self::create('Invalid JSONB format in array: %s', $value);
}
}

This file was deleted.

This file was deleted.

15 changes: 5 additions & 10 deletions src/MartinGeorgiev/Doctrine/DBAL/Types/JsonbArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MartinGeorgiev\Doctrine\DBAL\Types;

use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\UnexpectedTypeOfTransformedPHPValue;
use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidJsonbArrayItemForPHPException;

/**
* Implementation of PostgreSQL JSONB[] data type.
Expand All @@ -18,9 +18,6 @@ class JsonbArray extends BaseArray
{
use JsonTransformer;

/**
* @var string
*/
protected const TYPE_NAME = 'jsonb[]';

protected function transformArrayItemForPostgres(mixed $item): string
Expand Down Expand Up @@ -48,17 +45,15 @@ protected function transformPostgresArrayToPHPArray(string $postgresArray): arra
*/
public function transformArrayItemForPHP($item): array
{
$transformedValue = null;

try {
$transformedValue = $this->transformFromPostgresJson($item);
if (!\is_array($transformedValue)) {
throw new UnexpectedTypeOfTransformedPHPValue($item, \gettype($transformedValue));
throw InvalidJsonbArrayItemForPHPException::forInvalidType($item);
}

return $transformedValue;
} catch (\JsonException) {
throw new UnexpectedTypeOfTransformedPHPValue($item, \gettype($transformedValue));
throw InvalidJsonbArrayItemForPHPException::forInvalidFormat($item);
}

return $transformedValue;
}
}
22 changes: 17 additions & 5 deletions tests/MartinGeorgiev/Doctrine/DBAL/Types/JsonbArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Tests\MartinGeorgiev\Doctrine\DBAL\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\UnexpectedTypeOfTransformedPHPValue;
use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidJsonbArrayItemForPHPException;
use MartinGeorgiev\Doctrine\DBAL\Types\JsonbArray;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -92,13 +92,25 @@ public static function provideValidTransformations(): array

/**
* @test
*
* @dataProvider provideInvalidTransformations
*/
public function throws_an_error_when_transformed_value_is_not_an_array(): void
public function throws_exception_when_invalid_data_provided_to_convert_to_php_value(string $postgresValue): void
{
$this->expectException(UnexpectedTypeOfTransformedPHPValue::class);

$postgresValue = '"a string encoded as json"';
$this->expectException(InvalidJsonbArrayItemForPHPException::class);
$this->expectExceptionMessage('Invalid JSONB format in array');

$this->fixture->convertToPHPValue($postgresValue, $this->platform);
}

/**
* @return array<string, array{string}>
*/
public static function provideInvalidTransformations(): array
{
return [
'non-array json' => ['"a string encoded as json"'],
'invalid json format' => ['{invalid json}'],
];
}
}