-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_iterable_type.v
More file actions
173 lines (142 loc) · 3.92 KB
/
Copy pathphp_iterable_type.v
File metadata and controls
173 lines (142 loc) · 3.92 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
module vphp
pub struct PhpIterable {
mut:
value PhpValueZBox
}
pub fn PhpIterable.from_zval(z ZVal) ?PhpIterable {
if z.is_array() || (z.is_object() && z.is_instance_of('Traversable')) {
return PhpIterable{
value: PhpValueZBox.from_zval(z)
}
}
return none
}
pub fn PhpIterable.must_from_zval(z ZVal) !PhpIterable {
iter := PhpIterable.from_zval(z) or { return error('zval is not iterable') }
return iter
}
pub fn PhpIterable.from_request_owned_zbox(value RequestOwnedZBox) ?PhpIterable {
z := value.to_zval()
if z.is_array() || (z.is_object() && z.is_instance_of('Traversable')) {
return PhpIterable{
value: PhpValueZBox.request_owned(value)
}
}
return none
}
pub fn PhpIterable.from_persistent_owned_zbox(value PersistentOwnedZBox) ?PhpIterable {
z := value.to_zval()
if z.is_array() || (z.is_object() && z.is_instance_of('Traversable')) {
return PhpIterable{
value: PhpValueZBox.persistent_owned(value)
}
}
return none
}
pub fn PhpIterable.from_persistent_zval(z ZVal) ?PhpIterable {
return PhpIterable.from_persistent_owned_zbox(PersistentOwnedZBox.from_persistent_zval(z))
}
pub fn (i PhpIterable) to_zval() ZVal {
return i.value.to_zval()
}
pub fn (i PhpIterable) to_value() PhpValue {
return PhpValue{
value: i.value.clone()
}
}
pub fn (i PhpIterable) to_array() !PhpArray {
if arr := i.to_value().as_array() {
return arr
}
if !i.is_traversable() {
return error('value is not iterable')
}
mut out := PhpArray.new()
i.fold[[]int]([]int{}, fn [out] (key ZVal, value ZVal, mut _ []int) {
item := PhpValue.from_zval(value)
if key.is_string() {
out.set_value(key.to_string(), item)
return
}
if key.is_long() {
out.push_value(item)
}
})
return out
}
pub fn (i PhpIterable) to_borrowed() PhpIterable {
return PhpIterable.from_zval(i.value.to_borrowed_zbox().to_zval()) or { i }
}
pub fn (i PhpIterable) borrowed() PhpIterable {
return i.to_borrowed()
}
pub fn (i PhpIterable) borrow() PhpIterable {
return i.to_borrowed()
}
pub fn (i PhpIterable) to_borrowed_zbox() RequestBorrowedZBox {
return i.value.to_borrowed_zbox()
}
pub fn (i PhpIterable) to_request_owned() PhpIterable {
return PhpIterable.from_request_owned_zbox(i.value.to_request_owned_zbox()) or {
i.to_borrowed()
}
}
pub fn (i PhpIterable) owned() PhpIterable {
return i.to_request_owned()
}
pub fn (i PhpIterable) to_request_owned_zbox() RequestOwnedZBox {
return i.value.to_request_owned_zbox()
}
pub fn (i PhpIterable) to_persistent_owned() PhpIterable {
return PhpIterable.from_persistent_owned_zbox(i.value.to_persistent_owned_zbox()) or {
i.to_borrowed()
}
}
pub fn (i PhpIterable) retain() PhpIterable {
return i.to_persistent_owned()
}
pub fn (i PhpIterable) retained() PhpIterable {
return i.to_persistent_owned()
}
pub fn (i PhpIterable) to_persistent_owned_zbox() PersistentOwnedZBox {
return i.value.to_persistent_owned_zbox()
}
pub fn (i PhpIterable) is_borrowed() bool {
return i.value.is_borrowed()
}
pub fn (i PhpIterable) is_owned() bool {
return i.value.is_request_owned()
}
pub fn (i PhpIterable) is_retained() bool {
return i.value.is_retained()
}
pub fn (mut i PhpIterable) take_zval() ZVal {
return i.value.take_zval()
}
pub fn (mut i PhpIterable) release() {
i.value.release()
}
pub fn (i PhpIterable) is_array() bool {
return i.to_zval().is_array()
}
pub fn (i PhpIterable) is_traversable() bool {
return i.to_zval().is_object() && i.to_zval().is_instance_of('Traversable')
}
pub fn (i PhpIterable) count() int {
return i.key_strings().len
}
pub fn (i PhpIterable) fold[T](init T, cb ForeachWithCtxCb[T]) T {
return i.to_zval().foreach_with_ctx[T](init, cb)
}
pub fn (i PhpIterable) key_strings() []string {
return i.to_zval().foreach_with_ctx[[]string]([]string{}, fn (key ZVal, _ ZVal, mut acc []string) {
acc << key.to_string()
})
}
pub fn (i PhpIterable) to_dyn_value() !DynValue {
mut temp := i.to_request_owned_zbox()
defer {
temp.release()
}
return DynValue.from_zval(temp.to_zval())
}