Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
66 / 66 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
Media | |
100.00% |
66 / 66 |
|
100.00% |
5 / 5 |
25 | |
100.00% |
1 / 1 |
addElement | |
100.00% |
41 / 41 |
|
100.00% |
1 / 1 |
9 | |||
countElements | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
getElements | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
6 | |||
getElementsByType | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
resetElements | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * This file is part of PHPWord - A pure PHP library for reading and writing |
4 | * word processing documents. |
5 | * |
6 | * PHPWord is free software distributed under the terms of the GNU Lesser |
7 | * General Public License version 3 as published by the Free Software Foundation. |
8 | * |
9 | * For the full copyright and license information, please read the LICENSE |
10 | * file that was distributed with this source code. For the full list of |
11 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
12 | * |
13 | * @see https://github.com/PHPOffice/PHPWord |
14 | * |
15 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
16 | */ |
17 | |
18 | namespace PhpOffice\PhpWord; |
19 | |
20 | use PhpOffice\PhpWord\Element\Image; |
21 | use PhpOffice\PhpWord\Exception\Exception; |
22 | |
23 | /** |
24 | * Media collection. |
25 | */ |
26 | class Media |
27 | { |
28 | /** |
29 | * Media elements. |
30 | * |
31 | * @var array |
32 | */ |
33 | private static $elements = []; |
34 | |
35 | /** |
36 | * Add new media element. |
37 | * |
38 | * @since 0.10.0 |
39 | * @since 0.9.2 |
40 | * |
41 | * @param string $container section|headerx|footerx|footnote|endnote |
42 | * @param string $mediaType image|object|link |
43 | * @param string $source |
44 | * |
45 | * @return int |
46 | */ |
47 | public static function addElement($container, $mediaType, $source, ?Image $image = null) |
48 | { |
49 | // Assign unique media Id and initiate media container if none exists |
50 | $mediaId = md5($container . $source); |
51 | if (!isset(self::$elements[$container])) { |
52 | self::$elements[$container] = []; |
53 | } |
54 | |
55 | // Add media if not exists or point to existing media |
56 | if (!isset(self::$elements[$container][$mediaId])) { |
57 | $mediaCount = self::countElements($container); |
58 | $mediaTypeCount = self::countElements($container, $mediaType); |
59 | ++$mediaTypeCount; |
60 | $rId = ++$mediaCount; |
61 | $target = null; |
62 | $mediaData = ['mediaIndex' => $mediaTypeCount]; |
63 | |
64 | switch ($mediaType) { |
65 | // Images |
66 | case 'image': |
67 | if (null === $image) { |
68 | throw new Exception('Image object not assigned.'); |
69 | } |
70 | $isMemImage = $image->isMemImage(); |
71 | $extension = $image->getImageExtension(); |
72 | $mediaData['imageExtension'] = $extension; |
73 | $mediaData['imageType'] = $image->getImageType(); |
74 | if ($isMemImage) { |
75 | $mediaData['isMemImage'] = true; |
76 | $mediaData['imageString'] = $image->getImageString(); |
77 | } |
78 | $target = "{$container}_image{$mediaTypeCount}.{$extension}"; |
79 | $image->setTarget($target); |
80 | $image->setMediaIndex($mediaTypeCount); |
81 | |
82 | break; |
83 | // Objects |
84 | case 'object': |
85 | $target = "{$container}_oleObject{$mediaTypeCount}.bin"; |
86 | |
87 | break; |
88 | // Links |
89 | case 'link': |
90 | $target = $source; |
91 | |
92 | break; |
93 | } |
94 | |
95 | $mediaData['source'] = $source; |
96 | $mediaData['target'] = $target; |
97 | $mediaData['type'] = $mediaType; |
98 | $mediaData['rID'] = $rId; |
99 | self::$elements[$container][$mediaId] = $mediaData; |
100 | |
101 | return $rId; |
102 | } |
103 | |
104 | $mediaData = self::$elements[$container][$mediaId]; |
105 | if (null !== $image) { |
106 | $image->setTarget($mediaData['target']); |
107 | $image->setMediaIndex($mediaData['mediaIndex']); |
108 | } |
109 | |
110 | return $mediaData['rID']; |
111 | } |
112 | |
113 | /** |
114 | * Get media elements count. |
115 | * |
116 | * @param string $container section|headerx|footerx|footnote|endnote |
117 | * @param string $mediaType image|object|link |
118 | * |
119 | * @return int |
120 | * |
121 | * @since 0.10.0 |
122 | */ |
123 | public static function countElements($container, $mediaType = null) |
124 | { |
125 | $mediaCount = 0; |
126 | |
127 | if (isset(self::$elements[$container])) { |
128 | foreach (self::$elements[$container] as $mediaData) { |
129 | if (null !== $mediaType) { |
130 | if ($mediaType == $mediaData['type']) { |
131 | ++$mediaCount; |
132 | } |
133 | } else { |
134 | ++$mediaCount; |
135 | } |
136 | } |
137 | } |
138 | |
139 | return $mediaCount; |
140 | } |
141 | |
142 | /** |
143 | * Get media elements. |
144 | * |
145 | * @param string $container section|headerx|footerx|footnote|endnote |
146 | * @param string $type image|object|link |
147 | * |
148 | * @return array |
149 | * |
150 | * @since 0.10.0 |
151 | */ |
152 | public static function getElements($container, $type = null) |
153 | { |
154 | $elements = []; |
155 | |
156 | // If header/footer, search for headerx and footerx where x is number |
157 | if ($container == 'header' || $container == 'footer') { |
158 | foreach (self::$elements as $key => $val) { |
159 | if (substr($key, 0, 6) == $container) { |
160 | $elements[$key] = $val; |
161 | } |
162 | } |
163 | |
164 | return $elements; |
165 | } |
166 | |
167 | if (!isset(self::$elements[$container])) { |
168 | return $elements; |
169 | } |
170 | |
171 | return self::getElementsByType($container, $type); |
172 | } |
173 | |
174 | /** |
175 | * Get elements by media type. |
176 | * |
177 | * @param string $container section|footnote|endnote |
178 | * @param string $type image|object|link |
179 | * |
180 | * @return array |
181 | * |
182 | * @since 0.11.0 Splitted from `getElements` to reduce complexity |
183 | */ |
184 | private static function getElementsByType($container, $type = null) |
185 | { |
186 | $elements = []; |
187 | |
188 | foreach (self::$elements[$container] as $key => $data) { |
189 | if ($type !== null) { |
190 | if ($type == $data['type']) { |
191 | $elements[$key] = $data; |
192 | } |
193 | } else { |
194 | $elements[$key] = $data; |
195 | } |
196 | } |
197 | |
198 | return $elements; |
199 | } |
200 | |
201 | /** |
202 | * Reset media elements. |
203 | */ |
204 | public static function resetElements(): void |
205 | { |
206 | self::$elements = []; |
207 | } |
208 | } |