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

Commit 07f321b

Browse files
Added possibility to directly access the response vpack
1 parent ed4b9d7 commit 07f321b

File tree

3 files changed

+52
-28
lines changed

3 files changed

+52
-28
lines changed

src/main.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,9 @@ extern "C" {
155155

156156
response.method<&arangodb::fuerte::php::Response::getHttpCode>("getHttpCode");
157157
response.method<&arangodb::fuerte::php::Response::getBody>("getBody");
158-
response.method<&arangodb::fuerte::php::Response::accessResponse>("accessResponse", {
159-
Php::ByVal("accessor", Php::Type::String)
158+
response.method<&arangodb::fuerte::php::Response::get>("get", {
159+
Php::ByVal("accessor")
160160
});
161-
response.method<&arangodb::fuerte::php::Response::accessResponseTop>("accessResponseTop", {
162-
Php::ByVal("accessor", Php::Type::String)
163-
});
164-
165161
extension->add(std::move(response));
166162
}
167163

src/response.cpp

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,37 @@ namespace arangodb { namespace fuerte { namespace php {
3030
return body;
3131
}
3232

33-
Php::Value Response::accessResponse(Php::Parameters& params)
33+
Php::Value Response::get(Php::Parameters& params)
3434
{
35-
std::vector<std::string> accessor;
36-
std::istringstream accessorStr(params[0]);
37-
38-
for(std::string token; std::getline(accessorStr, token, '.'); ) {
39-
accessor.push_back(std::move(token));
40-
}
41-
42-
vp::Slice slice = this->response.slices().front().get(accessor);
43-
return this->sliceToPhpValue(slice);
44-
}
35+
try {
4536

46-
Php::Value Response::accessResponseTop(Php::Parameters& params)
47-
{
48-
std::string accessor = params[0];
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+
}
4959

50-
vp::Slice slice = this->response.slices().front().get(accessor);
51-
return this->sliceToPhpValue(slice);
60+
} catch(vp::Exception e) {
61+
ARANGODB_THROW(RuntimeException(), e.what());
62+
return NULL;
63+
}
5264
}
5365

5466
Php::Value Response::sliceToPhpValue(const vp::Slice& slice)
@@ -73,11 +85,8 @@ namespace arangodb { namespace fuerte { namespace php {
7385
break;
7486

7587
case vp::ValueType::Array:
76-
return -1;
77-
break;
78-
7988
case vp::ValueType::Object:
80-
return -1;
89+
return this->sliceToJson(slice);
8190
break;
8291

8392
default:
@@ -86,6 +95,25 @@ namespace arangodb { namespace fuerte { namespace php {
8695
}
8796

8897

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+
89117
fu::Response* Response::getFuerteResponse()
90118
{
91119
return &this->response;

src/response.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ namespace arangodb { namespace fuerte { namespace php {
2424
fu::Response response;
2525

2626
Php::Value sliceToPhpValue(const vp::Slice& slice);
27+
Php::Value sliceToJson(const vp::Slice& slice);
2728

2829
public:
2930
Response(const fu::Response &response);
3031

3132
Php::Value getHttpCode();
3233
Php::Value getBody();
33-
Php::Value accessResponse(Php::Parameters& params);
34-
Php::Value accessResponseTop(Php::Parameters& params);
34+
Php::Value get(Php::Parameters& params);
3535

3636
fu::Response* getFuerteResponse();
3737
};

0 commit comments

Comments
 (0)