SqlQueryResult::getFirst()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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 121
    public function __construct(array $data, array $metadata)
28
    {
29 121
        $this->data = $data;
30 121
        $this->metadata = $metadata;
31 121
        $this->keys = $metadata ? \array_column($metadata, 0) : [];
32
    }
33
34 40
    public function getData() : array
35
    {
36 40
        return $this->data;
37
    }
38
39 16
    public function getMetadata() : array
40
    {
41 16
        return $this->metadata;
42
    }
43
44 13
    public function isEmpty() : bool
45
    {
46 13
        return !$this->data;
47
    }
48
49 17
    public function getFirst() : ?array
50
    {
51 17
        return $this->data ? \array_combine($this->keys, \reset($this->data)) : null;
52
    }
53
54 12
    public function getLast() : ?array
55
    {
56 12
        return $this->data ? \array_combine($this->keys, \end($this->data)) : null;
57
    }
58
59 4
    public function getIterator() : \Generator
60
    {
61 4
        foreach ($this->data as $item) {
62 4
            yield \array_combine($this->keys, $item);
63
        }
64
    }
65
66 32
    public function count() : int
67
    {
68 32
        return \count($this->data);
69
    }
70
71 8
    public function offsetExists($offset) : bool
72
    {
73 8
        return isset($this->data[$offset]);
74
    }
75
76 16
    public function offsetGet($offset) : array
77
    {
78 16
        if (!isset($this->data[$offset])) {
79 4
            throw new \OutOfBoundsException(\sprintf('The offset "%s" does not exist', $offset));
80
        }
81
82 12
        return \array_combine($this->keys, $this->data[$offset]);
83
    }
84
85 4
    public function offsetSet($offset, $value) : void
86
    {
87 4
        throw new \BadMethodCallException(self::class.' object cannot be modified');
88
    }
89
90 4
    public function offsetUnset($offset) : void
91
    {
92 4
        throw new \BadMethodCallException(self::class.' object cannot be modified');
93
    }
94
}
95