Completed
Pull Request — master (#7)
by Sandro
02:42
created

VpackStreamHandler::result()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Sandro Keil (https://sandro-keil.de)
4
 *
5
 * @link      http://github.com/sandrokeil/arangodb-php-client for the canonical source repository
6
 * @copyright Copyright (c) 2018-2019 Sandro Keil
7
 * @license   http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License
8
 */
9
10
declare(strict_types=1);
11
12
namespace ArangoDb\Statement;
13
14
use Psr\Http\Message\StreamInterface;
15
use Velocypack\Vpack;
0 ignored issues
show
Bug introduced by
The type Velocypack\Vpack was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
/**
18
 * ArangoDB Velocypack stream handler
19
 *
20
 * @see https://github.com/martin-schilling/php-velocypack
21
 */
22
class VpackStreamHandler implements StreamHandler
23
{
24
    use ArrayAccessStreamHandlerTrait;
25
26
    public const RESULT_TYPE_ARRAY = 0;
27
    public const RESULT_TYPE_JSON = 1;
28
    public const RESULT_TYPE_BINARY = 2;
29
    public const RESULT_TYPE_OBJECT = 3;
30
31
    /**
32
     * @var Vpack[]
33
     */
34
    private $data = [];
35
36
    /**
37
     * @var int
38
     */
39
    private $resultType;
40
41
42 2
    public function __construct(StreamInterface $stream, int $resultType)
43
    {
44 2
        $this->data[$this->fetches] = Vpack::fromBinary($stream->getContents());
45 2
        $this->length = count($this->data[$this->fetches]['result']);
46
        $this->batchSize = $this->length;
47
        $this->resultType = $resultType;
48
    }
49
50
    /**
51
     * @return array|object|string
52
     */
53
    public function current()
54
    {
55
        return $this->toType(
56
            $this->data[$this->fetches]['result'][$this->position - ($this->batchSize * $this->fetches)]
57
        );
58
    }
59
60
    /**
61
     * @return array|object|string
62
     */
63
    public function result()
64
    {
65
        return $this->toType($this->data[$this->fetches]['result']);
66
    }
67
68
    public function raw(): array
69
    {
70
        return $this->toType($this->data[$this->fetches]);
71
    }
72
73
    public function completeResult()
74
    {
75
        $data = $this->resultType === self::RESULT_TYPE_ARRAY ? [[]] : [];
76
77
        $i = 0;
78
79
        foreach ($this->data as $vpack) {
80
            switch ($this->resultType) {
81
                case self::RESULT_TYPE_ARRAY:
82
                    $data[] = $vpack['result']->toArray();
83
                    break;
84
                case self::RESULT_TYPE_JSON:
85
                default:
86
//                    $data .= $i === 0 ? '' : ',';
87
//                    $data .= $vpack['result']->toJson();
88
                    $value = $vpack['result']->toJson();
89
                    $value = rtrim($value, ']');
90
                    $value = ltrim($value, '[');
91
                    $data[] = $value;
92
                    break;
93
            }
94
            $i++;
95
        }
96
        if ($this->resultType === self::RESULT_TYPE_JSON) {
97
            $data = implode(',', $data);
98
99
            if ($i > 0) {
100
                return '[' . $data . ']';
101
            }
102
            return $data;
103
        }
104
105
        return  array_merge(...$data);
106
107
        // TODO needs append method
108
//        $vpack = new Vpack();
109
//        foreach ($this->data as $result) {
110
//            $vpack->append($result['result']);
111
//        }
112
//        return $this->toType($vpack);
113
    }
114
115
    public function appendStream(StreamInterface $stream): void
116
    {
117
        $this->data[++$this->fetches] = Vpack::fromBinary($stream->getContents());
118
        $this->length += count($this->data[$this->fetches]['result']);
119
    }
120
121
    /**
122
     * @param Vpack $vpack
123
     * @return array|object|string
124
     */
125
    private function toType(Vpack $vpack)
126
    {
127
        switch ($this->resultType) {
128
            case self::RESULT_TYPE_OBJECT:
129
                return (object)$vpack->toArray();
130
            case self::RESULT_TYPE_ARRAY:
131
                return $vpack->toArray();
132
            case self::RESULT_TYPE_BINARY:
133
                return $vpack->toBinary();
134
            case self::RESULT_TYPE_JSON:
135
            default:
136
                return $vpack->toJson();
137
        }
138
    }
139
}
140