Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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
 
1294 lars 24
    /**
25
     * @param callable(T): bool $filter
26
     * @return DirectoryListing<T>
27
     */
148 lars 28
    public function filter(callable $filter): DirectoryListing
29
    {
30
        $generator = (static function (iterable $listing) use ($filter): Generator {
31
            foreach ($listing as $item) {
32
                if ($filter($item)) {
33
                    yield $item;
34
                }
35
            }
36
        })($this->listing);
37
 
38
        return new DirectoryListing($generator);
39
    }
40
 
1294 lars 41
    /**
42
     * @template R
43
     * @param callable(T): R $mapper
44
     * @return DirectoryListing<R>
45
     */
148 lars 46
    public function map(callable $mapper): DirectoryListing
47
    {
48
        $generator = (static function (iterable $listing) use ($mapper): Generator {
49
            foreach ($listing as $item) {
50
                yield $mapper($item);
51
            }
52
        })($this->listing);
53
 
54
        return new DirectoryListing($generator);
55
    }
56
 
1294 lars 57
    /**
58
     * @return DirectoryListing<T>
59
     */
148 lars 60
    public function sortByPath(): DirectoryListing
61
    {
62
        $listing = $this->toArray();
63
 
64
        usort($listing, function (StorageAttributes $a, StorageAttributes $b) {
65
            return $a->path() <=> $b->path();
66
        });
67
 
68
        return new DirectoryListing($listing);
69
    }
70
 
71
    /**
72
     * @return Traversable<T>
73
     */
74
    public function getIterator(): Traversable
75
    {
76
        return $this->listing instanceof Traversable
77
            ? $this->listing
78
            : new ArrayIterator($this->listing);
79
    }
80
 
81
    /**
82
     * @return T[]
83
     */
84
    public function toArray(): array
85
    {
86
        return $this->listing instanceof Traversable
87
            ? iterator_to_array($this->listing, false)
88
            : (array) $this->listing;
89
    }
90
}