1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Explicit Architecture POC, |
7
|
|
|
* which is created on top of the Symfony Demo application. |
8
|
|
|
* |
9
|
|
|
* (c) Herberto Graça <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Acme\App\Infrastructure\Persistence\Doctrine\Type; |
16
|
|
|
|
17
|
|
|
use Acme\PhpExtension\Identity\AbstractUuidId; |
18
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
19
|
|
|
use Ramsey\Uuid\Doctrine\UuidBinaryType; |
20
|
|
|
use Ramsey\Uuid\Uuid as RamseyUuid; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This type mapper stores the UUIDs in binary format, in the DB. |
24
|
|
|
* This makes it faster to query on the IDs and they take less space (they only take 16 characters as opposed to the |
25
|
|
|
* UUID 36 characters string). |
26
|
|
|
* However, if you look at the UUID stored in the DB, you won't be able to see the actual UUID, only its byte string. |
27
|
|
|
*/ |
28
|
|
|
abstract class AbstractBinaryUuidType extends UuidBinaryType |
29
|
|
|
{ |
30
|
|
|
use TypeTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @throws \Doctrine\DBAL\Types\ConversionException |
34
|
|
|
* |
35
|
|
|
* @return AbstractUuidId|null |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function convertToPHPValue($value, AbstractPlatform $platform) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
/** @var RamseyUuid $ramseyUuid */ |
40
|
|
|
$ramseyUuid = parent::convertToPHPValue($value, $platform); |
41
|
|
|
|
42
|
|
|
if ($ramseyUuid === null) { |
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->createSpecificObject((string) $ramseyUuid); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param AbstractUuidId $value |
51
|
|
|
* |
52
|
|
|
* @throws \Doctrine\DBAL\Types\ConversionException |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
57
|
|
|
{ |
58
|
|
|
$uuidString = (string) $value; |
59
|
|
|
|
60
|
|
|
return parent::convertToDatabaseValue($uuidString, $platform); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.