• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

davidcole1340 / ext-php-rs / 16351194682

17 Jul 2025 04:56PM UTC coverage: 22.222% (-0.1%) from 22.325%
16351194682

Pull #520

github

web-flow
Merge d7f9aa479 into 1166e2910
Pull Request #520: Array btreemap conversion

0 of 18 new or added lines in 1 file covered. (0.0%)

1 existing line in 1 file now uncovered.

870 of 3915 relevant lines covered (22.22%)

3.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/types/array.rs
1
//! Represents an array in PHP. As all arrays in PHP are associative arrays,
2
//! they are represented by hash tables.
3

4
use crate::{
5
    boxed::{ZBox, ZBoxable},
6
    convert::{FromZval, IntoZval},
7
    error::{Error, Result},
8
    ffi::zend_ulong,
9
    ffi::{
10
        _zend_new_array, zend_array_count, zend_array_destroy, zend_array_dup, zend_hash_clean,
11
        zend_hash_get_current_data_ex, zend_hash_get_current_key_type_ex,
12
        zend_hash_get_current_key_zval_ex, zend_hash_index_del, zend_hash_index_find,
13
        zend_hash_index_update, zend_hash_move_backwards_ex, zend_hash_move_forward_ex,
14
        zend_hash_next_index_insert, zend_hash_str_del, zend_hash_str_find, zend_hash_str_update,
15
        HashPosition, HT_MIN_SIZE,
16
    },
17
    flags::DataType,
18
    types::Zval,
19
};
20
use std::{
21
    collections::{BTreeMap, HashMap},
22
    convert::{TryFrom, TryInto},
23
    ffi::CString,
24
    fmt::{Debug, Display},
25
    iter::FromIterator,
26
    ptr,
27
    str::FromStr,
28
};
29

30
/// A PHP hashtable.
31
///
32
/// In PHP, arrays are represented as hashtables. This allows you to push values
33
/// onto the end of the array like a vector, while also allowing you to insert
34
/// at arbitrary string key indexes.
35
///
36
/// A PHP hashtable stores values as [`Zval`]s. This allows you to insert
37
/// different types into the same hashtable. Types must implement [`IntoZval`]
38
/// to be able to be inserted into the hashtable.
39
///
40
/// # Examples
41
///
42
/// ```no_run
43
/// use ext_php_rs::types::ZendHashTable;
44
///
45
/// let mut ht = ZendHashTable::new();
46
/// ht.push(1);
47
/// ht.push("Hello, world!");
48
/// ht.insert("Like", "Hashtable");
49
///
50
/// assert_eq!(ht.len(), 3);
51
/// assert_eq!(ht.get_index(0).and_then(|zv| zv.long()), Some(1));
52
/// ```
53
pub type ZendHashTable = crate::ffi::HashTable;
54

