PageRenderTime 88ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/application/third_party/phpoffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Worksheet/BaseDrawing.php

https://gitlab.com/Japang-Jawara/jawara-penilaian
PHP | 532 lines | 222 code | 63 blank | 247 comment | 27 complexity | d8d6789f3cb0e18f4f49f4d41e17519d MD5 | raw file
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Worksheet;
  3. use PhpOffice\PhpSpreadsheet\Cell\Hyperlink;
  4. use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
  5. use PhpOffice\PhpSpreadsheet\IComparable;
  6. class BaseDrawing implements IComparable
  7. {
  8. /**
  9. * Image counter.
  10. *
  11. * @var int
  12. */
  13. private static $imageCounter = 0;
  14. /**
  15. * Image index.
  16. *
  17. * @var int
  18. */
  19. private $imageIndex = 0;
  20. /**
  21. * Name.
  22. *
  23. * @var string
  24. */
  25. protected $name;
  26. /**
  27. * Description.
  28. *
  29. * @var string
  30. */
  31. protected $description;
  32. /**
  33. * Worksheet.
  34. *
  35. * @var Worksheet
  36. */
  37. protected $worksheet;
  38. /**
  39. * Coordinates.
  40. *
  41. * @var string
  42. */
  43. protected $coordinates;
  44. /**
  45. * Offset X.
  46. *
  47. * @var int
  48. */
  49. protected $offsetX;
  50. /**
  51. * Offset Y.
  52. *
  53. * @var int
  54. */
  55. protected $offsetY;
  56. /**
  57. * Width.
  58. *
  59. * @var int
  60. */
  61. protected $width;
  62. /**
  63. * Height.
  64. *
  65. * @var int
  66. */
  67. protected $height;
  68. /**
  69. * Proportional resize.
  70. *
  71. * @var bool
  72. */
  73. protected $resizeProportional;
  74. /**
  75. * Rotation.
  76. *
  77. * @var int
  78. */
  79. protected $rotation;
  80. /**
  81. * Shadow.
  82. *
  83. * @var Drawing\Shadow
  84. */
  85. protected $shadow;
  86. /**
  87. * Image hyperlink.
  88. *
  89. * @var null|Hyperlink
  90. */
  91. private $hyperlink;
  92. /**
  93. * Create a new BaseDrawing.
  94. */
  95. public function __construct()
  96. {
  97. // Initialise values
  98. $this->name = '';
  99. $this->description = '';
  100. $this->worksheet = null;
  101. $this->coordinates = 'A1';
  102. $this->offsetX = 0;
  103. $this->offsetY = 0;
  104. $this->width = 0;
  105. $this->height = 0;
  106. $this->resizeProportional = true;
  107. $this->rotation = 0;
  108. $this->shadow = new Drawing\Shadow();
  109. // Set image index
  110. ++self::$imageCounter;
  111. $this->imageIndex = self::$imageCounter;
  112. }
  113. /**
  114. * Get image index.
  115. *
  116. * @return int
  117. */
  118. public function getImageIndex()
  119. {
  120. return $this->imageIndex;
  121. }
  122. /**
  123. * Get Name.
  124. *
  125. * @return string
  126. */
  127. public function getName()
  128. {
  129. return $this->name;
  130. }
  131. /**
  132. * Set Name.
  133. *
  134. * @param string $pValue
  135. *
  136. * @return $this
  137. */
  138. public function setName($pValue)
  139. {
  140. $this->name = $pValue;
  141. return $this;
  142. }
  143. /**
  144. * Get Description.
  145. *
  146. * @return string
  147. */
  148. public function getDescription()
  149. {
  150. return $this->description;
  151. }
  152. /**
  153. * Set Description.
  154. *
  155. * @param string $description
  156. *
  157. * @return $this
  158. */
  159. public function setDescription($description)
  160. {
  161. $this->description = $description;
  162. return $this;
  163. }
  164. /**
  165. * Get Worksheet.
  166. *
  167. * @return Worksheet
  168. */
  169. public function getWorksheet()
  170. {
  171. return $this->worksheet;
  172. }
  173. /**
  174. * Set Worksheet.
  175. *
  176. * @param Worksheet $pValue
  177. * @param bool $pOverrideOld If a Worksheet has already been assigned, overwrite it and remove image from old Worksheet?
  178. *
  179. * @return $this
  180. */
  181. public function setWorksheet(?Worksheet $pValue = null, $pOverrideOld = false)
  182. {
  183. if ($this->worksheet === null) {
  184. // Add drawing to \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
  185. $this->worksheet = $pValue;
  186. $this->worksheet->getCell($this->coordinates);
  187. $this->worksheet->getDrawingCollection()->append($this);
  188. } else {
  189. if ($pOverrideOld) {
  190. // Remove drawing from old \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
  191. $iterator = $this->worksheet->getDrawingCollection()->getIterator();
  192. while ($iterator->valid()) {
  193. if ($iterator->current()->getHashCode() === $this->getHashCode()) {
  194. $this->worksheet->getDrawingCollection()->offsetUnset($iterator->key());
  195. $this->worksheet = null;
  196. break;
  197. }
  198. }
  199. // Set new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
  200. $this->setWorksheet($pValue);
  201. } else {
  202. throw new PhpSpreadsheetException('A Worksheet has already been assigned. Drawings can only exist on one \\PhpOffice\\PhpSpreadsheet\\Worksheet.');
  203. }
  204. }
  205. return $this;
  206. }
  207. /**
  208. * Get Coordinates.
  209. *
  210. * @return string
  211. */
  212. public function getCoordinates()
  213. {
  214. return $this->coordinates;
  215. }
  216. /**
  217. * Set Coordinates.
  218. *
  219. * @param string $pValue eg: 'A1'
  220. *
  221. * @return $this
  222. */
  223. public function setCoordinates($pValue)
  224. {
  225. $this->coordinates = $pValue;
  226. return $this;
  227. }
  228. /**
  229. * Get OffsetX.
  230. *
  231. * @return int
  232. */
  233. public function getOffsetX()
  234. {
  235. return $this->offsetX;
  236. }
  237. /**
  238. * Set OffsetX.
  239. *
  240. * @param int $pValue
  241. *
  242. * @return $this
  243. */
  244. public function setOffsetX($pValue)
  245. {
  246. $this->offsetX = $pValue;
  247. return $this;
  248. }
  249. /**
  250. * Get OffsetY.
  251. *
  252. * @return int
  253. */
  254. public function getOffsetY()
  255. {
  256. return $this->offsetY;
  257. }
  258. /**
  259. * Set OffsetY.
  260. *
  261. * @param int $pValue
  262. *
  263. * @return $this
  264. */
  265. public function setOffsetY($pValue)
  266. {
  267. $this->offsetY = $pValue;
  268. return $this;
  269. }
  270. /**
  271. * Get Width.
  272. *
  273. * @return int
  274. */
  275. public function getWidth()
  276. {
  277. return $this->width;
  278. }
  279. /**
  280. * Set Width.
  281. *
  282. * @param int $pValue
  283. *
  284. * @return $this
  285. */
  286. public function setWidth($pValue)
  287. {
  288. // Resize proportional?
  289. if ($this->resizeProportional && $pValue != 0) {
  290. $ratio = $this->height / ($this->width != 0 ? $this->width : 1);
  291. $this->height = round($ratio * $pValue);
  292. }
  293. // Set width
  294. $this->width = $pValue;
  295. return $this;
  296. }
  297. /**
  298. * Get Height.
  299. *
  300. * @return int
  301. */
  302. public function getHeight()
  303. {
  304. return $this->height;
  305. }
  306. /**
  307. * Set Height.
  308. *
  309. * @param int $pValue
  310. *
  311. * @return $this
  312. */
  313. public function setHeight($pValue)
  314. {
  315. // Resize proportional?
  316. if ($this->resizeProportional && $pValue != 0) {
  317. $ratio = $this->width / ($this->height != 0 ? $this->height : 1);
  318. $this->width = round($ratio * $pValue);
  319. }
  320. // Set height
  321. $this->height = $pValue;
  322. return $this;
  323. }
  324. /**
  325. * Set width and height with proportional resize.
  326. *
  327. * Example:
  328. * <code>
  329. * $objDrawing->setResizeProportional(true);
  330. * $objDrawing->setWidthAndHeight(160,120);
  331. * </code>
  332. *
  333. * @author Vincent@luo MSN:kele_100@hotmail.com
  334. *
  335. * @param int $width
  336. * @param int $height
  337. *
  338. * @return $this
  339. */
  340. public function setWidthAndHeight($width, $height)
  341. {
  342. $xratio = $width / ($this->width != 0 ? $this->width : 1);
  343. $yratio = $height / ($this->height != 0 ? $this->height : 1);
  344. if ($this->resizeProportional && !($width == 0 || $height == 0)) {
  345. if (($xratio * $this->height) < $height) {
  346. $this->height = ceil($xratio * $this->height);
  347. $this->width = $width;
  348. } else {
  349. $this->width = ceil($yratio * $this->width);
  350. $this->height = $height;
  351. }
  352. } else {
  353. $this->width = $width;
  354. $this->height = $height;
  355. }
  356. return $this;
  357. }
  358. /**
  359. * Get ResizeProportional.
  360. *
  361. * @return bool
  362. */
  363. public function getResizeProportional()
  364. {
  365. return $this->resizeProportional;
  366. }
  367. /**
  368. * Set ResizeProportional.
  369. *
  370. * @param bool $pValue
  371. *
  372. * @return $this
  373. */
  374. public function setResizeProportional($pValue)
  375. {
  376. $this->resizeProportional = $pValue;
  377. return $this;
  378. }
  379. /**
  380. * Get Rotation.
  381. *
  382. * @return int
  383. */
  384. public function getRotation()
  385. {
  386. return $this->rotation;
  387. }
  388. /**
  389. * Set Rotation.
  390. *
  391. * @param int $pValue
  392. *
  393. * @return $this
  394. */
  395. public function setRotation($pValue)
  396. {
  397. $this->rotation = $pValue;
  398. return $this;
  399. }
  400. /**
  401. * Get Shadow.
  402. *
  403. * @return Drawing\Shadow
  404. */
  405. public function getShadow()
  406. {
  407. return $this->shadow;
  408. }
  409. /**
  410. * Set Shadow.
  411. *
  412. * @param Drawing\Shadow $pValue
  413. *
  414. * @return $this
  415. */
  416. public function setShadow(?Drawing\Shadow $pValue = null)
  417. {
  418. $this->shadow = $pValue;
  419. return $this;
  420. }
  421. /**
  422. * Get hash code.
  423. *
  424. * @return string Hash code
  425. */
  426. public function getHashCode()
  427. {
  428. return md5(
  429. $this->name .
  430. $this->description .
  431. $this->worksheet->getHashCode() .
  432. $this->coordinates .
  433. $this->offsetX .
  434. $this->offsetY .
  435. $this->width .
  436. $this->height .
  437. $this->rotation .
  438. $this->shadow->getHashCode() .
  439. __CLASS__
  440. );
  441. }
  442. /**
  443. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  444. */
  445. public function __clone()
  446. {
  447. $vars = get_object_vars($this);
  448. foreach ($vars as $key => $value) {
  449. if ($key == 'worksheet') {
  450. $this->worksheet = null;
  451. } elseif (is_object($value)) {
  452. $this->$key = clone $value;
  453. } else {
  454. $this->$key = $value;
  455. }
  456. }
  457. }
  458. public function setHyperlink(?Hyperlink $pHyperlink = null): void
  459. {
  460. $this->hyperlink = $pHyperlink;
  461. }
  462. /**
  463. * @return null|Hyperlink
  464. */
  465. public function getHyperlink()
  466. {
  467. return $this->hyperlink;
  468. }
  469. }