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