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

⬆️ allow ORM 3 #14

Merged
merged 1 commit into from
May 4, 2024
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
9 changes: 4 additions & 5 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
name: PHP-CS-Fixer
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Fix CS
uses: docker://oskarstark/php-cs-fixer-ga
tests:
Expand All @@ -19,19 +19,18 @@ jobs:
matrix:
include:
- description: 'lowest'
php: '8.0'
- description: '8.1'
php: '8.1'
composer_option: '--prefer-lowest'
- description: '8.2'
php: '8.2'
- description: 'latest'
php: '8.3'
name: PHP ${{ matrix.php }} tests
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.composer/cache/files
key: ${{ matrix.php }}-${{ matrix.composer_option }}
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
}
],
"require": {
"php": "^8.0",
"doctrine/orm": "^2.7",
"php": "^8.1",
"doctrine/orm": "^2.7 || ^3.0",
"pugx/shortid-php": "^1.0"
},
"require-dev": {
"dg/bypass-finals": "^1.5",
"dg/bypass-finals": "^1.6",
"phpunit/phpunit": "^9.6"
},
"config": {
Expand Down
8 changes: 8 additions & 0 deletions src/Generator/ShortidGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
namespace PUGX\Shortid\Doctrine\Generator;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Id\AbstractIdGenerator;
use PUGX\Shortid\Shortid;

final class ShortidGenerator extends AbstractIdGenerator
{
// ORM 2
public function generate(EntityManager $manager, $entity): Shortid
{
return Shortid::generate();
}

// ORM 3
public function generateId(EntityManagerInterface $em, ?object $entity): Shortid
{
return Shortid::generate();
}
}
29 changes: 23 additions & 6 deletions src/ShortidType.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,52 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform): st

$field = ['length' => $length, 'fixed' => true];
if (!$platform instanceof MySqlPlatform) {
return $platform->getVarcharTypeDeclarationSQL($field);
if (\is_callable([$platform, 'getVarcharTypeDeclarationSQL'])) {
return $platform->getVarcharTypeDeclarationSQL($field);
}

return $platform->getStringTypeDeclarationSQL($field);
}
$field['collation'] = 'utf8_bin';
$collation = $platform->getColumnCollationDeclarationSQL('utf8_bin');

if (\is_callable([$platform, 'getVarcharTypeDeclarationSQL'])) {
return $platform->getVarcharTypeDeclarationSQL($field).' '.$collation;
}

return $platform->getVarcharTypeDeclarationSQL($field).' '.$platform->getColumnCollationDeclarationSQL('utf8_bin');
return $platform->getStringTypeDeclarationSQL($field).' '.$collation;
}

public function convertToPHPValue($value, AbstractPlatform $platform): ?Shortid
{
if (empty($value)) {
return null;
}
if (ShortId::isValid($value)) {
if (Shortid::isValid($value)) {
return new Shortid($value);
}

throw ConversionException::conversionFailed($value, self::NAME);
if (\is_callable([ConversionException::class, 'conversionFailed'])) {
throw ConversionException::conversionFailed($value, self::NAME);
}

throw new ConversionException(\sprintf('Invalid value %s for type %s', $value, self::NAME));
}

public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if (empty($value)) {
return null;
}
if ($value instanceof ShortId || ShortId::isValid($value)) {
if ($value instanceof Shortid || Shortid::isValid($value)) {
return (string) $value;
}

throw ConversionException::conversionFailed($value, self::NAME);
if (\is_callable([ConversionException::class, 'conversionFailed'])) {
throw ConversionException::conversionFailed($value, self::NAME);
}

throw new ConversionException(\sprintf('Invalid value %s for type %s', $value, self::NAME));
}

public function getName(): string
Expand Down
6 changes: 5 additions & 1 deletion tests/ShortidTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ protected function setUp(): void

public function testGetSQLDeclaration(): void
{
$this->platform->expects(self::once())->method('getVarcharTypeDeclarationSQL')->willReturn('CHAR');
if (\is_callable([$this->platform, 'getVarcharTypeDeclarationSQL'])) {
$this->platform->expects(self::once())->method('getVarcharTypeDeclarationSQL')->willReturn('CHAR');
} else {
$this->platform->expects(self::once())->method('getStringTypeDeclarationSQL')->willReturn('CHAR');
}
$declaration = $this->type->getSQLDeclaration([], $this->platform);
self::assertNotEmpty($declaration);
}
Expand Down