-
Notifications
You must be signed in to change notification settings - Fork 4
/
tuple.h
137 lines (123 loc) · 3.24 KB
/
tuple.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Tuple.h
*
* A coupld of tuple classes to simplify converting variables
*
* @author Toon Schoenmakers <[email protected]>
* @copyright 2015 - 2016 Copernica BV
*/
/**
* Include guard
*/
#pragma once
/**
* Dependencies
*/
#include <phpcpp.h>
#include <yothalot.h>
#include "json/array.h"
/**
* Begin of namespace
*/
namespace Tuple {
/**
* Class to turn a php value into a yothalot tuple
*/
class Yothalot : public ::Yothalot::Tuple
{
public:
/**
* Constructor
* @param value
*/
Yothalot(const Php::Value &value)
{
// if we're numeric or a string we just return a tuple with a single value
if (value.isNumeric()) add(value.numericValue());
if (value.isString()) add(value.stringValue());
// we only support arrays and objects as the other type
if (!value.isArray() && !value.isObject()) return;
// we start looping over it
for (auto iter : value)
{
// we're going to add numeric values as numbers, everything else as strings
if (iter.second.isNumeric()) add(iter.second.numericValue());
else if (iter.second.isNull()) add(nullptr);
else add(iter.second.stringValue());
}
}
/**
* Destructor
*/
virtual ~Yothalot() = default;
};
/**
* Class to convert a yothalot tuple to a php wrapped value
*/
class Php : public ::Php::Value
{
public:
/**
* Constructor
* @param input
*/
Php(const Yothalot::Tuple &input)
{
// if we have a single field we're not building an array
if (input.fields() == 1)
{
// we're going to construct a scalar
if (input.isNumber(0)) ::Php::Value::operator=(input.number(0));
else if (input.isNull(0)) ::Php::Value::operator=(nullptr);
else ::Php::Value::operator=(input.string(0));
}
else
{
// turn the variable into an array
setType(::Php::Type::Array);
// loop over all the fields and add them one by one
for (size_t i = 0; i < input.fields(); ++i)
{
// assign the value
if (input.isNumber(i)) set(i, input.number(i));
else if (input.isNull(i)) set(i, nullptr);
else set(i, input.string(i));
}
}
}
/**
* Destructor
*/
virtual ~Php() = default;
};
/**
* Class to convert a tuple into a json representation
*/
class Json : public JSON::Array
{
public:
/**
* Constructor
* @param input
* @return JSON::Object
*/
Json(const Yothalot::Tuple &input)
{
// loop over all the fields and add them one by one
for (size_t i = 0; i < input.fields(); ++i)
{
// determine the type to add
if (input.isNumber(i)) append(input.number(i));
else if (input.isNull(i)) append(nullptr);
else append(input.string(i));
}
}
/**
* Destructor
*/
virtual ~Json() = default;
};
/**
* End of namespace
*/
}