|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\ODM\MongoDB\Types; |
| 6 | + |
| 7 | +use InvalidArgumentException; |
| 8 | +use MongoDB\BSON\Binary; |
| 9 | +use MongoDB\BSON\VectorType; |
| 10 | + |
| 11 | +use function enum_exists; |
| 12 | +use function get_debug_type; |
| 13 | +use function is_array; |
| 14 | +use function sprintf; |
| 15 | +use function str_replace; |
| 16 | + |
| 17 | +/** @internal */ |
| 18 | +abstract class AbstractVectorType extends Type |
| 19 | +{ |
| 20 | + public function convertToDatabaseValue(mixed $value): ?Binary |
| 21 | + { |
| 22 | + if (! enum_exists(VectorType::class)) { |
| 23 | + throw new InvalidArgumentException('MongoDB\BSON\VectorType enum does not exist. Install the MongoDB Extension version 2.2.0 or higher in order to use a vector field type.'); |
| 24 | + } |
| 25 | + |
| 26 | + if ($value === null) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + if (is_array($value)) { |
| 31 | + return Binary::fromVector($value, $this->getVectorType()); |
| 32 | + } |
| 33 | + |
| 34 | + if (! $value instanceof Binary) { |
| 35 | + throw new InvalidArgumentException(sprintf('Invalid data type %s received for vector field, expected null, array or MongoDB\BSON\Binary', get_debug_type($value))); |
| 36 | + } |
| 37 | + |
| 38 | + if ($value->getType() !== Binary::TYPE_VECTOR) { |
| 39 | + throw new InvalidArgumentException(sprintf('Invalid binary data of type %d received for vector field, expected binary type %d', $value->getType(), Binary::TYPE_VECTOR)); |
| 40 | + } |
| 41 | + |
| 42 | + if ($value->getVectorType() !== $this->getVectorType()) { |
| 43 | + throw new InvalidArgumentException(sprintf('Invalid binary vector data of vector type %s received for vector field, expected vector type %s', $value->getVectorType()->name, $this->getVectorType()->name)); |
| 44 | + } |
| 45 | + |
| 46 | + return $value; |
| 47 | + } |
| 48 | + |
| 49 | + /** @return list<float>|list<int>|list<bool>|null */ |
| 50 | + public function convertToPHPValue(mixed $value): ?array |
| 51 | + { |
| 52 | + if ($value === null) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + if (is_array($value)) { |
| 57 | + return $value; |
| 58 | + } |
| 59 | + |
| 60 | + if (! $value instanceof Binary) { |
| 61 | + throw new InvalidArgumentException(sprintf('Invalid data of type "%s" received for vector field', get_debug_type($value))); |
| 62 | + } |
| 63 | + |
| 64 | + if ($value->getType() !== Binary::TYPE_VECTOR) { |
| 65 | + throw new InvalidArgumentException(sprintf('Invalid binary data of type %d received for vector field', $value->getType())); |
| 66 | + } |
| 67 | + |
| 68 | + if ($value->getVectorType() !== $this->getVectorType()) { |
| 69 | + throw new InvalidArgumentException(sprintf('Invalid binary vector data of vector type %s received for vector field, expected vector type %s', $value->getVectorType()->name, $this->getVectorType()->name)); |
| 70 | + } |
| 71 | + |
| 72 | + return $value->toArray(); |
| 73 | + } |
| 74 | + |
| 75 | + public function closureToMongo(): string |
| 76 | + { |
| 77 | + return str_replace('%%vectorType%%', $this->getVectorType()->name, <<<'PHP' |
| 78 | + if ($value === null) { |
| 79 | + $return = null; |
| 80 | + return; |
| 81 | + } |
| 82 | +
|
| 83 | + if (\is_array($value)) { |
| 84 | + $return = \MongoDB\BSON\Binary::fromVector($value, \MongoDB\BSON\VectorType::%%vectorType%%); |
| 85 | + return; |
| 86 | + } |
| 87 | +
|
| 88 | + if (! $value instanceof \MongoDB\BSON\Binary) { |
| 89 | + throw new InvalidArgumentException(sprintf('Invalid data type %s received for vector field, expected null, array or MongoDB\BSON\Binary', get_debug_type($value))); |
| 90 | + } |
| 91 | +
|
| 92 | + if ($value->getType() !== \MongoDB\BSON\Binary::TYPE_VECTOR) { |
| 93 | + throw new InvalidArgumentException(sprintf('Invalid binary data of type %d received for vector field, expected binary type %d', $value->getType(), \MongoDB\BSON\Binary::TYPE_VECTOR)); |
| 94 | + } |
| 95 | +
|
| 96 | + if ($value->getVectorType() !== \MongoDB\BSON\VectorType::%%vectorType%%) { |
| 97 | + throw new \InvalidArgumentException(sprintf('Invalid binary vector data of vector type %s received for vector field, expected vector type %%vectorType%%', $value->getVectorType()->name)); |
| 98 | + } |
| 99 | +
|
| 100 | + $return = $value; |
| 101 | +PHP); |
| 102 | + } |
| 103 | + |
| 104 | + public function closureToPHP(): string |
| 105 | + { |
| 106 | + return str_replace('%%vectorType%%', $this->getVectorType()->name, <<<'PHP' |
| 107 | + if ($value === null) { |
| 108 | + $return = null; |
| 109 | + return; |
| 110 | + } |
| 111 | +
|
| 112 | + if (\is_array($value)) { |
| 113 | + $return = $value; |
| 114 | + return; |
| 115 | + } |
| 116 | +
|
| 117 | + if (! $value instanceof \MongoDB\BSON\Binary) { |
| 118 | + throw new \InvalidArgumentException(sprintf('Invalid data of type "%s" received for vector field', get_debug_type($value))); |
| 119 | + } |
| 120 | +
|
| 121 | + if ($value->getType() !== \MongoDB\BSON\Binary::TYPE_VECTOR) { |
| 122 | + throw new \InvalidArgumentException(sprintf('Invalid binary data of type %d received for vector field', $value->getType())); |
| 123 | + } |
| 124 | +
|
| 125 | + if ($value->getVectorType() !== \MongoDB\BSON\VectorType::%%vectorType%%) { |
| 126 | + throw new \InvalidArgumentException(sprintf('Invalid binary vector data of vector type %s received for vector field, expected vector type %%vectorType%%', $value->getVectorType()->name)); |
| 127 | + } |
| 128 | +
|
| 129 | + $return = $value->toArray(); |
| 130 | +PHP); |
| 131 | + } |
| 132 | + |
| 133 | + abstract protected function getVectorType(): VectorType; |
| 134 | +} |
0 commit comments