-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathVectorType.php
More file actions
64 lines (52 loc) · 1.49 KB
/
Copy pathVectorType.php
File metadata and controls
64 lines (52 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use function array_values;
use function is_array;
use function pack;
use function unpack;
final class VectorType extends Type
{
/** @inheritdoc */
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getVectorTypeDeclarationSQL($column);
}
public function getBindingType(): ParameterType
{
return ParameterType::BINARY;
}
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): string|null
{
if ($value === null) {
return null;
}
if (! is_array($value)) {
throw InvalidType::new(
$value,
static::class,
['null', 'array'],
);
}
return pack('f*', ...$value);
}
/** @return list<float>|null */
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): array|null
{
if ($value === null) {
return null;
}
$unpacked = unpack('f*', $value);
if ($unpacked === false) {
throw ValueNotConvertible::new(
$value,
static::class,
);
}
return array_values($unpacked);
}
}