55
// Clippy complains about there being no `is_empty` function when implementing
56
// on the alias `ZendStr` :( <https://github.com/rust-lang/rust-clippy/issues/7702>
57
#[allow(clippy::len_without_is_empty)]
58
impl ZendHashTable {
59
    /// Creates a new, empty, PHP hashtable, returned inside a [`ZBox`].
60
    ///
61
    /// # Example
62
    ///
63
    /// ```no_run
64
    /// use ext_php_rs::types::ZendHashTable;
65
    ///
66
    /// let ht = ZendHashTable::new();
67
    /// ```
68
    ///
69
    /// # Panics
70
    ///
71
    /// Panics if memory for the hashtable could not be allocated.
72
    #[must_use]
73
    pub fn new() -> ZBox<Self> {
×
74
        Self::with_capacity(HT_MIN_SIZE)
×
75
    }
76

77
    /// Creates a new, empty, PHP hashtable with an initial size, returned
78
    /// inside a [`ZBox`].
79
    ///
80
    /// # Parameters
81
    ///
82
    /// * `size` - The size to initialize the array with.
83
    ///
84
    /// # Example
85
    ///
86
    /// ```no_run
87
    /// use ext_php_rs::types::ZendHashTable;
88
    ///
89
    /// let ht = ZendHashTable::with_capacity(10);
90
    /// ```
91
    ///
92
    /// # Panics
93
    ///
94
    /// Panics if memory for the hashtable could not be allocated.
95
    #[must_use]
96
    pub fn with_capacity(size: u32) -> ZBox<Self> {
×
97
        unsafe {
98
            // SAFETY: PHP allocator handles the creation of the array.
99
            #[allow(clippy::used_underscore_items)]
100
            let ptr = _zend_new_array(size);
×
101

102
            // SAFETY: `as_mut()` checks if the pointer is null, and panics if it is not.
103
            ZBox::from_raw(
104
                ptr.as_mut()
×
105
                    .expect("Failed to allocate memory for hashtable"),
×
106
            )
107
        }
108
    }
109

110
    /// Returns the current number of elements in the array.
111
    ///
112
    /// # Example
113
    ///
114
    /// ```no_run
115
    /// use ext_php_rs::types::ZendHashTable;
116
    ///
117
    /// let mut ht = ZendHashTable::new();
118
    ///
119
    /// ht.push(1);
120
    /// ht.push("Hello, world");
121
    ///
122
    /// assert_eq!(ht.len(), 2);
123
    /// ```
124
    #[must_use]
125
    pub fn len(&self) -> usize {
×
126
        unsafe { zend_array_count(ptr::from_ref(self).cast_mut()) as usize }
×
127
    }
128

129
    /// Returns whether the hash table is empty.
130
    ///
131
    /// # Example
132
    ///
133
    /// ```no_run
134
    /// use ext_php_rs::types::ZendHashTable;
135
    ///
136
    /// let mut ht = ZendHashTable::new();
137
    ///
138
    /// assert_eq!(ht.is_empty(), true);
139
    ///
140
    /// ht.push(1);
141
    /// ht.push("Hello, world");
142
    ///
143
    /// assert_eq!(ht.is_empty(), false);
144
    /// ```
145
    #[must_use]
146
    pub fn is_empty(&self) -> bool {
×
147
        self.len() == 0
×
148
    }
149

150
    /// Clears the hash table, removing all values.
151
    ///
152
    /// # Example
153
    ///
154
    /// ```no_run
155
    /// use ext_php_rs::types::ZendHashTable;
156
    ///
157
    /// let mut ht = ZendHashTable::new();
158
    ///
159
    /// ht.insert("test", "hello world");
160
    /// assert_eq!(ht.is_empty(), false);
161
    ///
162
    /// ht.clear();
163
    /// assert_eq!(ht.is_empty(), true);
164
    /// ```
165
    pub fn clear(&mut self) {
×
166
        unsafe { zend_hash_clean(self) }
×
167
    }
168

169
    /// Attempts to retrieve a value from the hash table with a string key.
170
    ///
171
    /// # Parameters
172
    ///
173
    /// * `key` - The key to search for in the hash table.
174
    ///
175
    /// # Returns
176
    ///
177
    /// * `Some(&Zval)` - A reference to the zval at the position in the hash
178
    ///   table.
179
    /// * `None` - No value at the given position was found.
180
    ///
181
    /// # Example
182
    ///
183
    /// ```no_run
184
    /// use ext_php_rs::types::ZendHashTable;
185
    ///
186
    /// let mut ht = ZendHashTable::new();
187
    ///
188
    /// ht.insert("test", "hello world");
189
    /// assert_eq!(ht.get("test").and_then(|zv| zv.str()), Some("hello world"));
190
    /// ```
191
    #[must_use]
192
    pub fn get<'a, K>(&self, key: K) -> Option<&Zval>
×
193
    where
194
        K: Into<ArrayKey<'a>>,
195
    {
196
        match key.into() {
×
197
            ArrayKey::Long(index) => unsafe {
198
                #[allow(clippy::cast_sign_loss)]
199
                zend_hash_index_find(self, index as zend_ulong).as_ref()
×
200
            },
201
            ArrayKey::String(key) => {
×
202
                if let Ok(index) = i64::from_str(key.as_str()) {
×
203
                    #[allow(clippy::cast_sign_loss)]
×
204
                    unsafe {
205
                        zend_hash_index_find(self, index as zend_ulong).as_ref()
×
206
                    }
207
                } else {
208
                    unsafe {
209
                        zend_hash_str_find(
210
                            self,
×
211
                            CString::new(key.as_str()).ok()?.as_ptr(),
×
212
                            key.len() as _,
×
213
                        )
214
                        .as_ref()
215
                    }
216
                }
217
            }
218
            ArrayKey::Str(key) => {
×
219
                if let Ok(index) = i64::from_str(key) {
×
220
                    #[allow(clippy::cast_sign_loss)]
×
221
                    unsafe {
222
                        zend_hash_index_find(self, index as zend_ulong).as_ref()
×
223
                    }
224
                } else {
225
                    unsafe {
226
                        zend_hash_str_find(self, CString::new(key).ok()?.as_ptr(), key.len() as _)
×
227
                            .as_ref()
228
                    }
229
                }
230
            }
231
        }
232
    }
233

234
    /// Attempts to retrieve a value from the hash table with a string key.
235
    ///
236
    /// # Parameters
237
    ///
238
    /// * `key` - The key to search for in the hash table.
239
    ///
240
    /// # Returns
241
    ///
242
    /// * `Some(&Zval)` - A reference to the zval at the position in the hash
243
    ///   table.
244
    /// * `None` - No value at the given position was found.
245
    ///
246
    /// # Example
247
    ///
248
    /// ```no_run
249
    /// use ext_php_rs::types::ZendHashTable;
250
    ///
251
    /// let mut ht = ZendHashTable::new();
252
    ///
253
    /// ht.insert("test", "hello world");
254
    /// assert_eq!(ht.get("test").and_then(|zv| zv.str()), Some("hello world"));
255
    /// ```
256
    // TODO: Verify if this is safe to use, as it allows mutating the
257
    // hashtable while only having a reference to it. #461
258
    #[allow(clippy::mut_from_ref)]
259
    #[must_use]
260
    pub fn get_mut<'a, K>(&self, key: K) -> Option<&mut Zval>
×
261
    where
262
        K: Into<ArrayKey<'a>>,
263
    {
264
        match key.into() {
×
265
            ArrayKey::Long(index) => unsafe {
266
                #[allow(clippy::cast_sign_loss)]
267
                zend_hash_index_find(self, index as zend_ulong).as_mut()
×
268
            },
269
            ArrayKey::String(key) => {
×
270
                if let Ok(index) = i64::from_str(key.as_str()) {
×
271
                    #[allow(clippy::cast_sign_loss)]
×
272
                    unsafe {
273
                        zend_hash_index_find(self, index as zend_ulong).as_mut()
×
274
                    }
275
                } else {
276
                    unsafe {
277
                        zend_hash_str_find(
278
                            self,
×
279
                            CString::new(key.as_str()).ok()?.as_ptr(),
×
280
                            key.len() as _,
×
281
                        )
282
                        .as_mut()
283
                    }
284
                }
285
            }
286
            ArrayKey::Str(key) => {
×
287
                if let Ok(index) = i64::from_str(key) {
×
288
                    #[allow(clippy::cast_sign_loss)]
×
289
                    unsafe {
290
                        zend_hash_index_find(self, index as zend_ulong).as_mut()
×
291
                    }
292
                } else {
293
                    unsafe {
294
                        zend_hash_str_find(self, CString::new(key).ok()?.as_ptr(), key.len() as _)
×
295
                            .as_mut()
296
                    }
297
                }
298
            }
299
        }
300
    }
301

302
    /// Attempts to retrieve a value from the hash table with an index.
303
    ///
304
    /// # Parameters
305
    ///
306
    /// * `key` - The key to search for in the hash table.
307
    ///
308
    /// # Returns
309
    ///
310
    /// * `Some(&Zval)` - A reference to the zval at the position in the hash
311
    ///   table.
312
    /// * `None` - No value at the given position was found.
313
    ///
314
    /// # Example
315
    ///
316
    /// ```no_run
317
    /// use ext_php_rs::types::ZendHashTable;
318
    ///
319
    /// let mut ht = ZendHashTable::new();
320
    ///
321
    /// ht.push(100);
322
    /// assert_eq!(ht.get_index(0).and_then(|zv| zv.long()), Some(100));
323
    /// ```
324
    #[must_use]
325
    pub fn get_index(&self, key: i64) -> Option<&Zval> {
×
326
        #[allow(clippy::cast_sign_loss)]
327
        unsafe {
328
            zend_hash_index_find(self, key as zend_ulong).as_ref()
×
329
        }
330
    }
331

332
    /// Attempts to retrieve a value from the hash table with an index.
333
    ///
334
    /// # Parameters
335
    ///
336
    /// * `key` - The key to search for in the hash table.
337
    ///
338
    /// # Returns
339
    ///
340
    /// * `Some(&Zval)` - A reference to the zval at the position in the hash
341
    ///   table.
342
    /// * `None` - No value at the given position was found.
343
    ///
344
    /// # Example
345
    ///
346
    /// ```no_run
347
    /// use ext_php_rs::types::ZendHashTable;
348
    ///
349
    /// let mut ht = ZendHashTable::new();
350
    ///
351
    /// ht.push(100);
352
    /// assert_eq!(ht.get_index(0).and_then(|zv| zv.long()), Some(100));
353
    /// ```
354
    // TODO: Verify if this is safe to use, as it allows mutating the
355
    // hashtable while only having a reference to it. #461
356
    #[allow(clippy::mut_from_ref)]
357
    #[must_use]
358
    pub fn get_index_mut(&self, key: i64) -> Option<&mut Zval> {
×
359
        unsafe {
360
            #[allow(clippy::cast_sign_loss)]
361
            zend_hash_index_find(self, key as zend_ulong).as_mut()
×
362
        }
363
    }
364

365
    /// Attempts to remove a value from the hash table with a string key.
366
    ///
367
    /// # Parameters
368
    ///
369
    /// * `key` - The key to remove from the hash table.
370
    ///
371
    /// # Returns
372
    ///
373
    /// * `Some(())` - Key was successfully removed.
374
    /// * `None` - No key was removed, did not exist.
375
    ///
376
    /// # Example
377
    ///
378
    /// ```no_run
379
    /// use ext_php_rs::types::ZendHashTable;
380
    ///
381
    /// let mut ht = ZendHashTable::new();
382
    ///
383
    /// ht.insert("test", "hello world");
384
    /// assert_eq!(ht.len(), 1);
385
    ///
386
    /// ht.remove("test");
387
    /// assert_eq!(ht.len(), 0);
388
    /// ```
389
    pub fn remove<'a, K>(&mut self, key: K) -> Option<()>
×
390
    where
391
        K: Into<ArrayKey<'a>>,
392
    {
393
        let result = match key.into() {
×
394
            ArrayKey::Long(index) => unsafe {
395
                #[allow(clippy::cast_sign_loss)]
396
                zend_hash_index_del(self, index as zend_ulong)
×
397
            },
398
            ArrayKey::String(key) => {
×
399
                if let Ok(index) = i64::from_str(key.as_str()) {
×
400
                    #[allow(clippy::cast_sign_loss)]
×
401
                    unsafe {
402
                        zend_hash_index_del(self, index as zend_ulong)
×
403
                    }
404
                } else {
405
                    unsafe {
406
                        zend_hash_str_del(
407
                            self,
×
408
                            CString::new(key.as_str()).ok()?.as_ptr(),
×
409
                            key.len() as _,
×
410
                        )
411
                    }
412
                }
413
            }
414
            ArrayKey::Str(key) => {
×
415
                if let Ok(index) = i64::from_str(key) {
×
416
                    #[allow(clippy::cast_sign_loss)]
×
417
                    unsafe {
418
                        zend_hash_index_del(self, index as zend_ulong)
×
419
                    }
420
                } else {
421
                    unsafe {
422
                        zend_hash_str_del(self, CString::new(key).ok()?.as_ptr(), key.len() as _)
×
423
                    }
424
                }
425
            }
426
        };
427

428
        if result < 0 {
×
429
            None
×
430
        } else {
431
            Some(())
×
432
        }
433
    }
434

435
    /// Attempts to remove a value from the hash table with a string key.
436
    ///
437
    /// # Parameters
438
    ///
439
    /// * `key` - The key to remove from the hash table.
440
    ///
441
    /// # Returns
442
    ///
443
    /// * `Ok(())` - Key was successfully removed.
444
    /// * `None` - No key was removed, did not exist.
445
    ///
446
    /// # Example
447
    ///
448
    /// ```no_run
449
    /// use ext_php_rs::types::ZendHashTable;
450
    ///
451
    /// let mut ht = ZendHashTable::new();
452
    ///
453
    /// ht.push("hello");
454
    /// assert_eq!(ht.len(), 1);
455
    ///
456
    /// ht.remove_index(0);
457
    /// assert_eq!(ht.len(), 0);
458
    /// ```
459
    pub fn remove_index(&mut self, key: i64) -> Option<()> {
×
460
        let result = unsafe {
461
            #[allow(clippy::cast_sign_loss)]
462
            zend_hash_index_del(self, key as zend_ulong)
×
463
        };
464

465
        if result < 0 {
×
466
            None
×
467
        } else {
468
            Some(())
×
469
        }
470
    }
471

472
    /// Attempts to insert an item into the hash table, or update if the key
473
    /// already exists. Returns nothing in a result if successful.
474
    ///
475
    /// # Parameters
476
    ///
477
    /// * `key` - The key to insert the value at in the hash table.
478
    /// * `value` - The value to insert into the hash table.
479
    ///
480
    /// # Returns
481
    ///
482
    /// Returns nothing in a result on success.
483
    ///
484
    /// # Errors
485
    ///
486
    /// Returns an error if the key could not be converted into a [`CString`],
487
    /// or converting the value into a [`Zval`] failed.
488
    ///
489
    /// # Example
490
    ///
491
    /// ```no_run
492
    /// use ext_php_rs::types::ZendHashTable;
493
    ///
494
    /// let mut ht = ZendHashTable::new();
495
    ///
496
    /// ht.insert("a", "A");
497
    /// ht.insert("b", "B");
498
    /// ht.insert("c", "C");
499
    /// assert_eq!(ht.len(), 3);
500
    /// ```
501
    pub fn insert<'a, K, V>(&mut self, key: K, val: V) -> Result<()>
×
502
    where
503
        K: Into<ArrayKey<'a>>,
504
        V: IntoZval,
505
    {
506
        let mut val = val.into_zval(false)?;
×
507
        match key.into() {
×
508
            ArrayKey::Long(index) => {
×
509
                unsafe {
510
                    #[allow(clippy::cast_sign_loss)]
511
                    zend_hash_index_update(self, index as zend_ulong, &raw mut val)
×
512
                };
513
            }
514
            ArrayKey::String(key) => {
×
515
                if let Ok(index) = i64::from_str(&key) {
×
516
                    unsafe {
517
                        #[allow(clippy::cast_sign_loss)]
518
                        zend_hash_index_update(self, index as zend_ulong, &raw mut val)
×
519
                    };
520
                } else {
521
                    unsafe {
522
                        zend_hash_str_update(
523
                            self,
×
524
                            CString::new(key.as_str())?.as_ptr(),
×
525
                            key.len(),
×
526
                            &raw mut val,
×
527
                        )
528
                    };
529
                }
530
            }
531
            ArrayKey::Str(key) => {
×
532
                if let Ok(index) = i64::from_str(key) {
×
533
                    unsafe {
534
                        #[allow(clippy::cast_sign_loss)]
535
                        zend_hash_index_update(self, index as zend_ulong, &raw mut val)
×
536
                    };
537
                } else {
538
                    unsafe {
539
                        zend_hash_str_update(
540
                            self,
×
541
                            CString::new(key)?.as_ptr(),
×
542
                            key.len(),
×
543
                            &raw mut val,
×
544
                        )
545
                    };
546
                }
547
            }
548
        }
549
        val.release();
×
550
        Ok(())
×
551
    }
552

553
    /// Inserts an item into the hash table at a specified index, or updates if
554
    /// the key already exists. Returns nothing in a result if successful.
555
    ///
556
    /// # Parameters
557
    ///
558
    /// * `key` - The index at which the value should be inserted.
559
    /// * `val` - The value to insert into the hash table.
560
    ///
561
    /// # Returns
562
    ///
563
    /// Returns nothing in a result on success.
564
    ///
565
    /// # Errors
566
    ///
567
    /// Returns an error if converting the value into a [`Zval`] failed.
568
    ///
569
    /// # Example
570
    ///
571
    /// ```no_run
572
    /// use ext_php_rs::types::ZendHashTable;
573
    ///
574
    /// let mut ht = ZendHashTable::new();
575
    ///
576
    /// ht.insert_at_index(0, "A");
577
    /// ht.insert_at_index(5, "B");
578
    /// ht.insert_at_index(0, "C"); // notice overriding index 0
579
    /// assert_eq!(ht.len(), 2);
580
    /// ```
581
    pub fn insert_at_index<V>(&mut self, key: i64, val: V) -> Result<()>
×
582
    where
583
        V: IntoZval,
584
    {
585
        let mut val = val.into_zval(false)?;
×
586
        unsafe {
587
            #[allow(clippy::cast_sign_loss)]
588
            zend_hash_index_update(self, key as zend_ulong, &raw mut val)
×
589
        };
590
        val.release();
×
591
        Ok(())
×
592
    }
593

594
    /// Pushes an item onto the end of the hash table. Returns a result
595
    /// containing nothing if the element was successfully inserted.
596
    ///
597
    /// # Parameters
598
    ///
599
    /// * `val` - The value to insert into the hash table.
600
    ///
601
    /// # Returns
602
    ///
603
    /// Returns nothing in a result on success.
604
    ///
605
    /// # Errors
606
    ///
607
    /// Returns an error if converting the value into a [`Zval`] failed.
608
    ///
609
    /// # Example
610
    ///
611
    /// ```no_run
612
    /// use ext_php_rs::types::ZendHashTable;
613
    ///
614
    /// let mut ht = ZendHashTable::new();
615
    ///
616
    /// ht.push("a");
617
    /// ht.push("b");
618
    /// ht.push("c");
619
    /// assert_eq!(ht.len(), 3);
620
    /// ```
621
    pub fn push<V>(&mut self, val: V) -> Result<()>
×
622
    where
623
        V: IntoZval,
624
    {
625
        let mut val = val.into_zval(false)?;
×
626
        unsafe { zend_hash_next_index_insert(self, &raw mut val) };
×
627
        val.release();
×
628

629
        Ok(())
×
630
    }
631

632
    /// Checks if the hashtable only contains numerical keys.
633
    ///
634
    /// # Returns
635
    ///
636
    /// True if all keys on the hashtable are numerical.
637
    ///
638
    /// # Example
639
    ///
640
    /// ```no_run
641
    /// use ext_php_rs::types::ZendHashTable;
642
    ///
643
    /// let mut ht = ZendHashTable::new();
644
    ///
645
    /// ht.push(0);
646
    /// ht.push(3);
647
    /// ht.push(9);
648
    /// assert!(ht.has_numerical_keys());
649
    ///
650
    /// ht.insert("obviously not numerical", 10);
651
    /// assert!(!ht.has_numerical_keys());
652
    /// ```
653
    #[must_use]
654
    pub fn has_numerical_keys(&self) -> bool {
×
655
        !self.into_iter().any(|(k, _)| !k.is_long())
×
656
    }
657

658
    /// Checks if the hashtable has numerical, sequential keys.
659
    ///
660
    /// # Returns
661
    ///
662
    /// True if all keys on the hashtable are numerical and are in sequential
663
    /// order (i.e. starting at 0 and not skipping any keys).
664
    ///
665
    /// # Panics
666
    ///
667
    /// Panics if the number of elements in the hashtable exceeds `i64::MAX`.
668
    ///
669
    /// # Example
670
    ///
671
    /// ```no_run
672
    /// use ext_php_rs::types::ZendHashTable;
673
    ///
674
    /// let mut ht = ZendHashTable::new();
675
    ///
676
    /// ht.push(0);
677
    /// ht.push(3);
678
    /// ht.push(9);
679
    /// assert!(ht.has_sequential_keys());
680
    ///
681
    /// ht.insert_at_index(90, 10);
682
    /// assert!(!ht.has_sequential_keys());
683
    /// ```
684
    #[must_use]
685
    pub fn has_sequential_keys(&self) -> bool {
×
686
        !self
×
687
            .into_iter()
×
688
            .enumerate()
×
689
            .any(|(i, (k, _))| ArrayKey::Long(i64::try_from(i).expect("Integer overflow")) != k)
×
690
    }
691

692
    /// Returns an iterator over the values contained inside the hashtable, as
693
    /// if it was a set or list.
694
    ///
695
    /// # Example
696
    ///
697
    /// ```no_run
698
    /// use ext_php_rs::types::ZendHashTable;
699
    ///
700
    /// let mut ht = ZendHashTable::new();
701
    ///
702
    /// for val in ht.values() {
703
    ///     dbg!(val);
704
    /// }
705
    #[inline]
706
    #[must_use]
707
    pub fn values(&self) -> Values<'_> {
×
708
        Values::new(self)
×
709
    }
