Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to doctrine/orm v3 & doctrine/dbal v4 #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"license": "MIT",
"require": {
"brick/date-time": "~0.6.0",
"doctrine/dbal": "^2.7.0 || ^3.0",
"doctrine/orm": "^2.7.0",
"doctrine/dbal": "^4.0",
"doctrine/orm": "^3.0",
"php": "^8.1"
},
"require-dev": {
Expand All @@ -20,8 +20,8 @@
"phpunit/phpunit": "^10.5",
"php-coveralls/php-coveralls": "^2.4",
"vimeo/psalm": "5.21.1",
"doctrine/annotations": "^1.0",
"guzzlehttp/guzzle": "^7.0"
"guzzlehttp/guzzle": "^7.0",
"symfony/cache": "^5.0 || ^6.0 || ^7.0"
},
"autoload": {
"psr-4": {
Expand Down
13 changes: 0 additions & 13 deletions psalm-baseline.xml

This file was deleted.

1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
findUnusedBaselineEntry="true"
findUnusedPsalmSuppress="true"
findUnusedCode="false"
Expand Down
43 changes: 18 additions & 25 deletions src/Types/DayOfWeekType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use Brick\DateTime\DayOfWeek;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use ValueError;

/**
* Doctrine type for DayOfWeek.
Expand All @@ -17,17 +19,12 @@
*/
final class DayOfWeekType extends Type
{
public function getName(): string
{
return 'DayOfWeek';
}

public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getSmallIntTypeDeclarationSQL($column);
}

public function convertToDatabaseValue($value, AbstractPlatform $platform): ?int
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?int
{
if ($value === null) {
return null;
Expand All @@ -37,37 +34,33 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?int
return $value->value;
}

throw ConversionException::conversionFailedInvalidType(
throw InvalidType::new(
$value,
$this->getName(),
[DayOfWeek::class, 'null']
static::class,
[DayOfWeek::class, 'null'],
);
}

public function convertToPHPValue($value, AbstractPlatform $platform): ?DayOfWeek
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?DayOfWeek
{
if ($value === null) {
return null;
}

if (is_int($value)) {
return DayOfWeek::from($value);
try {
return DayOfWeek::from((int) $value);
} catch (ValueError $e) {
throw ValueNotConvertible::new(
$value,
DayOfWeek::class,
$e->getMessage(),
$e,
);
}

throw ConversionException::conversionFailedInvalidType(
$value,
$this->getName(),
[DayOfWeek::class, 'null']
);
}

public function getBindingType(): int
public function getBindingType(): ParameterType
{
return ParameterType::INTEGER;
}

public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}
}
37 changes: 19 additions & 18 deletions src/Types/DurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Brick\DateTime\Doctrine\Types;

use Brick\DateTime\DateTimeException;
use Brick\DateTime\Duration;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\Type;

/**
Expand All @@ -16,21 +18,16 @@
*/
final class DurationType extends Type
{
public function getName(): string
{
return 'Duration';
}

public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
if (!isset($column['length'])) {
$column['length'] = 64;
}

return $platform->getVarcharTypeDeclarationSQL($column);
return $platform->getStringTypeDeclarationSQL($column);
}

public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string
{
if ($value === null) {
return null;
Expand All @@ -40,24 +37,28 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
return (string) $value;
}

throw ConversionException::conversionFailedInvalidType(
throw InvalidType::new(
$value,
$this->getName(),
[Duration::class, 'null']
static::class,
[Duration::class, 'null'],
);
}

public function convertToPHPValue($value, AbstractPlatform $platform): ?Duration
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Duration
{
if ($value === null) {
return null;
}

return Duration::parse((string) $value);
}

public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
try {
return Duration::parse((string) $value);
} catch (DateTimeException $e) {
throw ValueNotConvertible::new(
$value,
Duration::class,
$e->getMessage(),
$e,
);
}
}
}
24 changes: 7 additions & 17 deletions src/Types/InstantType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Brick\DateTime\Instant;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;

Expand All @@ -17,17 +17,12 @@
*/
final class InstantType extends Type
{
public function getName(): string
{
return 'Instant';
}

public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getIntegerTypeDeclarationSQL($column);
}

public function convertToDatabaseValue($value, AbstractPlatform $platform): ?int
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?int
{
if ($value === null) {
return null;
Expand All @@ -37,14 +32,14 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?int
return $value->getEpochSecond();
}

throw ConversionException::conversionFailedInvalidType(
throw InvalidType::new(
$value,
$this->getName(),
[Instant::class, 'null']
static::class,
[Instant::class, 'null'],
);
}

public function convertToPHPValue($value, AbstractPlatform $platform): ?Instant
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?Instant
{
if ($value === null) {
return null;
Expand All @@ -53,13 +48,8 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?Instant
return Instant::of((int) $value);
}

public function getBindingType(): int
public function getBindingType(): ParameterType
{
return ParameterType::INTEGER;
}

public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}
}
35 changes: 18 additions & 17 deletions src/Types/LocalDateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Brick\DateTime\Doctrine\Types;

use Brick\DateTime\DateTimeException;
use Brick\DateTime\LocalDateTime;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;

Expand All @@ -16,17 +18,12 @@
*/
final class LocalDateTimeType extends Type
{
public function getName(): string
{
return 'LocalDateTime';
}

public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getDateTimeTypeDeclarationSQL($column);
}

public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): ?string
{
if ($value === null) {
return null;
Expand All @@ -42,26 +39,30 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
return $stringValue;
}

throw ConversionException::conversionFailedInvalidType(
throw InvalidType::new(
$value,
$this->getName(),
[LocalDateTime::class, 'null']
static::class,
[LocalDateTime::class, 'null'],
);
}

public function convertToPHPValue($value, AbstractPlatform $platform): ?LocalDateTime
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?LocalDateTime
{
if ($value === null) {
return null;
}

$value = str_replace(' ', 'T', (string) $value);

return LocalDateTime::parse($value);
}

public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
try {
return LocalDateTime::parse($value);
} catch (DateTimeException $e) {
throw ValueNotConvertible::new(
$value,
LocalDateTime::class,
$e->getMessage(),
$e,
);
}
}
}
Loading
Loading