xref: /PHP-8.2/ext/phar/tests/phar_oo_008.phpt (revision c6357b80)
1--TEST--
2Phar object: iterating via SplFileObject
3--EXTENSIONS--
4phar
5--INI--
6phar.require_hash=0
7--FILE--
8<?php
9
10$pharconfig = 1;
11
12require_once 'files/phar_oo_test.inc';
13
14$phar = new Phar($fname);
15$phar->setInfoClass('SplFileObject');
16
17$f = $phar['a.csv'];
18echo "===1===\n";
19foreach($f as $k => $v)
20{
21    echo "$k=>$v\n";
22}
23
24$f->setFlags(SplFileObject::DROP_NEW_LINE);
25
26echo "===2===\n";
27foreach($f as $k => $v)
28{
29    echo "$k=>$v\n";
30}
31
32class MyCSVFile extends SplFileObject
33{
34    function current(): array|false
35    {
36        return parent::fgetcsv(',', '"');
37    }
38}
39
40$phar->setInfoClass('MyCSVFile');
41/** @var MyCSVFile $v */
42$v = $phar['a.csv'];
43
44echo "===3===\n";
45while(!$v->eof())
46{
47    echo $v->key() . "=>" . join('|', $v->fgetcsv()) . "\n";
48}
49
50echo "===4===\n";
51$v->rewind();
52while(!$v->eof())
53{
54    $l = $v->fgetcsv();
55    echo $v->key() . "=>" . join('|', $l) . "\n";
56}
57
58echo "===5===\n";
59foreach($v as $k => $d)
60{
61    echo "$k=>" . join('|', $d) . "\n";
62}
63
64class MyCSVFile2 extends SplFileObject
65{
66    function getCurrentLine(): string
67    {
68        echo __METHOD__ . "\n";
69        return implode('|', parent::fgetcsv(',', '"'));
70    }
71}
72
73$phar->setInfoClass('MyCSVFile2');
74/** @var MyCSVFile2 $v */
75$v = $phar['a.csv'];
76
77echo "===6===\n";
78foreach($v as $k => $d)
79{
80    echo "$k=>" . $d . "\n";
81}
82
83?>
84--CLEAN--
85<?php
86unlink(__DIR__ . '/files/phar_oo_008.phar.php');
87__halt_compiler();
88?>
89--EXPECT--
90===1===
910=>1,2,3
92
931=>2,a,b
94
952=>3,"c","'e'"
96===2===
970=>1,2,3
981=>2,a,b
992=>3,"c","'e'"
100===3===
1010=>1|2|3
1020=>2|a|b
1031=>3|c|'e'
104===4===
1050=>1|2|3
1061=>2|a|b
1072=>3|c|'e'
108===5===
1090=>1|2|3
1101=>2|a|b
1112=>3|c|'e'
112===6===
113MyCSVFile2::getCurrentLine
1141=>1|2|3
115MyCSVFile2::getCurrentLine
1163=>2|a|b
117MyCSVFile2::getCurrentLine
1185=>3|c|'e'
119