710

711
    /// Returns an iterator over the key(s) and value contained inside the
712
    /// hashtable.
713
    ///
714
    /// # Example
715
    ///
716
    /// ```no_run
717
    /// use ext_php_rs::types::{ZendHashTable, ArrayKey};
718
    ///
719
    /// let mut ht = ZendHashTable::new();
720
    ///
721
    /// for (key, val) in ht.iter() {
722
    ///     match &key {
723
    ///         ArrayKey::Long(index) => {
724
    ///         }
725
    ///         ArrayKey::String(key) => {
726
    ///         }
727
    ///         ArrayKey::Str(key) => {
728
    ///         }
729
    ///     }
730
    ///     dbg!(key, val);
731
    /// }
732
    #[inline]
733
    #[must_use]
734
    pub fn iter(&self) -> Iter<'_> {
×
735
        self.into_iter()
×
736
    }
737
}
738

739
unsafe impl ZBoxable for ZendHashTable {
740
    fn free(&mut self) {
×
741
        // SAFETY: ZBox has immutable access to `self`.
742
        unsafe { zend_array_destroy(self) }
×
743
    }
744
}
745

746
impl Debug for ZendHashTable {
747
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
×
748
        f.debug_map()
×
749
            .entries(self.into_iter().map(|(k, v)| (k.to_string(), v)))
×
750
            .finish()
751
    }
