-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathphp_array.h
More file actions
89 lines (78 loc) · 1.91 KB
/
php_array.h
File metadata and controls
89 lines (78 loc) · 1.91 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
/**
* PhpArray.h
*
* Helper class to turn a v8/javascript array into a php array
*
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
* @copyright 2025 Copernica BV
*/
/**
* Include guard
*/
#pragma once
/**
* Dependencies
*/
#include "core.h"
#include "php_variable.h"
/**
* Begin of namespace
*/
namespace JS {
/**
* Class definition
*/
class PhpArray : public Php::Array
{
public:
/**
* Contructor based on a v8::Array
* @param isolate
* @param input
*/
PhpArray(v8::Isolate *isolate, const v8::Local<v8::Array> &input)
{
// we need a context
auto ctx = isolate->GetCurrentContext();
// iterate over the input array
for (uint32_t i = 0; i < input->Length(); ++i)
{
// get item from the array
v8::MaybeLocal<v8::Value> maybe = input->Get(ctx, i);
if (maybe.IsEmpty()) continue;
// the underlying element (arrays can be sparse)
v8::Local<v8::Value> element = maybe.ToLocalChecked();
if (element->IsUndefined()) continue;
// set this in the output array
set(i, PhpVariable(isolate, element));
}
}
/**
* Contructor based on a v8::Array
* @param isolate
* @param input
*/
PhpArray(v8::Isolate *isolate, const v8::FunctionCallbackInfo<v8::Value> &input)
{
// iterate over the input array
for (int i = 0; i < input.Length(); ++i)
{
// set this in the output array
set(i, PhpVariable(isolate, input[i]));
}
}
/**
* Constructor for arguments
* @param info
*/
PhpArray(const v8::FunctionCallbackInfo<v8::Value> &args) :
PhpArray(args.GetIsolate(), args) {}
/**
* Destructor
*/
virtual ~PhpArray() = default;
};
/**
* End of namespace
*/
}