Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Collection
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 10
240
0.00% covered (danger)
0.00%
0 / 1
 setSources
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addSource
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSources
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSourceByName
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 findFile
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 rewind
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 current
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 key
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 next
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 valid
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   Modules\Media\Models
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace Modules\Media\Models;
16
17use phpOMS\Utils\StringUtils;
18
19/**
20 * Media class.
21 *
22 * @package Modules\Media\Models
23 * @license OMS License 2.0
24 * @link    https://jingga.app
25 * @since   1.0.0
26 */
27class Collection extends Media implements \Iterator
28{
29    /**
30     * Resource id.
31     *
32     * @var array<int, Media>
33     * @since 1.0.0
34     */
35    public array $sources = [];
36
37    /**
38     * Extension name.
39     *
40     * @var string
41     * @since 1.0.0
42     */
43    public string $extension = 'collection';
44
45    /**
46     * Is collection.
47     *
48     * @var int
49     * @since 1.0.0
50     */
51    public int $class = MediaClass::COLLECTION;
52
53    /**
54     * Set sources.
55     *
56     * @param array $sources Source array
57     *
58     * @return void
59     *
60     * @since 1.0.0
61     */
62    public function setSources(array $sources) : void
63    {
64        $this->sources = $sources;
65    }
66
67    /**
68     * Set sources.
69     *
70     * @param Media $source Source
71     *
72     * @return void
73     *
74     * @since 1.0.0
75     */
76    public function addSource(Media $source) : void
77    {
78        $this->sources[] = $source;
79    }
80
81    /**
82     * Get sources.
83     *
84     * @return Media[]
85     *
86     * @since 1.0.0
87     */
88    public function getSources() : array
89    {
90        return $this->sources;
91    }
92
93    /**
94     * Get media element by its name.
95     *
96     * @param string $name Name of the media element
97     *
98     * @return Media
99     *
100     * @since 1.0.0
101     */
102    public function getSourceByName(string $name) : Media
103    {
104        foreach ($this->sources as $source) {
105            if ($source->name === $name) {
106                return $source;
107            }
108        }
109
110        return new NullMedia();
111    }
112
113    /**
114     * Find file by file name
115     *
116     * @param string $name File name
117     *
118     * @return Media
119     *
120     * @since 1.0.0
121     */
122    public function findFile(string $name) : Media
123    {
124        foreach ($this->sources as $file) {
125            if (StringUtils::endsWith($file->getPath(), $name)) {
126                return $file;
127            }
128        }
129
130        return new NullMedia();
131    }
132
133    /**
134     * {@inheritdoc}
135     */
136    public function rewind() : void
137    {
138        \reset($this->sources);
139    }
140
141    /**
142     * {@inheritdoc}
143     */
144    public function current() : Media
145    {
146        $current = \current($this->sources);
147
148        return $current === false ? $this : $current;
149    }
150
151    /**
152     * {@inheritdoc}
153     */
154    public function key() : ?int
155    {
156        return \key($this->sources);
157    }
158
159    /**
160     * {@inheritdoc}
161     */
162    public function next() : void
163    {
164        \next($this->sources);
165    }
166
167    /**
168     * {@inheritdoc}
169     */
170    public function valid() : bool
171    {
172        return \current($this->sources) !== false;
173    }
174}