752
}
753

754
impl ToOwned for ZendHashTable {
755
    type Owned = ZBox<ZendHashTable>;
756

757
    fn to_owned(&self) -> Self::Owned {
×
758
        unsafe {
759
            // SAFETY: FFI call does not modify `self`, returns a new hashtable.
760
            let ptr = zend_array_dup(ptr::from_ref(self).cast_mut());
×
761

762
            // SAFETY: `as_mut()` checks if the pointer is null, and panics if it is not.
763
            ZBox::from_raw(
764
                ptr.as_mut()
×
765
                    .expect("Failed to allocate memory for hashtable"),
×
766
            )
767
        }
768
    }
769
}
770

771
/// Immutable iterator upon a reference to a hashtable.
772
pub struct Iter<'a> {
773
    ht: &'a ZendHashTable,
774
    current_num: i64,
775
    end_num: i64,
776
    pos: HashPosition,
777
    end_pos: HashPosition,
778
}
779

780
/// Represents the key of a PHP array, which can be either a long or a string.
781
#[derive(Debug, Clone, PartialEq)]
782
pub enum ArrayKey<'a> {
783
    /// A numerical key.
784
    /// In Zend API it's represented by `u64` (`zend_ulong`), so the value needs
785
    /// to be cast to `zend_ulong` before passing into Zend functions.
