Completed
Pull Request — master (#7)
by Sandro
05:37
created

VpackStreamHandler::raw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
    public function __construct(StreamInterface $stream, int $resultType)
43
    {
44
        $this->data[$this->fetches] = Vpack::fromBinary($stream->getContents());
45
        $this->length = count($this->data[$this->fetches]['result']);
46
        $this->batchSize = $this->length;
47
        $this->resultType = $resultType;
48
    }
49
50
    public function current()
51
    {
52
        return $this->toType(
53
            $this->data[$this->fetches]['result'][$this->position - ($this->batchSize * $this->fetches)]
54
        );
55
    }
56
57
    public function result()
58
    {
59
        return $this->toType($this->data[$this->fetches]['result']);
60
    }
61
62
    public function raw(): array
63
    {
64
        return $this->toType($this->data[$this->fetches]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->toType($this->data[$this->fetches]) could return the type object which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
65
    }
66
67
    public function completeResult()
68
    {
69
        $data = $this->resultType === self::RESULT_TYPE_ARRAY ? [[]] : [];
70
71
        $i = 0;
72
73
        foreach ($this->data as $vpack) {
74
            switch ($this->resultType) {
75
                case self::RESULT_TYPE_ARRAY:
76
                    $data[] = $vpack['result']->toArray();
77
                    break;
78
                case self::RESULT_TYPE_JSON:
79
                default:
80
//                    $data .= $i === 0 ? '' : ',';
81
//                    $data .= $vpack['result']->toJson();
82
                    $value = $vpack['result']->toJson();
83
                    $value = rtrim($value, ']');
84
                    $value = ltrim($value, '[');
85
                    $data[] = $value;
86
                    break;
87
            }
88
            $i++;
89
        }
90
        if ($this->resultType === self::RESULT_TYPE_JSON) {
91
            $data = implode(',', $data);
92
93
            if ($i > 0) {
94
                return '[' . $data . ']';
95
            }
96
            return $data;
97
        }
98
99
        return  array_merge(...$data);
100
101
        // TODO needs append method
102
//        $vpack = new Vpack();
103
//        foreach ($this->data as $result) {
104
//            $vpack->append($result['result']);
105
//        }
106
//        return $this->toType($vpack);
107
    }
108
109
    public function appendStream(StreamInterface $stream): void
110
    {
111
        $this->data[++$this->fetches] = Vpack::fromBinary($stream->getContents());
112
        $this->length += count($this->data[$this->fetches]['result']);
113
    }
114
115
    private function toType(Vpack $vpack)
116
    {
117
        switch ($this->resultType) {
118
            case self::RESULT_TYPE_OBJECT:
119
                return (object)$vpack->toArray();
120
            case self::RESULT_TYPE_ARRAY:
121
                return $vpack->toArray();
122
            case self::RESULT_TYPE_BINARY:
123
                return $vpack->toBinary();
124
            case self::RESULT_TYPE_JSON:
125
            default:
126
                return $vpack->toJson();
127
        }
128
    }
129
}
130