Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace League\Flysystem;
6
 
7
use ArrayIterator;
8
use Generator;
9
use IteratorAggregate;
10
use Traversable;
11
 
12
/**
13
 * @template T
14
 */
15
class DirectoryListing implements IteratorAggregate
16
{
17
    /**
18
     * @param iterable<T> $listing
19
     */
20
    public function __construct(private iterable $listing)
21
    {
22
    }
23
 
24
    public function filter(callable $filter): DirectoryListing
25
    {
26
        $generator = (static function (iterable $listing) use ($filter): Generator {
27
            foreach ($listing as $item) {
28
                if ($filter($item)) {
29
                    yield $item;
30
                }
31
            }
32
        })($this->listing);
33
 
34
        return new DirectoryListing($generator);
35
    }
36
 
37
    public function map(callable $mapper): DirectoryListing
38
    {
39
        $generator = (static function (iterable $listing) use ($mapper): Generator {
40
            foreach ($listing as $item) {
41
                yield $mapper($item);
42
            }
43
        })($this->listing);
44
 
45
        return new DirectoryListing($generator);
46
    }
47
 
48
    public function sortByPath(): DirectoryListing
49
    {
50
        $listing = $this->toArray();
51
 
52
        usort($listing, function (StorageAttributes $a, StorageAttributes $b) {
53
            return $a->path() <=> $b->path();
54
        });
55
 
56
        return new DirectoryListing($listing);
57
    }
58
 
59
    /**
60
     * @return Traversable<T>
61
     */
62
    public function getIterator(): Traversable
63
    {
64
        return $this->listing instanceof Traversable
65
            ? $this->listing
66
            : new ArrayIterator($this->listing);
67
    }
68
 
69
    /**
70
     * @return T[]
71
     */
72
    public function toArray(): array
73
    {
74
        return $this->listing instanceof Traversable
75
            ? iterator_to_array($this->listing, false)
76
            : (array) $this->listing;
77
    }
78
}