786
    Long(i64),
787
    /// A string key.
788
    String(String),
789
    /// A string key by reference.
790
    Str(&'a str),
791
}
792

793
impl From<String> for ArrayKey<'_> {
794
    fn from(value: String) -> Self {
×
795
        Self::String(value)
×
796
    }
797
}
798

799
impl ArrayKey<'_> {
800
    /// Check if the key is an integer.
801
    ///
802
    /// # Returns
803
    ///
804
    /// Returns true if the key is an integer, false otherwise.
805
    #[must_use]
806
    pub fn is_long(&self) -> bool {
×
807
        match self {
×
808
            ArrayKey::Long(_) => true,
×
809
            ArrayKey::String(_) | ArrayKey::Str(_) => false,
×
810
        }
811
    }
812
}
813

814
impl Display for ArrayKey<'_> {
815
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
×
816
        match self {
×
817
            ArrayKey::Long(key) => write!(f, "{key}"),
×
818
            ArrayKey::String(key) => write!(f, "{key}"),
×
819
            ArrayKey::Str(key) => write!(f, "{key}"),
×
820
        }
821
    }
822
}
823

824
impl<'a> From<&'a str> for ArrayKey<'a> {
825
    fn from(key: &'a str) -> ArrayKey<'a> {
×
826
        ArrayKey::Str(key)
×
827
    }
828
}
829

