php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63508 LimitIterator should implement SeekableIterator
Submitted: 2012-11-13 23:00 UTC Modified: 2012-11-16 09:20 UTC
Votes:2
Avg. Score:3.0 ± 2.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: levim@php.net Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.4.8 OS:
Private report: No CVE-ID: None
 [2012-11-13 23:00 UTC] levim@php.net
Description:
------------
LimitIterator already implements SeekableIterator; it just doesn't do so formally.

Test script:
---------------
<?php

$iterator = new LimitIterator(
    new ArrayIterator(array(1,2,3,4))
);

var_dump($iterator instanceof SeekableIterator);
var_dump(method_exists($iterator, 'seek'));

$iterator->seek(2);
var_dump($iterator->current());

Expected result:
----------------
bool(true)
bool(true)
int(3)

Actual result:
--------------
bool(false)
bool(true)
int(3)

Patches

LimitIterator_implement_SeekableIterator (last revision 2012-11-13 23:08 UTC by levim@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-11-13 23:00 UTC] levim@php.net
The following patch has been added/updated:

Patch Name: LimitIterator_implement_SeekableIterator
Revision:   1352847640
URL:        https://bugs.php.net/patch-display.php?bug=63508&patch=LimitIterator_implement_SeekableIterator&revision=1352847640
 [2012-11-13 23:08 UTC] levim@php.net
The following patch has been added/updated:

Patch Name: LimitIterator_implement_SeekableIterator
Revision:   1352848115
URL:        https://bugs.php.net/patch-display.php?bug=63508&patch=LimitIterator_implement_SeekableIterator&revision=1352848115
 [2012-11-13 23:18 UTC] levim@php.net
-Type: Feature/Change Request +Type: Bug
 [2012-11-13 23:19 UTC] levim@php.net
-PHP Version: Irrelevant +PHP Version: 5.4.8
 [2012-11-14 06:05 UTC] levim@php.net
I just noticed something: LimitIterator::seek returns int; SeekableIterator::seek 
returns void;

When they were created they should have been made to match.  That's probably why 
LimitIterator didn't implement SeekableIterator, though.  

Is this a problem?
 [2012-11-16 09:20 UTC] nikic@php.net
The LimitIterator::seek() method does not have the same functionality as the SeekableIterator::seek() method. The latter seeks to a certain offset in an iterator, whereas seeks to a certain offset in the *parent* iterator.

E.g. given this code:

<?php
$array = range(0, 9);
$it = new ArrayIterator($array);
$lim = new LimitIterator($it, 3, 4);
$lim->seek(3);
var_dump(iterator_to_array($lim));

The result will the numbers 3 to 6, rather than just 3+3 to 6 (i.e. just 6).

Also the limit iterator won't allow you to seek outside the range of the limit, e.g. you can't do $lim->seek(2) in the above example.

This behavior difference is rather unfortunate :/ But in its current form it can't implement the interface.
 [2012-11-16 09:20 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 09:01:28 2024 UTC