1use crate::{PhpKey, PhpValue};
5
6pub fn is_empty(value: &PhpValue) -> bool {
8 match value {
9 PhpValue::Null => true,
10 PhpValue::Bool(b) => !b,
11 PhpValue::Int(i) => *i == 0,
12 PhpValue::Float(f) => *f == 0.0,
13 PhpValue::Str(s) => s.is_empty() || s.as_slice() == b"0",
14 PhpValue::Array(items) => items.is_empty(),
15 _ => false,
16 }
17}
18
19pub fn bytes_truthy(bytes: &[u8]) -> bool {
21 !matches!(bytes, b"" | b"0")
22}
23
24pub fn get<'a>(items: &'a [(PhpKey, PhpValue)], name: &str) -> Option<&'a PhpValue> {
28 items
29 .iter()
30 .find(|(k, _)| matches!(k, PhpKey::Str(s) if s.as_slice() == name.as_bytes()))
31 .map(|(_, v)| v)
32}
33
34pub fn isset<'a>(items: &'a [(PhpKey, PhpValue)], name: &str) -> Option<&'a PhpValue> {
38 get(items, name).filter(|v| !matches!(v, PhpValue::Null))
39}
40
41pub fn set(items: &mut Vec<(PhpKey, PhpValue)>, name: &str, value: PhpValue) {
46 let key = PhpKey::Str(name.as_bytes().to_vec());
47 match items.iter_mut().find(|(k, _)| *k == key) {
48 Some(slot) => slot.1 = value,
49 None => items.push((key, value)),
50 }
51}
52
53pub fn unset(items: &mut Vec<(PhpKey, PhpValue)>, name: &str) {
57 items.retain(|(k, _)| !matches!(k, PhpKey::Str(s) if s.as_slice() == name.as_bytes()));
58}
59
60pub fn array_merge(
63 base: Vec<(PhpKey, PhpValue)>,
64 over: Vec<(PhpKey, PhpValue)>,
65) -> Vec<(PhpKey, PhpValue)> {
66 let mut out: Vec<(PhpKey, PhpValue)> = Vec::with_capacity(base.len() + over.len());
67 let mut next = 0;
68 for (key, value) in base.into_iter().chain(over) {
69 match key {
70 PhpKey::Int(_) => {
71 out.push((PhpKey::Int(next), value));
72 next += 1;
73 }
74 PhpKey::Str(_) => match out.iter_mut().find(|(k, _)| *k == key) {
75 Some(slot) => slot.1 = value,
76 None => out.push((key, value)),
77 },
78 }
79 }
80 out
81}
82
83pub fn is_integer_key(bytes: &[u8]) -> bool {
85 let digits = bytes.strip_prefix(b"-").unwrap_or(bytes);
86 let canonical = match digits {
87 [b'0'] => bytes.len() == 1,
88 [b'1'..=b'9', rest @ ..] => rest.iter().all(u8::is_ascii_digit),
89 _ => false,
90 };
91 canonical
92 && std::str::from_utf8(bytes)
93 .ok()
94 .and_then(|s| s.parse::<i64>().ok())
95 .is_some()
96}
97
98pub fn to_array_key(value: &PhpValue) -> Option<PhpKey> {
103 match value {
104 PhpValue::Null => Some(PhpKey::Str(Vec::new())),
105 PhpValue::Bool(b) => Some(PhpKey::Int(i64::from(*b))),
106 PhpValue::Int(i) => Some(PhpKey::Int(*i)),
107 PhpValue::Float(f) if !f.is_finite() => Some(PhpKey::Int(0)),
108 PhpValue::Float(f) => {
109 let wrapped = f.trunc() % 18_446_744_073_709_551_616.0;
111 Some(PhpKey::Int(wrapped as i128 as i64))
112 }
113 PhpValue::Str(s) if is_integer_key(s) => std::str::from_utf8(s)
114 .ok()
115 .and_then(|s| s.parse().ok())
116 .map(PhpKey::Int),
117 PhpValue::Str(s) => Some(PhpKey::Str(s.clone())),
118 _ => None,
119 }
120}
121
122pub fn key_to_string(key: &PhpKey) -> String {
124 match key {
125 PhpKey::Int(i) => i.to_string(),
126 PhpKey::Str(s) => String::from_utf8_lossy(s).into_owned(),
127 }
128}
129
130pub fn to_array(value: PhpValue) -> Vec<(PhpKey, PhpValue)> {
134 match value {
135 PhpValue::Null => Vec::new(),
136 PhpValue::Array(items) => items,
137 PhpValue::Object { properties, .. } => properties
139 .into_iter()
140 .map(|(key, value)| match key {
141 PhpKey::Str(name) if is_integer_key(&name) => {
142 let int = std::str::from_utf8(&name)
143 .ok()
144 .and_then(|s| s.parse().ok())
145 .expect("an integer key parses");
146 (PhpKey::Int(int), value)
147 }
148 key => (key, value),
149 })
150 .collect(),
151 PhpValue::Enum { case, .. } => vec![(
152 PhpKey::Str(b"name".to_vec()),
153 PhpValue::Str(case.into_bytes()),
154 )],
155 other => vec![(PhpKey::Int(0), other)],
156 }
157}
158
159pub fn to_php_string(value: &PhpValue) -> Vec<u8> {
162 match value {
163 PhpValue::Null | PhpValue::Bool(false) => Vec::new(),
164 PhpValue::Bool(true) => b"1".to_vec(),
165 PhpValue::Int(i) => i.to_string().into_bytes(),
166 PhpValue::Float(f) => float_to_string(*f).into_bytes(),
167 PhpValue::Str(s) => s.clone(),
168 PhpValue::Array(_) => b"Array".to_vec(),
169 PhpValue::Object { class, .. }
170 | PhpValue::Custom { class, .. }
171 | PhpValue::Enum { class, .. } => class.clone().into_bytes(),
172 PhpValue::Ref(_) | PhpValue::ObjectRef(_) => Vec::new(),
173 }
174}
175
176pub fn to_php_int(value: &PhpValue) -> i64 {
185 match value {
186 PhpValue::Null | PhpValue::Bool(false) => 0,
187 PhpValue::Bool(true) => 1,
188 PhpValue::Int(i) => *i,
189 PhpValue::Float(f) => float_to_int(*f),
190 PhpValue::Str(s) => string_to_int(s),
191 PhpValue::Array(items) => i64::from(!items.is_empty()),
192 PhpValue::Object { .. }
193 | PhpValue::Custom { .. }
194 | PhpValue::Enum { .. }
195 | PhpValue::ObjectRef(_) => 1,
196 PhpValue::Ref(_) => 0,
197 }
198}
199
200fn float_to_int(f: f64) -> i64 {
202 const TWO_63: f64 = 9_223_372_036_854_775_808.0;
203 if !f.is_finite() {
204 return 0;
205 }
206 if (-TWO_63..TWO_63).contains(&f) {
207 return f as i64;
208 }
209 const TWO_64: f64 = 18_446_744_073_709_551_616.0;
210 let mut m = f.trunc() % TWO_64;
211 if m < 0.0 {
212 m += TWO_64;
213 }
214 (m as u64) as i64
216}
217
218fn string_to_int(bytes: &[u8]) -> i64 {
220 let ws = |b: &u8| matches!(b, b' ' | b'\t' | b'\n' | b'\r' | b'\x0b' | b'\x0c');
221 let start = bytes.iter().position(|b| !ws(b)).unwrap_or(bytes.len());
222 let s = &bytes[start..];
223 let mut i = 0;
224 if matches!(s.first(), Some(b'+' | b'-')) {
225 i += 1;
226 }
227 let int_digits = s[i..].iter().take_while(|b| b.is_ascii_digit()).count();
228 i += int_digits;
229 let mut is_float = false;
230 let mut frac_digits = 0;
231 if s.get(i) == Some(&b'.') {
232 frac_digits = s[i + 1..].iter().take_while(|b| b.is_ascii_digit()).count();
233 if int_digits + frac_digits > 0 {
234 is_float = true;
235 i += 1 + frac_digits;
236 }
237 }
238 if int_digits + frac_digits == 0 {
239 return 0;
240 }
241 if matches!(s.get(i), Some(b'e' | b'E')) {
242 let mut j = i + 1;
243 if matches!(s.get(j), Some(b'+' | b'-')) {
244 j += 1;
245 }
246 let exp_digits = s[j..].iter().take_while(|b| b.is_ascii_digit()).count();
247 if exp_digits > 0 {
248 is_float = true;
249 i = j + exp_digits;
250 }
251 }
252 let text = std::str::from_utf8(&s[..i]).expect("ASCII");
253 if !is_float {
254 return text.parse::<i64>().unwrap_or(if text.starts_with('-') {
255 i64::MIN
256 } else {
257 i64::MAX
258 });
259 }
260 let f: f64 = text.parse().expect("a float literal");
261 if !f.is_finite() {
262 0
263 } else {
264 f as i64
266 }
267}
268
269fn float_to_string(f: f64) -> String {
271 const PRECISION: i32 = 14;
272 if f.is_nan() {
273 return "NAN".into();
274 }
275 if f.is_infinite() {
276 return if f > 0.0 { "INF".into() } else { "-INF".into() };
277 }
278 if f == 0.0 {
279 return if f.is_sign_negative() {
280 "-0".into()
281 } else {
282 "0".into()
283 };
284 }
285 let sci = format!("{:.*e}", (PRECISION - 1) as usize, f.abs());
286 let (mantissa, exp) = sci.split_once('e').expect("{:e} has an exponent");
287 let exp: i32 = exp.parse().expect("an integer exponent");
288 let digits: String = mantissa.chars().filter(char::is_ascii_digit).collect();
289 let digits = digits.trim_end_matches('0');
290 let digits = if digits.is_empty() { "0" } else { digits };
291 let sign = if f < 0.0 { "-" } else { "" };
292 let n = digits.len() as i32;
293 if !(-4..PRECISION).contains(&exp) {
294 let frac = if n > 1 { &digits[1..] } else { "0" };
295 return format!(
296 "{sign}{}.{frac}E{}{}",
297 &digits[..1],
298 if exp < 0 { '-' } else { '+' },
299 exp.abs()
300 );
301 }
302 let body = if exp >= 0 {
303 if n > exp + 1 {
304 format!(
305 "{}.{}",
306 &digits[..(exp + 1) as usize],
307 &digits[(exp + 1) as usize..]
308 )
309 } else {
310 format!("{digits}{}", "0".repeat((exp + 1 - n) as usize))
311 }
312 } else {
313 format!("0.{}{digits}", "0".repeat((-exp - 1) as usize))
314 };
315 format!("{sign}{body}")
316}
317
318#[derive(Clone, Copy)]
320enum Numeric {
321 Int(i64),
322 Float { value: f64, overflow: i8 },
323}
324
325impl Numeric {
326 fn as_f64(self) -> f64 {
327 match self {
328 Numeric::Int(i) => i as f64,
329 Numeric::Float { value, .. } => value,
330 }
331 }
332}
333
334fn numeric(bytes: &[u8]) -> Option<Numeric> {
337 let ws = |b: &u8| matches!(b, b' ' | b'\t' | b'\n' | b'\r' | b'\x0b' | b'\x0c');
338 let start = bytes.iter().position(|b| !ws(b))?;
339 let end = bytes.iter().rposition(|b| !ws(b))? + 1;
340 let s = &bytes[start..end];
341 let mut i = 0;
342 if matches!(s.first(), Some(b'+' | b'-')) {
343 i += 1;
344 }
345 let int_digits = s[i..].iter().take_while(|b| b.is_ascii_digit()).count();
346 i += int_digits;
347 let mut frac_digits = 0;
348 let mut is_float = false;
349 if s.get(i) == Some(&b'.') {
350 is_float = true;
351 i += 1;
352 frac_digits = s[i..].iter().take_while(|b| b.is_ascii_digit()).count();
353 i += frac_digits;
354 }
355 if int_digits + frac_digits == 0 {
356 return None;
357 }
358 if matches!(s.get(i), Some(b'e' | b'E')) {
359 let mut j = i + 1;
360 if matches!(s.get(j), Some(b'+' | b'-')) {
361 j += 1;
362 }
363 let exp_digits = s[j..].iter().take_while(|b| b.is_ascii_digit()).count();
364 if exp_digits > 0 {
365 is_float = true;
366 i = j + exp_digits;
367 }
368 }
369 if i != s.len() {
370 return None;
371 }
372 let text = std::str::from_utf8(s).ok()?;
373 if !is_float {
374 if let Ok(int) = text.parse::<i64>() {
375 return Some(Numeric::Int(int));
376 }
377 let value: f64 = text.parse().ok()?;
378 let overflow = if value < 0.0 { -1 } else { 1 };
379 return Some(Numeric::Float { value, overflow });
380 }
381 let value: f64 = text.parse().ok()?;
382 Some(Numeric::Float { value, overflow: 0 })
383}
384
385fn truthy(value: &PhpValue) -> bool {
387 !is_empty(value)
388}
389
390fn number(value: &PhpValue) -> Option<Numeric> {
392 match value {
393 PhpValue::Int(i) => Some(Numeric::Int(*i)),
394 PhpValue::Float(f) => Some(Numeric::Float {
395 value: *f,
396 overflow: 0,
397 }),
398 _ => None,
399 }
400}
401
402fn numbers_eq(a: Numeric, b: Numeric) -> bool {
403 match (a, b) {
404 (Numeric::Int(x), Numeric::Int(y)) => x == y,
405 _ => a.as_f64() == b.as_f64(),
406 }
407}
408
409fn strings_eq(a: &[u8], b: &[u8]) -> bool {
411 let (Some(x), Some(y)) = (numeric(a), numeric(b)) else {
412 return a == b;
413 };
414 match (x, y) {
415 (Numeric::Int(x), Numeric::Int(y)) => x == y,
416 (
417 Numeric::Float {
418 value: v1,
419 overflow: o1,
420 },
421 Numeric::Float {
422 value: v2,
423 overflow: o2,
424 },
425 ) => {
426 if (o1 != 0 && o1 == o2 && v1 == v2) || (v1 == v2 && !v1.is_finite()) {
428 a == b
429 } else {
430 v1 == v2
431 }
432 }
433 (Numeric::Int(_), Numeric::Float { overflow, .. })
434 | (Numeric::Float { overflow, .. }, Numeric::Int(_))
435 if overflow != 0 =>
436 {
437 false
438 }
439 _ => x.as_f64() == y.as_f64(),
440 }
441}
442
443fn hashes_eq(a: &[(PhpKey, PhpValue)], b: &[(PhpKey, PhpValue)]) -> bool {
445 a.len() == b.len()
446 && a.iter().all(|(key, value)| {
447 b.iter()
448 .find(|(k, _)| k == key)
449 .is_some_and(|(_, other)| loose_eq(value, other))
450 })
451}
452
453pub fn loose_eq(a: &PhpValue, b: &PhpValue) -> bool {
460 use PhpValue::{Array, Bool, Enum, Null, Object, Str};
461 match (a, b) {
462 (Null, Null) => true,
463 (Null, Str(s)) | (Str(s), Null) => s.is_empty(),
464 (Null | Bool(_), _) | (_, Null | Bool(_)) => truthy(a) == truthy(b),
465 (Str(x), Str(y)) => strings_eq(x, y),
466 (Str(s), n) | (n, Str(s)) if number(n).is_some() => {
467 let n = number(n).expect("checked");
468 if matches!(n, Numeric::Float { value, .. } if value.is_nan()) {
470 return false;
471 }
472 match numeric(s) {
474 Some(parsed) => numbers_eq(n, parsed),
475 None => {
476 to_php_string(&match n {
477 Numeric::Int(i) => PhpValue::Int(i),
478 Numeric::Float { value, .. } => PhpValue::Float(value),
479 }) == *s
480 }
481 }
482 }
483 (x, y) if number(x).is_some() && number(y).is_some() => {
484 numbers_eq(number(x).expect("checked"), number(y).expect("checked"))
485 }
486 (Array(x), Array(y)) => hashes_eq(x, y),
487 (
488 Object {
489 class: c1,
490 properties: p1,
491 },
492 Object {
493 class: c2,
494 properties: p2,
495 },
496 ) => c1 == c2 && hashes_eq(p1, p2),
497 (Enum { .. }, Enum { .. }) => a == b,
498 _ => false,
499 }
500}
501
502#[cfg(test)]
503mod tests {
504 use super::*;
505
506 #[test]
507 fn bytes_truthy_is_false_only_for_empty_and_zero() {
508 assert!(!bytes_truthy(b""));
509 assert!(!bytes_truthy(b"0"));
510 assert!(bytes_truthy(b"00"));
511 assert!(bytes_truthy(b"0.0"));
512 assert!(bytes_truthy(b" "));
513 assert!(bytes_truthy(b"a"));
514 }
515
516 fn s(v: &str) -> PhpValue {
517 PhpValue::Str(v.as_bytes().to_vec())
518 }
519
520 fn k(v: &str) -> PhpKey {
521 PhpKey::Str(v.as_bytes().to_vec())
522 }
523
524 #[test]
525 fn empty_follows_php() {
526 for v in [
527 PhpValue::Null,
528 PhpValue::Bool(false),
529 PhpValue::Int(0),
530 PhpValue::Float(0.0),
531 s(""),
532 s("0"),
533 PhpValue::Array(vec![]),
534 ] {
535 assert!(is_empty(&v), "{v:?}");
536 }
537 for v in [
538 PhpValue::Bool(true),
539 PhpValue::Int(-1),
540 s("00"),
541 s(" "),
542 s("a"),
543 PhpValue::Float(0.1),
544 ] {
545 assert!(!is_empty(&v), "{v:?}");
546 }
547 }
548
549 #[test]
550 fn merge_replaces_in_place_and_renumbers_ints() {
551 let base = vec![(k("a"), s("1")), (k("b"), s("2"))];
552 let over = vec![(PhpKey::Int(7), s("x")), (k("b"), s("3")), (k("c"), s("4"))];
553 assert_eq!(
554 array_merge(base, over),
555 vec![
556 (k("a"), s("1")),
557 (k("b"), s("3")),
558 (PhpKey::Int(0), s("x")),
559 (k("c"), s("4"))
560 ]
561 );
562 }
563
564 #[test]
565 fn array_keys_convert_as_php_does() {
566 use PhpValue::{Bool, Float, Int, Null};
567 let cases = [
568 (Null, Some(k(""))),
569 (Bool(false), Some(PhpKey::Int(0))),
570 (Bool(true), Some(PhpKey::Int(1))),
571 (Int(-7), Some(PhpKey::Int(-7))),
572 (Float(1.9), Some(PhpKey::Int(1))),
573 (Float(-2.5), Some(PhpKey::Int(-2))),
574 (Float(f64::NAN), Some(PhpKey::Int(0))),
575 (Float(f64::INFINITY), Some(PhpKey::Int(0))),
576 (Float(f64::NEG_INFINITY), Some(PhpKey::Int(0))),
577 (Float(1e20), Some(PhpKey::Int(7766279631452241920))),
579 (Float(-1e20), Some(PhpKey::Int(-7766279631452241920))),
580 (Float(9223372036854775808.0), Some(PhpKey::Int(i64::MIN))),
581 (Float(1e19), Some(PhpKey::Int(-8446744073709551616))),
582 (Float(18446744073709551616.0), Some(PhpKey::Int(0))),
583 (Float(1.5e300), Some(PhpKey::Int(0))),
584 (s("12"), Some(PhpKey::Int(12))),
585 (s("012"), Some(k("012"))),
586 (s("edit_book"), Some(k("edit_book"))),
587 (PhpValue::Array(vec![]), None),
588 ];
589 for (value, want) in cases {
590 assert_eq!(to_array_key(&value), want, "{value:?}");
591 }
592 assert_eq!(key_to_string(&PhpKey::Int(-3)), "-3");
593 assert_eq!(key_to_string(&k("a")), "a");
594 }
595
596 #[test]
597 fn integer_keys_follow_php() {
598 for yes in ["0", "10", "-5", "9223372036854775807"] {
599 assert!(is_integer_key(yes.as_bytes()), "{yes}");
600 }
601 for no in [
602 "",
603 "-0",
604 "010",
605 "1e3",
606 " 1",
607 "1.5",
608 "9223372036854775808",
609 "color",
610 ] {
611 assert!(!is_integer_key(no.as_bytes()), "{no}");
612 }
613 }
614
615 fn arr(items: Vec<(PhpKey, PhpValue)>) -> PhpValue {
616 PhpValue::Array(items)
617 }
618
619 #[test]
620 fn loose_eq_follows_php8() {
621 use PhpValue::{Bool, Float, Int, Null};
622 let t = Bool(true);
623 let f = Bool(false);
624 let cases: Vec<(PhpValue, PhpValue, bool)> = vec![
625 (t.clone(), s("yes"), true),
626 (Int(1), s("1"), true),
627 (Int(1), s("01"), true),
628 (s("1"), s("01"), true),
629 (s("1e1"), s("10"), true),
630 (s(" 1"), s("1"), true),
631 (s("1 "), s("1"), true),
632 (s("abc"), Int(0), false),
633 (Int(0), s(""), false),
634 (Null, f.clone(), true),
635 (Null, s(""), true),
636 (Null, s("0"), false),
637 (Null, Int(0), true),
638 (s("0"), f.clone(), true),
639 (arr(vec![]), f.clone(), true),
640 (
641 arr(vec![(PhpKey::Int(0), Int(1))]),
642 arr(vec![(PhpKey::Int(0), Int(1))]),
643 true,
644 ),
645 (
646 arr(vec![(k("a"), Int(1)), (k("b"), Int(2))]),
647 arr(vec![(k("b"), Int(2)), (k("a"), s("1"))]),
648 true,
649 ),
650 (arr(vec![(PhpKey::Int(0), Int(1))]), Int(1), false),
651 (Float(1.0), s("1"), true),
652 (s("9223372036854775807"), s("9223372036854775808"), false),
654 (s("1."), s("1"), true),
655 (s(".5"), s("0.5"), true),
656 (s("0x1"), s("1"), false),
657 (Float(1.5), s("1.5 "), true),
658 (Float(f64::INFINITY), s("INF"), true),
659 (s("abc"), s("ABC"), false),
660 (Int(1), Float(1.0), true),
661 (Null, arr(vec![]), true),
662 (t.clone(), arr(vec![]), false),
663 (Int(i64::MAX), s("9223372036854775808"), true),
665 (Int(i64::MIN), s("-9223372036854775809"), true),
666 (Float(f64::NAN), s("NAN"), false),
667 (Float(f64::NAN), s("abc"), false),
668 (Float(f64::NAN), s("1"), false),
669 (Float(f64::NAN), s(""), false),
670 ];
671 for (a, b, want) in cases {
672 assert_eq!(loose_eq(&a, &b), want, "{a:?} == {b:?}");
673 assert_eq!(loose_eq(&b, &a), want, "{b:?} == {a:?}");
674 }
675 }
676
677 #[test]
678 fn to_php_string_follows_php() {
679 use PhpValue::{Bool, Float, Int, Null};
680 let cases: Vec<(PhpValue, &str)> = vec![
681 (Null, ""),
682 (Bool(false), ""),
683 (Bool(true), "1"),
684 (Int(-42), "-42"),
685 (s("x"), "x"),
686 (Float(1.0), "1"),
687 (Float(0.1), "0.1"),
688 (Float(0.1 + 0.2), "0.3"),
689 (Float(1.0 / 3.0), "0.33333333333333"),
690 (Float(1e14), "1.0E+14"),
691 (Float(1e13), "10000000000000"),
692 (Float(123456789012345.0), "1.2345678901234E+14"),
693 (Float(1.5e20), "1.5E+20"),
694 (Float(1e-5), "1.0E-5"),
695 (Float(0.0001), "0.0001"),
696 (Float(-0.0), "-0"),
697 (Float(-1.25), "-1.25"),
698 (Float(2.5e-7), "2.5E-7"),
699 (Float(f64::INFINITY), "INF"),
700 (Float(f64::NEG_INFINITY), "-INF"),
701 (Float(f64::NAN), "NAN"),
702 (arr(vec![]), "Array"),
703 ];
704 for (v, want) in cases {
705 assert_eq!(to_php_string(&v), want.as_bytes(), "{v:?}");
706 }
707 }
708
709 #[test]
710 fn to_array_follows_the_cast() {
711 assert_eq!(to_array(PhpValue::Null), vec![]);
712 assert_eq!(to_array(s("x")), vec![(PhpKey::Int(0), s("x"))]);
713 let items = vec![(k("a"), PhpValue::Int(1))];
714 assert_eq!(to_array(arr(items.clone())), items);
715 let object = PhpValue::Object {
716 class: "stdClass".into(),
717 properties: items.clone(),
718 };
719 assert_eq!(to_array(object), items);
720 let object = PhpValue::Object {
722 class: "stdClass".into(),
723 properties: vec![
724 (k("0"), s("a")),
725 (k("10"), s("b")),
726 (k("010"), s("c")),
727 (k("x"), s("d")),
728 ],
729 };
730 assert_eq!(
731 to_array(object),
732 vec![
733 (PhpKey::Int(0), s("a")),
734 (PhpKey::Int(10), s("b")),
735 (k("010"), s("c")),
736 (k("x"), s("d")),
737 ]
738 );
739 }
740
741 #[test]
742 fn to_php_int_follows_php8() {
743 let cases = [
745 (PhpValue::Null, 0),
746 (PhpValue::Bool(false), 0),
747 (PhpValue::Bool(true), 1),
748 (PhpValue::Int(-7), -7),
749 (PhpValue::Float(1.9), 1),
750 (PhpValue::Float(-1.9), -1),
751 (PhpValue::Float(f64::NAN), 0),
752 (PhpValue::Float(f64::INFINITY), 0),
753 (PhpValue::Float(f64::NEG_INFINITY), 0),
754 (PhpValue::Float(1e30), 5_076_964_154_930_102_272),
755 (PhpValue::Float(-1e30), -5_076_964_154_930_102_272),
756 (PhpValue::Float(1e19), -8_446_744_073_709_551_616),
757 (PhpValue::Float(-9.3e18), 9_146_744_073_709_551_616),
758 (s("12abc"), 12),
759 (s("1e3"), 1000),
760 (s(" 7"), 7),
761 (s("abc"), 0),
762 (s(""), 0),
763 (s(" "), 0),
764 (s(" \n\t-4.7x"), -4),
765 (s("\x0b3"), 3),
766 (s("\x0c3"), 3),
767 (s("+5"), 5),
768 (s("-0"), 0),
769 (s(".5"), 0),
770 (s("5."), 5),
771 (s("1.e2"), 100),
772 (s("-.5e1"), -5),
773 (s("1.5e3"), 1500),
774 (s("1e"), 1),
775 (s("0x1A"), 0),
776 (s("9999999999999999999"), i64::MAX),
777 (s("-9999999999999999999"), i64::MIN),
778 (s("1e30"), i64::MAX),
779 (s("-1e30"), i64::MIN),
780 (s("1e999"), 0),
781 (s("-1e999"), 0),
782 (PhpValue::Array(vec![]), 0),
783 (PhpValue::Array(vec![(PhpKey::Int(0), PhpValue::Int(1))]), 1),
784 (
785 PhpValue::Object {
786 class: "stdClass".into(),
787 properties: vec![],
788 },
789 1,
790 ),
791 ];
792 for (value, int) in cases {
793 assert_eq!(to_php_int(&value), int, "(int) {value:?}");
794 }
795 }
796}