Skip to content

$set->toArray is required for Iterator  #82

Closed
@basz

Description

@basz

with the extension v1.1.8 (polypill is fine) I seeing this strange behaviour using php 7.1.3

#!/usr/bin/env php
<?php

declare(strict_types=1);

class RolesWhereUpdated
{
    private $additions;

    public static function withChanges(
        \Ds\Set $additions
    ): RolesWhereUpdated {
        $event = new static();

        $event->additions = $additions->toArray();

        return $event;
    }

    public function additions(): \Ds\Set
    {
        return new \Ds\Set($this->additions);
    }
}

$event = RolesWhereUpdated::withChanges(new \Ds\Set(['abc', 'def']));

// not iterated
foreach ($event->additions() as $item => $value) {
    print 'iterated: $event->additions()' . PHP_EOL;
}

// is iterated
foreach ($event->additions()->toArray() as $item => $value) {
    print 'iterated: $event->additions()->toArray()' . PHP_EOL;
}


// but with a plain set
$set = new \Ds\Set(['abc', 'def']);

// this is iterated
foreach ($set as $item => $value) {
    print 'iterated: $set' . PHP_EOL;
}

expected output

iterated: $event->additions()
iterated: $event->additions()
iterated: $event->additions()->toArray()
iterated: $event->additions()->toArray()
iterated: $set
iterated: $set

actual output

iterated: $event->additions()->toArray()
iterated: $event->additions()->toArray()
iterated: $set
iterated: $set

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions