|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Tarantool Client package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Tarantool\Client; |
|
15
|
|
|
|
|
16
|
|
|
final class SqlQueryResult implements \ArrayAccess, \Countable, \IteratorAggregate |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var array<int, mixed> */ |
|
19
|
|
|
private $data; |
|
20
|
|
|
|
|
21
|
|
|
/** @var array<int, array<int, string>> */ |
|
22
|
|
|
private $metadata; |
|
23
|
|
|
|
|
24
|
|
|
/** @var array<int, string> */ |
|
25
|
|
|
private $keys; |
|
26
|
|
|
|
|
27
|
72 |
|
public function __construct(array $data, array $metadata) |
|
28
|
|
|
{ |
|
29
|
72 |
|
$this->data = $data; |
|
30
|
72 |
|
$this->metadata = $metadata; |
|
31
|
72 |
|
$this->keys = $metadata ? \array_column($metadata, 0) : []; |
|
32
|
72 |
|
} |
|
33
|
|
|
|
|
34
|
24 |
|
public function getData() : array |
|
35
|
|
|
{ |
|
36
|
24 |
|
return $this->data; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
6 |
|
public function getMetadata() : array |
|
40
|
|
|
{ |
|
41
|
6 |
|
return $this->metadata; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
6 |
|
public function isEmpty() : bool |
|
45
|
|
|
{ |
|
46
|
6 |
|
return !$this->data; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
9 |
|
public function getFirst() : ?array |
|
50
|
|
|
{ |
|
51
|
9 |
|
return $this->data ? \array_combine($this->keys, \reset($this->data)) : null; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
9 |
|
public function getLast() : ?array |
|
55
|
|
|
{ |
|
56
|
9 |
|
return $this->data ? \array_combine($this->keys, \end($this->data)) : null; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
public function getIterator() : \Generator |
|
60
|
|
|
{ |
|
61
|
3 |
|
foreach ($this->data as $item) { |
|
62
|
3 |
|
yield \array_combine($this->keys, $item); |
|
63
|
|
|
} |
|
64
|
3 |
|
} |
|
65
|
|
|
|
|
66
|
24 |
|
public function count() : int |
|
67
|
|
|
{ |
|
68
|
24 |
|
return \count($this->data); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
6 |
|
public function offsetExists($offset) : bool |
|
72
|
|
|
{ |
|
73
|
6 |
|
return isset($this->data[$offset]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
9 |
|
public function offsetGet($offset) : array |
|
77
|
|
|
{ |
|
78
|
9 |
|
if (!isset($this->data[$offset])) { |
|
79
|
3 |
|
throw new \OutOfBoundsException(\sprintf('The offset "%s" does not exist', $offset)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
6 |
|
return \array_combine($this->keys, $this->data[$offset]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
3 |
|
public function offsetSet($offset, $value) : void |
|
86
|
|
|
{ |
|
87
|
3 |
|
throw new \BadMethodCallException(self::class.' object cannot be modified'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
3 |
|
public function offsetUnset($offset) : void |
|
91
|
|
|
{ |
|
92
|
3 |
|
throw new \BadMethodCallException(self::class.' object cannot be modified'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|