Skip to content
This repository was archived by the owner on Jun 18, 2020. It is now read-only.

Commit b03d9a5

Browse files
authored
Merge pull request #49 from sandrokeil/feature/response-vpack-access
Added possibility to directly access the response vpack
2 parents c0257ee + 07f321b commit b03d9a5

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

src/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ extern "C" {
155155

156156
response.method<&arangodb::fuerte::php::Response::getHttpCode>("getHttpCode");
157157
response.method<&arangodb::fuerte::php::Response::getBody>("getBody");
158-
158+
response.method<&arangodb::fuerte::php::Response::get>("get", {
159+
Php::ByVal("accessor")
160+
});
159161
extension->add(std::move(response));
160162
}
161163

src/response.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,90 @@ namespace arangodb { namespace fuerte { namespace php {
3030
return body;
3131
}
3232

33+
Php::Value Response::get(Php::Parameters& params)
34+
{
35+
try {
36+
37+
if(params[0].isString()) {
38+
return this->sliceToPhpValue(this->response.slices().front().get(params[0].rawValue()));
39+
40+
} else if(params[0].isArray()) {
41+
vp::Slice tmpSlice(this->response.slices().front());
42+
43+
for(auto const &value : params[0]) {
44+
if(value.second.isNumeric()) {
45+
tmpSlice = vp::Slice(tmpSlice.at(value.second.numericValue()));
46+
} else if(value.second.isString()) {
47+
tmpSlice = vp::Slice(tmpSlice.get(value.second.rawValue()));
48+
} else {
49+
ARANGODB_THROW(InvalidArgumentException(), "The accessor may only contain strings and integers in %s on line %d");
50+
return NULL;
51+
}
52+
}
53+
54+
return this->sliceToPhpValue(tmpSlice);
55+
} else {
56+
ARANGODB_THROW(InvalidArgumentException(), "The accessor must be either of type array or of type string in %s on line %d");
57+
return NULL;
58+
}
59+
60+
} catch(vp::Exception e) {
61+
ARANGODB_THROW(RuntimeException(), e.what());
62+
return NULL;
63+
}
64+
}
65+
66+
Php::Value Response::sliceToPhpValue(const vp::Slice& slice)
67+
{
68+
switch(slice.type()) {
69+
case vp::ValueType::String:
70+
return slice.copyString();
71+
break;
72+
73+
case vp::ValueType::Int:
74+
case vp::ValueType::UInt:
75+
case vp::ValueType::SmallInt:
76+
return slice.getInt();
77+
break;
78+
79+
case vp::ValueType::Double:
80+
return slice.getDouble();
81+
break;
82+
83+
case vp::ValueType::Null:
84+
return NULL;
85+
break;
86+
87+
case vp::ValueType::Array:
88+
case vp::ValueType::Object:
89+
return this->sliceToJson(slice);
90+
break;
91+
92+
default:
93+
return -1;
94+
}
95+
}
96+
97+
98+
Php::Value Response::sliceToJson(const vp::Slice& slice)
99+
{
100+
std::string json;
101+
102+
try {
103+
vp::Options dumperOptions;
104+
105+
vp::StringSink sink(&json);
106+
vp::Dumper dumper(&sink, &dumperOptions);
107+
dumper.dump(slice);
108+
} catch(vp::Exception const& e) {
109+
ARANGODB_THROW(InvalidArgumentException(), e.what());
110+
return NULL;
111+
}
112+
113+
return json;
114+
}
115+
116+
33117
fu::Response* Response::getFuerteResponse()
34118
{
35119
return &this->response;

src/response.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
#include <phpcpp.h>
44

5+
#include <sstream>
6+
57
#include <fuerte/fuerte.h>
68
#include <fuerte/types.h>
79

810
#include "velocypack/vpack.h"
911
#include "velocypack/velocypack-exception-macros.h"
12+
#include "velocypack/ValueType.h"
1013

1114
#include "exception.h"
1215

@@ -20,12 +23,15 @@ namespace arangodb { namespace fuerte { namespace php {
2023
private:
2124
fu::Response response;
2225

26+
Php::Value sliceToPhpValue(const vp::Slice& slice);
27+
Php::Value sliceToJson(const vp::Slice& slice);
28+
2329
public:
2430
Response(const fu::Response &response);
2531

2632
Php::Value getHttpCode();
27-
2833
Php::Value getBody();
34+
Php::Value get(Php::Parameters& params);
2935

3036
fu::Response* getFuerteResponse();
3137
};

0 commit comments

Comments
 (0)