-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathphp_variable.cpp
More file actions
138 lines (115 loc) · 3.7 KB
/
php_variable.cpp
File metadata and controls
138 lines (115 loc) · 3.7 KB
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
/**
* ToPhp.cpp
*
* Implementation file to convert a v8/javascript variable into its
* PHP counterpart
*
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
* @copyright 2025 Copernica BV
*/
/**
* Dependencies
*/
#include "php_variable.h"
#include "php_object.h"
#include "php_function.h"
#include "core.h"
#include "linker.h"
#include "php_array.h"
#include "names.h"
/**
* Begin of namespace
*/
namespace JS {
/**
* Constructor
* @param isolate
* @param input
*/
PhpVariable::PhpVariable(v8::Isolate *isolate, const v8::Local<v8::Value> &input)
{
// if we received an invalid input we simply keep empty PHP value
if (input.IsEmpty() || input->IsNull() || input->IsUndefined()) return;
// check for boolean true/false or a javascript "Boolean" instance
if (input->IsBoolean() || input->IsBooleanObject())
{
// convert the value to a boolean
v8::Local<v8::Boolean> value(v8::Local<v8::Boolean>::Cast(input));
// expose the value
_value = value->Value();
}
else if (input->IsInt32())
{
// convert the value to an int32
v8::Local<v8::Int32> value(v8::Local<v8::Int32>::Cast(input));
// expose the value
_value = value->Value();
}
else if (input->IsNumber() || input->IsNumberObject())
{
// convert the value to a number
v8::Local<v8::Number> value(v8::Local<v8::Number>::Cast(input));
// expose the value
_value = value->Value();
}
else if (input->IsString())
{
// convert input to a string
v8::Local<v8::String> value(v8::Local<v8::String>::Cast(input));
// convert to
v8::String::Utf8Value utf8(isolate, value);
// expose to the php value
_value = Php::Value(*utf8, utf8.length());
}
else if (input->IsStringObject())
{
// convert input to a string
v8::Local<v8::StringObject> value(v8::Local<v8::StringObject>::Cast(input));
// convert to
v8::String::Utf8Value utf8(isolate, value);
// expose to the php value
_value = Php::Value(*utf8, utf8.length());
}
else if (input->IsRegExp())
{
// convert input to a regexp
v8::Local<v8::RegExp> value(v8::Local<v8::RegExp>::Cast(input));
// convert to
v8::String::Utf8Value utf8(isolate, value);
// expose to the php value
_value = Php::Value(*utf8, utf8.length());
}
else if (input->IsFunction())
{
// retrieve the function
auto func = input.As<v8::Function>();
// use a linker to check if the object was already associated with a php::value
Linker linker(isolate, func);
// if already linked
if (linker.valid()) _value = linker.value();
// otherwise we associate the object now
else _value = linker.attach(Php::Object(Names::Function, new PhpFunction(isolate, func)));
}
else if (input->IsArray())
{
// convert input to a string
v8::Local<v8::Array> value(v8::Local<v8::Array>::Cast(input));
// we have a helper class for filling arrays
_value = PhpArray(isolate, value);
}
else if (input->IsObject())
{
// retrieve the object
auto object = input.As<v8::Object>();
// use a linker to check if the object was already associated with a php::value
Linker linker(isolate, object);
// if already linked
if (linker.valid()) _value = linker.value();
// otherwise we associate the object now
else _value = linker.attach(Php::Object(Names::Object, new PhpObject(isolate, object)));
}
}
/**
* End of namespace
*/
}