Skip to content

Commit 9334f8d

Browse files
committed
chore: prepare for v1
1 parent 02b1fd8 commit 9334f8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+387
-65
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"brianium/paratest": "^7.4",
3333
"laravel/pint": "^1.0",
3434
"nunomaduro/collision": "^8.5",
35+
"phpstan/phpstan": "^2.1",
3536
"phpunit/phpunit": "^11.5.6",
3637
"robiningelbrecht/phpunit-pretty-print": "^1.3",
3738
"spatie/ray": "^1.28",

phpstan.neon.dist

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 7
3+
paths:
4+
- src
5+
- tests

src/Contracts/Arrayable.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Upstash\Vector\Contracts;
4+
5+
interface Arrayable
6+
{
7+
/**
8+
* @return array<string, mixed>
9+
*/
10+
public function toArray(): array;
11+
}

src/DataQuery.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
namespace Upstash\Vector;
44

5+
use Upstash\Vector\Contracts\Arrayable;
56
use Upstash\Vector\Enums\FusionAlgorithm;
67
use Upstash\Vector\Enums\QueryMode;
78
use Upstash\Vector\Enums\WeightingStrategy;
89

9-
final readonly class DataQuery
10+
final readonly class DataQuery implements Arrayable
1011
{
1112
public function __construct(
1213
public string $data,
@@ -20,6 +21,9 @@ public function __construct(
2021
public QueryMode $queryMode = QueryMode::HYBRID,
2122
) {}
2223

24+
/**
25+
* @return array<string, mixed>
26+
*/
2327
public function toArray(): array
2428
{
2529
$data = [

src/DataQueryResult.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
use Countable;
88
use IteratorAggregate;
99
use Traversable;
10+
use Upstash\Vector\Contracts\Arrayable;
1011

1112
/**
1213
* @implements IteratorAggregate<int,VectorMatch>
1314
* @implements ArrayAccess<int,VectorMatch>
1415
*/
15-
final readonly class DataQueryResult implements ArrayAccess, Countable, IteratorAggregate
16+
final readonly class DataQueryResult implements Arrayable, ArrayAccess, Countable, IteratorAggregate
1617
{
1718
/**
1819
* @param array<VectorMatch> $results
@@ -28,7 +29,7 @@ public function count(): int
2829

2930
public function getIterator(): Traversable
3031
{
31-
return new ArrayIterator($this->getResults());
32+
return new ArrayIterator($this->results);
3233
}
3334

3435
/**
@@ -52,4 +53,16 @@ public function offsetGet(mixed $offset): VectorMatch
5253
public function offsetSet(mixed $offset, mixed $value): void {}
5354

5455
public function offsetUnset(mixed $offset): void {}
56+
57+
/**
58+
* @return array{
59+
* results: VectorMatch[]
60+
* }
61+
*/
62+
public function toArray(): array
63+
{
64+
return [
65+
'results' => $this->results,
66+
];
67+
}
5568
}

src/DataUpsert.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22

33
namespace Upstash\Vector;
44

5-
final readonly class DataUpsert
5+
use Upstash\Vector\Contracts\Arrayable;
6+
7+
final readonly class DataUpsert implements Arrayable
68
{
9+
/**
10+
* @param array<string, mixed> $metadata
11+
*/
712
public function __construct(
813
public string $id,
914
public string $data,
1015
public array $metadata = [],
1116
) {}
1217

18+
/**
19+
* @return array<string, mixed>
20+
*/
1321
public function toArray(): array
1422
{
1523
$result = [

src/IndexInfo.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Upstash\Vector;
44

5-
final readonly class IndexInfo
5+
use Upstash\Vector\Contracts\Arrayable;
6+
7+
final readonly class IndexInfo implements Arrayable
68
{
79
/**
810
* @param array<string, NamespaceInfo> $namespaces
@@ -20,4 +22,22 @@ public function namespace(string $namespace): NamespaceInfo
2022
{
2123
return $this->namespaces[$namespace] ?? new NamespaceInfo;
2224
}
25+
26+
/**
27+
* @return array{
28+
* indexSize: int,
29+
* dimension: int,
30+
* similarityFunction: string,
31+
* namespaces: array<string, NamespaceInfo>
32+
* }
33+
*/
34+
public function toArray(): array
35+
{
36+
return [
37+
'indexSize' => $this->indexSize,
38+
'dimension' => $this->dimension,
39+
'similarityFunction' => $this->similarityFunction,
40+
'namespaces' => $this->namespaces,
41+
];
42+
}
2343
}

src/Iterators/VectorRangeIterator.php

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class VectorRangeIterator implements \Iterator
1616

1717
private int $position = 0;
1818

19+
/**
20+
* @var VectorMatch[]
21+
*/
1922
private array $results = [];
2023

2124
public function __construct(

src/NamespaceInfo.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,26 @@
22

33
namespace Upstash\Vector;
44

5-
final readonly class NamespaceInfo
5+
use Upstash\Vector\Contracts\Arrayable;
6+
7+
final readonly class NamespaceInfo implements Arrayable
68
{
79
public function __construct(
810
public int $vectorCount = 0,
911
public int $pendingVectorCount = 0,
1012
) {}
13+
14+
/**
15+
* @return array{
16+
* vectorCount: int,
17+
* pendingVectorCount: int
18+
* }
19+
*/
20+
public function toArray(): array
21+
{
22+
return [
23+
'vectorCount' => $this->vectorCount,
24+
'pendingVectorCount' => $this->pendingVectorCount,
25+
];
26+
}
1127
}

src/Operations/Concerns/MapsVectorMatches.php

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
trait MapsVectorMatches
99
{
10+
/**
11+
* @param array<mixed> $result
12+
*/
1013
private function mapVectorMatch(array $result): VectorMatch
1114
{
1215
$vector = [];

src/Operations/DeleteVectorsOperation.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private function transformResponse(TransporterResponse $response): VectorDeleteR
6868
}
6969

7070
/**
71-
* @param array<string|VectorIdentifierInterface> $ids
71+
* @param array<string|VectorIdentifierInterface|mixed> $ids
7272
* @return array<string>
7373
*/
7474
private function mapIds(array $ids): array

src/Operations/GetIndexInfoOperation.php

+16-10
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
use Upstash\Vector\Contracts\TransporterInterface;
66
use Upstash\Vector\IndexInfo;
77
use Upstash\Vector\NamespaceInfo;
8+
use Upstash\Vector\Operations\Concerns\AssertsApiResponseErrors;
89
use Upstash\Vector\Transporter\ContentType;
910
use Upstash\Vector\Transporter\Method;
1011
use Upstash\Vector\Transporter\TransporterRequest;
12+
use Upstash\Vector\Transporter\TransporterResponse;
1113

1214
/**
1315
* @internal
1416
*/
1517
final readonly class GetIndexInfoOperation
1618
{
19+
use AssertsApiResponseErrors;
20+
1721
public function __construct(private TransporterInterface $transporter) {}
1822

1923
public function getInfo(): IndexInfo
@@ -26,28 +30,30 @@ public function getInfo(): IndexInfo
2630

2731
$response = $this->transporter->sendRequest($request);
2832

29-
$data = json_decode($response->data, true);
33+
$this->assertResponse($response);
3034

31-
return $this->transformDataToIndexInfo($data['result']);
35+
return $this->transformResponse($response);
3236
}
3337

34-
private function transformDataToIndexInfo(array $data): IndexInfo
38+
private function transformResponse(TransporterResponse $response): IndexInfo
3539
{
36-
$namespaces = [];
40+
$data = json_decode($response->data, true);
41+
$result = $data['result'] ?? [];
3742

38-
foreach ($data['namespaces'] as $namespace => $namespaceData) {
43+
$namespaces = [];
44+
foreach ($result['namespaces'] as $namespace => $namespaceData) {
3945
$namespaces[$namespace] = new NamespaceInfo(
4046
vectorCount: $namespaceData['vectorCount'],
4147
pendingVectorCount: $namespaceData['pendingVectorCount'],
4248
);
4349
}
4450

4551
return new IndexInfo(
46-
vectorCount: $data['vectorCount'],
47-
pendingVectorCount: $data['pendingVectorCount'],
48-
indexSize: $data['indexSize'],
49-
dimension: $data['dimension'],
50-
similarityFunction: $data['similarityFunction'],
52+
vectorCount: $result['vectorCount'],
53+
pendingVectorCount: $result['pendingVectorCount'],
54+
indexSize: $result['indexSize'],
55+
dimension: $result['dimension'],
56+
similarityFunction: $result['similarityFunction'],
5157
namespaces: $namespaces,
5258
);
5359
}

src/Operations/ListNamespacesOperation.php

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
public function __construct(private TransporterInterface $transporter) {}
2020

21+
/**
22+
* @return array<string>
23+
*/
2124
public function list(): array
2225
{
2326
$request = new TransporterRequest(
@@ -33,6 +36,9 @@ public function list(): array
3336
return $this->transformResponse($response);
3437
}
3538

39+
/**
40+
* @return array<string>
41+
*/
3642
private function transformResponse(TransporterResponse $response): array
3743
{
3844
$data = json_decode($response->data, true);

src/Operations/QueryVectorsManyOperation.php

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ private function getPath(): string
5757
return '/query';
5858
}
5959

60+
/**
61+
* @param array<string|int> $queryKeys
62+
*/
6063
private function transformResponse(TransporterResponse $response, array $queryKeys): VectorQueryManyResult
6164
{
6265
$data = json_decode($response->data, true);

src/Operations/UpsertDataOperation.php

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public function upsert(DataUpsert $upsert): void
2525
$this->assertResponse($response);
2626
}
2727

28+
/**
29+
* @param array<DataUpsert> $upsert
30+
*/
2831
public function upsertMany(array $upsert): void
2932
{
3033
$data = array_map(fn (DataUpsert $upsert) => $upsert->toArray(), $upsert);
@@ -35,6 +38,9 @@ public function upsertMany(array $upsert): void
3538
$this->assertResponse($response);
3639
}
3740

41+
/**
42+
* @param array<string, mixed> $data
43+
*/
3844
private function createRequest(array $data): TransporterRequest
3945
{
4046
$namespace = trim($this->namespace);

src/Operations/UpsertVectorOperation.php

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public function upsertMany(array $upsert): void
4040
$this->assertResponse($response);
4141
}
4242

43+
/**
44+
* @param array<string, mixed> $data
45+
*/
4346
private function createRequest(array $data): TransporterRequest
4447
{
4548
$namespace = trim($this->namespace);

src/SparseVector.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@
22

33
namespace Upstash\Vector;
44

5-
final readonly class SparseVector
5+
use Upstash\Vector\Contracts\Arrayable;
6+
7+
final readonly class SparseVector implements Arrayable
68
{
79
/**
810
* @param array<int> $indices
911
* @param array<float> $values
1012
*/
1113
public function __construct(public array $indices = [], public array $values = []) {}
1214

15+
/**
16+
* @param array<int> $indices
17+
* @param array<float> $values
18+
*/
1319
public static function of(array $indices = [], array $values = []): SparseVector
1420
{
1521
return new self($indices, $values);
1622
}
1723

24+
/**
25+
* @return array{indices: array<int>, values: array<float>}
26+
*/
1827
public function toArray(): array
1928
{
2029
return [

src/Telemetry/SdkVersion.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@ class SdkVersion
1111
public static function resolve(): string
1212
{
1313
try {
14-
$version = json_decode(file_get_contents(__DIR__.'/../../composer.json'), true);
14+
$composerContents = file_get_contents(__DIR__.'/../../composer.json');
15+
if (! $composerContents) {
16+
return static::UNKNOWN;
17+
}
1518

16-
return $version['version'] ?? static::UNKNOWN;
19+
$json = json_decode($composerContents, true);
20+
if (! isset($json['version'])) {
21+
return static::UNKNOWN;
22+
}
23+
24+
return $json['version'];
1725
} catch (Throwable $e) {
1826
return static::UNKNOWN;
1927
}

src/Transporter/Headers.php

+6
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
*/
88
class Headers
99
{
10+
/**
11+
* @param array<string, string> $headers
12+
*/
1013
public function __construct(private array $headers = []) {}
1114

1215
public function withHeader(string $header, string $value): self
1316
{
1417
return new Headers([...$this->headers, $header => $value]);
1518
}
1619

20+
/**
21+
* @return string[]
22+
*/
1723
public function toArray(): array
1824
{
1925
return $this->headers;

0 commit comments

Comments
 (0)