Completed
Pull Request — master (#62)
by Eugene
14:40 queued 04:42
created

SqlQueryResult::__get()   A

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 1
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 \IteratorAggregate, \Countable
17
{
18
    private $data;
19
    private $metadata;
20
    private $keys;
21
22 28
    public function __construct(array $data, array $metadata)
23
    {
24 28
        $this->data = $data;
25 28
        $this->metadata = $metadata;
26
        $this->keys = ([] === $metadata) ? [] : \array_column($metadata, 0);
27 28
    }
28 28
29
    public function getData() : array
30 10
    {
31
        return $this->data;
32 10
    }
33
34
    public function getMetadata() : array
35 4
    {
36
        return $this->metadata;
37 4
    }
38
39
    public function isEmpty() : bool
40 4
    {
41
        return [] === $this->data;
42 4
    }
43
44
    public function getFirst() : ?array
45 4
    {
46
        return $this->data ? \array_combine($this->keys, \reset($this->data)) : null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data ? arr...et($this->data)) : null could return the type false which is incompatible with the type-hinted return array|null. Consider adding an additional type-check to rule them out.
Loading history...
47 4
    }
48
49
    public function getLast() : ?array
50 4
    {
51
        return $this->data ? \array_combine($this->keys, \end($this->data)) : null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data ? arr...nd($this->data)) : null could return the type false which is incompatible with the type-hinted return array|null. Consider adding an additional type-check to rule them out.
Loading history...
52 4
    }
53
54
    public function getIterator() : \Generator
55 2
    {
56
        foreach ($this->data as $item) {
57 2
            yield \array_combine($this->keys, $item);
58 2
        }
59
    }
60 2
61
    public function count() : int
62 2
    {
63
        return \count($this->data);
64 2
    }
65
}
66