-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathVector.php
More file actions
47 lines (38 loc) · 1.11 KB
/
Vector.php
File metadata and controls
47 lines (38 loc) · 1.11 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
<?php
namespace Pgvector;
class Vector
{
protected array $value;
public function __construct(mixed $value)
{
if (is_string($value)) {
try {
$value = json_decode($value, true, 2, JSON_THROW_ON_ERROR);
} catch (\Exception $e) {
// do nothing
}
if (!is_array($value) || !array_is_list($value)) {
throw new \InvalidArgumentException("Invalid text representation");
}
} else {
if ($value instanceof \SplFixedArray) {
$value = $value->toArray();
}
if (!is_array($value)) {
throw new \InvalidArgumentException("Expected array");
}
if (!array_is_list($value)) {
throw new \InvalidArgumentException("Expected array to be a list");
}
}
$this->value = $value;
}
public function __toString(): string
{
return json_encode($this->value, JSON_THROW_ON_ERROR, 1);
}
public function toArray(): array
{
return $this->value;
}
}