1--TEST-- 2SplObjectStorage::seek() basic functionality 3--FILE-- 4<?php 5 6class Test { 7 public function __construct(public string $marker) {} 8} 9 10$a = new Test("a"); 11$b = new Test("b"); 12$c = new Test("c"); 13$d = new Test("d"); 14$e = new Test("e"); 15 16$storage = new SplObjectStorage(); 17$storage[$a] = 1; 18$storage[$b] = 2; 19$storage[$c] = 3; 20$storage[$d] = 4; 21$storage[$e] = 5; 22 23echo "--- Error cases ---\n"; 24 25try { 26 $storage->seek(-1); 27} catch (OutOfBoundsException $e) { 28 echo $e->getMessage(), "\n"; 29} 30try { 31 $storage->seek(5); 32} catch (OutOfBoundsException $e) { 33 echo $e->getMessage(), "\n"; 34} 35 36var_dump($storage->key()); 37var_dump($storage->current()); 38 39echo "--- Normal cases ---\n"; 40 41$storage->seek(2); 42var_dump($storage->key()); 43var_dump($storage->current()); 44 45$storage->seek(1); 46var_dump($storage->key()); 47var_dump($storage->current()); 48 49$storage->seek(4); 50var_dump($storage->key()); 51var_dump($storage->current()); 52 53$storage->seek(0); 54var_dump($storage->key()); 55var_dump($storage->current()); 56 57$storage->seek(3); 58var_dump($storage->key()); 59var_dump($storage->current()); 60 61$storage->seek(3); 62var_dump($storage->key()); 63var_dump($storage->current()); 64 65echo "--- With holes cases ---\n"; 66 67$storage->detach($b); 68$storage->detach($d); 69 70foreach (range(0, 2) as $index) { 71 $storage->seek($index); 72 var_dump($storage->key()); 73 var_dump($storage->current()); 74} 75 76try { 77 $storage->seek(3); 78} catch (OutOfBoundsException $e) { 79 echo $e->getMessage(), "\n"; 80} 81 82?> 83--EXPECT-- 84--- Error cases --- 85Seek position -1 is out of range 86Seek position 5 is out of range 87int(0) 88object(Test)#1 (1) { 89 ["marker"]=> 90 string(1) "a" 91} 92--- Normal cases --- 93int(2) 94object(Test)#3 (1) { 95 ["marker"]=> 96 string(1) "c" 97} 98int(1) 99object(Test)#2 (1) { 100 ["marker"]=> 101 string(1) "b" 102} 103int(4) 104object(Test)#5 (1) { 105 ["marker"]=> 106 string(1) "e" 107} 108int(0) 109object(Test)#1 (1) { 110 ["marker"]=> 111 string(1) "a" 112} 113int(3) 114object(Test)#4 (1) { 115 ["marker"]=> 116 string(1) "d" 117} 118int(3) 119object(Test)#4 (1) { 120 ["marker"]=> 121 string(1) "d" 122} 123--- With holes cases --- 124int(0) 125object(Test)#1 (1) { 126 ["marker"]=> 127 string(1) "a" 128} 129int(1) 130object(Test)#3 (1) { 131 ["marker"]=> 132 string(1) "c" 133} 134int(2) 135object(Test)#5 (1) { 136 ["marker"]=> 137 string(1) "e" 138} 139Seek position 3 is out of range 140