Skip to content

Commit 08150e7

Browse files
authored
fix: Introduce NullVector and VectorInterface (#114)
* Introduce `NullVector` and `VectorInterface` * - * - * - * -
1 parent f91edc5 commit 08150e7

File tree

7 files changed

+89
-3
lines changed

7 files changed

+89
-3
lines changed

src/Document/NullVector.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\LlmChain\Document;
6+
7+
use PhpLlm\LlmChain\Exception\RuntimeException;
8+
9+
final class NullVector implements VectorInterface
10+
{
11+
public function getData(): array
12+
{
13+
throw new RuntimeException('getData() method cannot be called on a NullVector.');
14+
}
15+
16+
public function getDimensions(): int
17+
{
18+
throw new RuntimeException('getDimensions() method cannot be called on a NullVector.');
19+
}
20+
}

src/Document/Vector.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use PhpLlm\LlmChain\Exception\InvalidArgumentException;
88

9-
final class Vector
9+
final class Vector implements VectorInterface
1010
{
1111
/**
1212
* @param list<float> $data

src/Document/VectorDocument.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
{
1111
public function __construct(
1212
public Uuid $id,
13-
public Vector $vector,
13+
public VectorInterface $vector,
1414
public Metadata $metadata = new Metadata(),
1515
) {
1616
}

src/Document/VectorInterface.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\LlmChain\Document;
6+
7+
interface VectorInterface
8+
{
9+
/**
10+
* @return list<float>
11+
*/
12+
public function getData(): array;
13+
14+
public function getDimensions(): int;
15+
}

src/Store/Azure/SearchStore.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpLlm\LlmChain\Store\Azure;
66

77
use PhpLlm\LlmChain\Document\Metadata;
8+
use PhpLlm\LlmChain\Document\NullVector;
89
use PhpLlm\LlmChain\Document\Vector;
910
use PhpLlm\LlmChain\Document\VectorDocument;
1011
use PhpLlm\LlmChain\Store\VectorStoreInterface;
@@ -80,7 +81,9 @@ private function convertToVectorDocument(array $data): VectorDocument
8081
{
8182
return new VectorDocument(
8283
id: Uuid::fromString($data['id']),
83-
vector: $data[$this->vectorFieldName] ? new Vector($data[$this->vectorFieldName]) : null,
84+
vector: !array_key_exists($this->vectorFieldName, $data) || null === $data[$this->vectorFieldName]
85+
? new NullVector()
86+
: new Vector($data[$this->vectorFieldName]),
8487
metadata: new Metadata($data),
8588
);
8689
}

tests/Document/NullVectorTest.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\LlmChain\Tests\Document;
6+
7+
use PhpLlm\LlmChain\Document\NullVector;
8+
use PhpLlm\LlmChain\Document\VectorInterface;
9+
use PhpLlm\LlmChain\Exception\RuntimeException;
10+
use PHPUnit\Framework\Attributes\CoversClass;
11+
use PHPUnit\Framework\Attributes\Test;
12+
use PHPUnit\Framework\TestCase;
13+
14+
#[CoversClass(NullVector::class)]
15+
final class NullVectorTest extends TestCase
16+
{
17+
#[Test]
18+
public function implementsInterface(): void
19+
{
20+
self::assertInstanceOf(VectorInterface::class, new NullVector());
21+
}
22+
23+
#[Test]
24+
public function getDataThrowsOnAccess(): void
25+
{
26+
$this->expectException(RuntimeException::class);
27+
28+
(new NullVector())->getData();
29+
}
30+
31+
#[Test]
32+
public function getDimensionsThrowsOnAccess(): void
33+
{
34+
$this->expectException(RuntimeException::class);
35+
36+
(new NullVector())->getDimensions();
37+
}
38+
}

tests/Document/VectorTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@
55
namespace PhpLlm\LlmChain\Tests\Document;
66

77
use PhpLlm\LlmChain\Document\Vector;
8+
use PhpLlm\LlmChain\Document\VectorInterface;
89
use PHPUnit\Framework\Attributes\CoversClass;
910
use PHPUnit\Framework\Attributes\Test;
1011
use PHPUnit\Framework\TestCase;
1112

1213
#[CoversClass(Vector::class)]
1314
final class VectorTest extends TestCase
1415
{
16+
#[Test]
17+
public function implementsInterface(): void
18+
{
19+
self::assertInstanceOf(
20+
VectorInterface::class,
21+
new Vector([1.0, 2.0, 3.0])
22+
);
23+
}
24+
1525
#[Test]
1626
public function withDimensionNull(): void
1727
{

0 commit comments

Comments
 (0)