1 | <?php |
||
8 | class Icon implements \ArrayAccess, \Countable, \Iterator |
||
9 | { |
||
10 | /** |
||
11 | * @var IconImage[] |
||
12 | */ |
||
13 | private $images = []; |
||
14 | |||
15 | /** |
||
16 | * @var int iterator position |
||
17 | */ |
||
18 | private $position = 0; |
||
19 | |||
20 | /** |
||
21 | * Returns best icon image with dimensions matching w,h |
||
22 | * @param $w |
||
23 | * @param $h |
||
24 | * @return IconImage|null |
||
25 | */ |
||
26 | 1 | public function findBestForSize($w, $h) |
|
27 | { |
||
28 | 1 | $bestBitCount = 0; |
|
29 | 1 | $best = null; |
|
30 | 1 | foreach ($this->images as $image) { |
|
31 | 1 | if ($image->width == $w && $image->height == $h && ($image->bitCount > $bestBitCount)) { |
|
32 | $bestBitCount = $image->bitCount; |
||
33 | 1 | $best = $image; |
|
34 | } |
||
35 | } |
||
36 | 1 | return $best; |
|
37 | } |
||
38 | |||
39 | /** |
||
40 | * Finds the highest quality image in the icon |
||
41 | * @return IconImage |
||
42 | */ |
||
43 | 1 | public function findBest() |
|
44 | { |
||
45 | 1 | $bestBitCount = 0; |
|
46 | 1 | $bestWidth = 0; |
|
47 | 1 | $best = null; |
|
48 | 1 | foreach ($this->images as $image) { |
|
49 | 1 | if (($image->width > $bestWidth) || |
|
50 | 1 | (($image->width == $bestWidth) && ($image->bitCount > $bestBitCount)) |
|
51 | ) { |
||
52 | 1 | $bestWidth = $image->width; |
|
53 | 1 | $bestBitCount = $image->bitCount; |
|
54 | 1 | $best = $image; |
|
55 | } |
||
56 | } |
||
57 | 1 | return $best; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Count number of images in the icon |
||
62 | * As this class implements Countable you can simply use count($icon) if you desire |
||
63 | * @return int |
||
64 | */ |
||
65 | 19 | public function count() |
|
69 | |||
70 | /** |
||
71 | * Set an icon |
||
72 | * This is an implementation of ArrayAccess allowing you to do $icon[$x]=$image |
||
73 | * @param integer $offset |
||
74 | * @param IconImage $value |
||
75 | */ |
||
76 | 20 | public function offsetSet($offset, $value) |
|
77 | { |
||
78 | 20 | if (!$value instanceof IconImage) { |
|
79 | 1 | throw new \InvalidArgumentException('Can only add IconImage instances to an Icon'); |
|
80 | } |
||
81 | 19 | if (is_null($offset)) { |
|
82 | 19 | $this->images[] = $value; |
|
83 | } else { |
||
84 | 1 | $this->images[$offset] = $value; |
|
85 | } |
||
86 | 19 | } |
|
87 | |||
88 | /** |
||
89 | * Check if image with particular index exists |
||
90 | * This is an implementation of ArrayAccess allowing you to do isset($icon[$x]) |
||
91 | * @param integer $offset |
||
92 | * @return boolean |
||
93 | */ |
||
94 | 1 | public function offsetExists($offset) |
|
98 | |||
99 | /** |
||
100 | * Remove image from icon |
||
101 | * This is an implementation of ArrayAccess allowing you to do unset($icon[$x]) |
||
102 | * @param integer $offset |
||
103 | * @return boolean |
||
104 | */ |
||
105 | 1 | public function offsetUnset($offset) |
|
109 | |||
110 | /** |
||
111 | * Get image from icon |
||
112 | * This is an implementation of ArrayAccess allowing you to do $image = $icon[$x] |
||
113 | * @param integer $offset |
||
114 | * @return IconImage |
||
115 | */ |
||
116 | 18 | public function offsetGet($offset) |
|
120 | |||
121 | /** |
||
122 | * Implements \Iterator allowing foreach($icon as $image){} |
||
123 | */ |
||
124 | 1 | public function rewind() |
|
128 | |||
129 | /** |
||
130 | * Implements \Iterator allowing foreach($icon as $image){} |
||
131 | */ |
||
132 | 1 | public function current() |
|
136 | |||
137 | /** |
||
138 | * Implements \Iterator allowing foreach($icon as $image){} |
||
139 | */ |
||
140 | public function key() |
||
144 | |||
145 | /** |
||
146 | * Implements \Iterator allowing foreach($icon as $image){} |
||
147 | */ |
||
148 | 1 | public function next() |
|
152 | |||
153 | /** |
||
154 | * Implements \Iterator allowing foreach($icon as $image){} |
||
155 | */ |
||
156 | 1 | public function valid() |
|
160 | } |
||
161 |