@@ -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 ;
0 commit comments