830
impl<'a> From<i64> for ArrayKey<'a> {
831
    fn from(index: i64) -> ArrayKey<'a> {
×
832
        ArrayKey::Long(index)
×
833
    }
834
}
835

836
impl<'a> FromZval<'a> for ArrayKey<'_> {
837
    const TYPE: DataType = DataType::String;
838

839
    fn from_zval(zval: &'a Zval) -> Option<Self> {
×
840
        if let Some(key) = zval.long() {
×
841
            return Some(ArrayKey::Long(key));
×
842
        }
843
        if let Some(key) = zval.string() {
×
844
            return Some(ArrayKey::String(key));
×
845
        }
846
        None
×
847
    }
848
}
849

850
impl<'a> Iter<'a> {
851
    /// Creates a new iterator over a hashtable.
852
    ///
853
    /// # Parameters
854
    ///
855
    /// * `ht` - The hashtable to iterate.
856
    pub fn new(ht: &'a ZendHashTable) -> Self {
×
857
        let end_num: i64 = ht
×
858
            .len()
859
            .try_into()
860
            .expect("Integer overflow in hashtable length");
861
        let end_pos = if ht.nNumOfElements > 0 {
×
862
            ht.nNumOfElements - 1
×
863
        } else {
864
            0
×
865
        };
866

867
        Self {
868
            ht,
869
            current_num: 0,
870
            end_num,
871
            pos: 0,
872
            end_pos,
873
        }
874
    }
875
}
876

877
impl<'a> IntoIterator for &'a ZendHashTable {
878
    type Item = (ArrayKey<'a>, &'a Zval);
879
    type IntoIter = Iter<'a>;
880

881
    /// Returns an iterator over the key(s) and value contained inside the
882
    /// hashtable.
883
    ///
884
    /// # Example
885
    ///
886
    /// ```no_run
887
    /// use ext_php_rs::types::ZendHashTable;
888
    ///
889
    /// let mut ht = ZendHashTable::new();
890
    ///
891
    /// for (key, val) in ht.iter() {
892
    /// //   ^ Index if inserted at an index.
893
    /// //        ^ Optional string key, if inserted like a hashtable.
894
    /// //             ^ Inserted value.
895
    ///
896
    ///     dbg!(key, val);
897
    /// }
898
    #[inline]
899
    fn into_iter(self) -> Self::IntoIter {
×
900
        Iter::new(self)
×
901
    }
902
}
903

904
impl<'a> Iterator for Iter<'a> {
905
    type Item = (ArrayKey<'a>, &'a Zval);
906

907
    fn next(&mut self) -> Option<Self::Item> {
×
908
        self.next_zval()
×
909
            .map(|(k, v)| (ArrayKey::from_zval(&k).expect("Invalid array key!"), v))
×
910
    }
911

912
    fn count(self) -> usize
×
913
    where
914
        Self: Sized,
915
    {
916
        self.ht.len()
×
917
    }
918
}
919

920
impl ExactSizeIterator for Iter<'_> {
921
    fn len(&self) -> usize {
×
922
        self.ht.len()
×
923
    }
924
}
925

926
impl DoubleEndedIterator for Iter<'_> {
927
    fn next_back(&mut self) -> Option<Self::Item> {
×
928
        if self.end_num <= self.current_num {
×
929
            return None;
×
930
        }
931

932
        let key_type = unsafe {
933
            zend_hash_get_current_key_type_ex(ptr::from_ref(self.ht).cast_mut(), &raw mut self.pos)
×
934
        };
935

936
        if key_type == -1 {
×
937
            return None;
×
938
        }
939

940
        let key = Zval::new();
×
941

942
        unsafe {
943
            zend_hash_get_current_key_zval_ex(
944
                ptr::from_ref(self.ht).cast_mut(),
×
945
                (&raw const key).cast_mut(),
×
946
                &raw mut self.end_pos,
×
947
            );
948
        }
949
        let value = unsafe {
950
            &*zend_hash_get_current_data_ex(
×
951
                ptr::from_ref(self.ht).cast_mut(),
×
952
                &raw mut self.end_pos,
×
953
            )
954
        };
955

956
        let key = match ArrayKey::from_zval(&key) {
×
957
            Some(key) => key,
×
958
            None => ArrayKey::Long(self.end_num),
×
959
        };
960

961
        unsafe {
962
            zend_hash_move_backwards_ex(ptr::from_ref(self.ht).cast_mut(), &raw mut self.end_pos)
×
963
        };
964
        self.end_num -= 1;
×
965

966
        Some((key, value))
×
967
    }
968
}
969

970
impl<'a> Iter<'a> {
971
    pub fn next_zval(&mut self) -> Option<(Zval, &'a Zval)> {
×
972
        if self.current_num >= self.end_num {
×
973
            return None;
×
974
        }
975

976
        let key_type = unsafe {
977
            zend_hash_get_current_key_type_ex(ptr::from_ref(self.ht).cast_mut(), &raw mut self.pos)
×
978
        };
979

980
        // Key type `-1` is ???
981
        // Key type `1` is string
982
        // Key type `2` is long
983
        // Key type `3` is null meaning the end of the array
984
        if key_type == -1 || key_type == 3 {
×
985
            return None;
×
986
        }
987

988
        let mut key = Zval::new();
×
989

990
        unsafe {
991
            zend_hash_get_current_key_zval_ex(
992
                ptr::from_ref(self.ht).cast_mut(),
×
993
                (&raw const key).cast_mut(),
×
994
                &raw mut self.pos,
×
995
            );
996
        }
997
        let value = unsafe {
998
            let val_ptr =
×
999
                zend_hash_get_current_data_ex(ptr::from_ref(self.ht).cast_mut(), &raw mut self.pos);
×
1000

1001
            if val_ptr.is_null() {
×
1002
                return None;
×
1003
            }
1004

1005
            &*val_ptr
×
1006
        };
1007

1008
        if !key.is_long() && !key.is_string() {
×
1009
            key.set_long(self.current_num);
×
1010
        }
1011

1012
        unsafe { zend_hash_move_forward_ex(ptr::from_ref(self.ht).cast_mut(), &raw mut self.pos) };
×
1013
        self.current_num += 1;
×
1014

1015
        Some((key, value))
×
1016
    }
1017
}
1018

