1 <?php
2
3 /*
4 * This file is part of the ICanBoogie package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace ICanBoogie\HTTP;
13
14 /**
15 * Representation of a list of request files.
16 */
17 class FileList implements \ArrayAccess, \IteratorAggregate, \Countable
18 {
19 static public function from($files)
20 {
21 if ($files instanceof self)
22 {
23 return clone $files;
24 }
25
26 if (!$files)
27 {
28 return new static([]);
29 }
30
31 foreach ($files as &$file)
32 {
33 $file = File::from($file);
34 }
35
36 return new static($files);
37 }
38
39 protected $list;
40
41 public function __construct(array $files)
42 {
43 foreach ($files as $id => $file)
44 {
45 $this[$id] = $file;
46 }
47 }
48
49 public function offsetExists($id)
50 {
51 return isset($this->list[$id]);
52 }
53
54 public function offsetGet($id)
55 {
56 if (!$this->offsetExists($id))
57 {
58 return;
59 }
60
61 return $this->list[$id];
62 }
63
64 public function offsetSet($id, $file)
65 {
66 if (!($file instanceof File || $file instanceof FileList))
67 {
68 $file = File::from($file);
69 }
70
71 $this->list[$id] = $file;
72 }
73
74 public function offsetUnset($id)
75 {
76 unset($this->list[$id]);
77 }
78
79 public function getIterator()
80 {
81 return new \ArrayIterator($this->list);
82 }
83
84 public function count()
85 {
86 return count($this->list);
87 }
88 }