1019
/// Immutable iterator which iterates over the values of the hashtable, as it
1020
/// was a set or list.
1021
pub struct Values<'a>(Iter<'a>);
1022

1023
impl<'a> Values<'a> {
1024
    /// Creates a new iterator over a hashtables values.
1025
    ///
1026
    /// # Parameters
1027
    ///
1028
    /// * `ht` - The hashtable to iterate.
1029
    pub fn new(ht: &'a ZendHashTable) -> Self {
×
1030
        Self(Iter::new(ht))
×
1031
    }
1032
}
1033

1034
impl<'a> Iterator for Values<'a> {
1035
    type Item = &'a Zval;
1036

1037
    fn next(&mut self) -> Option<Self::Item> {
×
1038
        self.0.next().map(|(_, zval)| zval)
×
1039
    }
1040

1041
    fn count(self) -> usize
×
1042
    where
1043
        Self: Sized,
1044
    {
1045
        self.0.count()
×
1046
    }
1047
}
1048

1049
impl ExactSizeIterator for Values<'_> {
1050
    fn len(&self) -> usize {
×
1051
        self.0.len()
×
1052
    }
1053
}
1054

1055
impl DoubleEndedIterator for Values<'_> {
1056
    fn next_back(&mut self) -> Option<Self::Item> {
×
1057
        self.0.next_back().map(|(_, zval)| zval)
×
1058
    }
1059
}
1060

1061
impl Default for ZBox<ZendHashTable> {
1062
    fn default() -> Self {
×
1063
        ZendHashTable::new()
×
1064
    }
1065
}
1066

1067
impl Clone for ZBox<ZendHashTable> {
1068
    fn clone(&self) -> Self {
×
1069
        (**self).to_owned()
×
1070
    }
1071
}
1072

1073
impl IntoZval for ZBox<ZendHashTable> {
1074
    const TYPE: DataType = DataType::Array;
1075
    const NULLABLE: bool = false;
1076

1077
    fn set_zval(self, zv: &mut Zval, _: bool) -> Result<()> {
×
1078
        zv.set_hashtable(self);
×
1079
        Ok(())
×
1080
    }
1081
}
1082

1083
impl<'a> FromZval<'a> for &'a ZendHashTable {
1084
    const TYPE: DataType = DataType::Array;
1085

1086
    fn from_zval(zval: &'a Zval) -> Option<Self> {
×
1087
        zval.array()
×
1088
    }
1089
}
1090

1091
///////////////////////////////////////////
1092
// HashMap
1093
///////////////////////////////////////////
1094

1095
// TODO: Generalize hasher
1096
#[allow(clippy::implicit_hasher)]
1097
impl<'a, V> TryFrom<&'a ZendHashTable> for HashMap<String, V>
1098
where
1099
    V: FromZval<'a>,
1100
{
1101
    type Error = Error;
1102

1103
    fn try_from(value: &'a ZendHashTable) -> Result<Self> {
×
1104
        let mut hm = HashMap::with_capacity(value.len());
×
1105

1106
        for (key, val) in value {
×
1107
            hm.insert(
×
1108
                key.to_string(),
×
1109
                V::from_zval(val).ok_or_else(|| Error::ZvalConversion(val.get_type()))?,
×
1110
            );
1111
        }
1112

1113
        Ok(hm)
×
1114
    }
1115
}
1116

1117
impl<K, V> TryFrom<HashMap<K, V>> for ZBox<ZendHashTable>
1118
where
1119
    K: AsRef<str>,
1120
    V: IntoZval,
1121
{
1122
    type Error = Error;
1123

1124
    fn try_from(value: HashMap<K, V>) -> Result<Self> {
×
1125
        let mut ht = ZendHashTable::with_capacity(
1126
            value.len().try_into().map_err(|_| Error::IntegerOverflow)?,
×
1127
        );
1128

1129
        for (k, v) in value {
×
1130
            ht.insert(k.as_ref(), v)?;
×
1131
        }
1132

1133
        Ok(ht)
×
1134
    }
1135
}
1136

1137
// TODO: Generalize hasher
1138
#[allow(clippy::implicit_hasher)]
1139
impl<K, V> IntoZval for HashMap<K, V>
1140
where
1141
    K: AsRef<str>,
1142
    V: IntoZval,
1143
{
1144
    const TYPE: DataType = DataType::Array;
1145
    const NULLABLE: bool = false;
1146

1147
    fn set_zval(self, zv: &mut Zval, _: bool) -> Result<()> {
×
1148
        let arr = self.try_into()?;
×
1149
        zv.set_hashtable(arr);
×
1150
        Ok(())
×
1151
    }
1152
}
1153

1154
// TODO: Generalize hasher
1155
#[allow(clippy::implicit_hasher)]
1156
impl<'a, T> FromZval<'a> for HashMap<String, T>
1157
where
1158
    T: FromZval<'a>,
1159
{
1160
    const TYPE: DataType = DataType::Array;
1161

1162
    fn from_zval(zval: &'a Zval) -> Option<Self> {
×
1163
        zval.array().and_then(|arr| arr.try_into().ok())
×
1164
    }
1165
}
1166

1167
///////////////////////////////////////////
1168
// BTreeMap
1169
///////////////////////////////////////////
1170

1171
impl<'a, V> TryFrom<&'a ZendHashTable> for BTreeMap<String, V>
1172
where
1173
    V: FromZval<'a>,
1174
{
1175
    type Error = Error;
1176

NEW
1177
    fn try_from(value: &'a ZendHashTable) -> Result<Self> {
×
NEW
1178
        let mut hm = BTreeMap::new();
×
1179

NEW
1180
        for (key, val) in value {
×
NEW
1181
            hm.insert(
×
NEW
1182
                key.to_string(),
×
NEW
1183
                V::from_zval(val).ok_or_else(|| Error::ZvalConversion(val.get_type()))?,
×
1184
            );
1185
        }
1186

NEW
1187
        Ok(hm)
×
1188
    }
1189
}
1190

1191
impl<K, V> TryFrom<BTreeMap<K, V>> for ZBox<ZendHashTable>
1192
where
1193
    K: AsRef<str>,
1194
    V: IntoZval,
1195
{
1196
    type Error = Error;
1197

NEW
1198
    fn try_from(value: BTreeMap<K, V>) -> Result<Self> {
×
1199
        let mut ht = ZendHashTable::with_capacity(
NEW
1200
            value.len().try_into().map_err(|_| Error::IntegerOverflow)?,
×
1201
        );
1202

NEW
1203
        for (k, v) in value {
×
NEW
1204
            ht.insert(k.as_ref(), v)?;
×
1205
        }
1206

NEW
1207
        Ok(ht)
×
1208
    }
1209
}
1210

1211
impl<K, V> IntoZval for BTreeMap<K, V>
1212
where
1213
    K: AsRef<str>,
1214
    V: IntoZval,
1215
{
1216
    const TYPE: DataType = DataType::Array;
1217
    const NULLABLE: bool = false;
1218

NEW
1219
    fn set_zval(self, zv: &mut Zval, _: bool) -> Result<()> {
×
NEW
1220
        let arr = self.try_into()?;
×
NEW
1221
        zv.set_hashtable(arr);
×
NEW
1222
        Ok(())
×
1223
    }
1224
}
1225

1226
impl<'a, T> FromZval<'a> for BTreeMap<String, T>
1227
where
1228
    T: FromZval<'a>,
1229
{
1230
    const TYPE: DataType = DataType::Array;
1231

NEW
1232
    fn from_zval(zval: &'a Zval) -> Option<Self> {
×
NEW
1233
        zval.array().and_then(|arr| arr.try_into().ok())
×
1234
    }
1235
}
1236

1237
///////////////////////////////////////////
1238
// Vec
1239
///////////////////////////////////////////
1240

1241
impl<'a, T> TryFrom<&'a ZendHashTable> for Vec<T>
1242
where
1243
    T: FromZval<'a>,
1244
{
1245
    type Error = Error;
1246

1247
    fn try_from(value: &'a ZendHashTable) -> Result<Self> {
×
1248
        let mut vec = Vec::with_capacity(value.len());
×
1249

1250
        for (_, val) in value {
×
1251
            vec.push(T::from_zval(val).ok_or_else(|| Error::ZvalConversion(val.get_type()))?);
×
1252
        }
1253

1254
        Ok(vec)
×
1255
    }
1256
}
1257

1258
impl<T> TryFrom<Vec<T>> for ZBox<ZendHashTable>
1259
where
1260
    T: IntoZval,
1261
{
1262
    type Error = Error;
1263

1264
    fn try_from(value: Vec<T>) -> Result<Self> {
×
1265
        let mut ht = ZendHashTable::with_capacity(
1266
            value.len().try_into().map_err(|_| Error::IntegerOverflow)?,
×
1267
        );
1268

1269
        for val in value {
×
1270
            ht.push(val)?;
×
1271
        }
1272

1273
        Ok(ht)
×
1274
    }
1275
}
1276

1277
impl<T> IntoZval for Vec<T>
1278
where
1279
    T: IntoZval,
1280
{
1281
    const TYPE: DataType = DataType::Array;
1282
    const NULLABLE: bool = false;
1283

1284
    fn set_zval(self, zv: &mut Zval, _: bool) -> Result<()> {
×
1285
        let arr = self.try_into()?;
×
1286
        zv.set_hashtable(arr);
×
1287
        Ok(())
×
1288
    }
1289
}
1290

1291
impl<'a, T> FromZval<'a> for Vec<T>
1292
where
1293
    T: FromZval<'a>,
1294
{
1295
    const TYPE: DataType = DataType::Array;
1296

1297
    fn from_zval(zval: &'a Zval) -> Option<Self> {
×
1298
        zval.array().and_then(|arr| arr.try_into().ok())
×
1299
    }
1300
}
1301

1302
impl FromIterator<Zval> for ZBox<ZendHashTable> {
1303
    fn from_iter<T: IntoIterator<Item = Zval>>(iter: T) -> Self {
×
1304
        let mut ht = ZendHashTable::new();
×
1305
        for item in iter {
×
1306
            // Inserting a zval cannot fail, as `push` only returns `Err` if converting
1307
            // `val` to a zval fails.
1308
            let _ = ht.push(item);
×
1309
        }
1310
        ht
×
1311
    }
1312
}
1313

1314
impl FromIterator<(i64, Zval)> for ZBox<ZendHashTable> {
1315
    fn from_iter<T: IntoIterator<Item = (i64, Zval)>>(iter: T) -> Self {
×
1316
        let mut ht = ZendHashTable::new();
×
1317
        for (key, val) in iter {
×
1318
            // Inserting a zval cannot fail, as `push` only returns `Err` if converting
1319
            // `val` to a zval fails.
1320
            let _ = ht.insert_at_index(key, val);
×
1321
        }
1322
        ht
×
1323
    }
1324
}
1325

1326
impl<'a> FromIterator<(&'a str, Zval)> for ZBox<ZendHashTable> {
1327
    fn from_iter<T: IntoIterator<Item = (&'a str, Zval)>>(iter: T) -> Self {
×
1328
        let mut ht = ZendHashTable::new();
×
1329
        for (key, val) in iter {
×
1330
            // Inserting a zval cannot fail, as `push` only returns `Err` if converting
1331
            // `val` to a zval fails.
1332
            let _ = ht.insert(key, val);
×
1333
        }
1334
        ht
×
1335
    }
1336
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc