Comment.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet;
  3. use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
  4. use PhpOffice\PhpSpreadsheet\Helper\Size;
  5. use PhpOffice\PhpSpreadsheet\RichText\RichText;
  6. use PhpOffice\PhpSpreadsheet\Shared\Drawing as SharedDrawing;
  7. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  8. use PhpOffice\PhpSpreadsheet\Style\Color;
  9. use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
  10. class Comment implements IComparable
  11. {
  12. /**
  13. * Author.
  14. *
  15. * @var string
  16. */
  17. private $author;
  18. /**
  19. * Rich text comment.
  20. *
  21. * @var RichText
  22. */
  23. private $text;
  24. /**
  25. * Comment width (CSS style, i.e. XXpx or YYpt).
  26. *
  27. * @var string
  28. */
  29. private $width = '96pt';
  30. /**
  31. * Left margin (CSS style, i.e. XXpx or YYpt).
  32. *
  33. * @var string
  34. */
  35. private $marginLeft = '59.25pt';
  36. /**
  37. * Top margin (CSS style, i.e. XXpx or YYpt).
  38. *
  39. * @var string
  40. */
  41. private $marginTop = '1.5pt';
  42. /**
  43. * Visible.
  44. *
  45. * @var bool
  46. */
  47. private $visible = false;
  48. /**
  49. * Comment height (CSS style, i.e. XXpx or YYpt).
  50. *
  51. * @var string
  52. */
  53. private $height = '55.5pt';
  54. /**
  55. * Comment fill color.
  56. *
  57. * @var Color
  58. */
  59. private $fillColor;
  60. /**
  61. * Alignment.
  62. *
  63. * @var string
  64. */
  65. private $alignment;
  66. /**
  67. * Background image in comment.
  68. *
  69. * @var Drawing
  70. */
  71. private $backgroundImage;
  72. /**
  73. * Create a new Comment.
  74. */
  75. public function __construct()
  76. {
  77. // Initialise variables
  78. $this->author = 'Author';
  79. $this->text = new RichText();
  80. $this->fillColor = new Color('FFFFFFE1');
  81. $this->alignment = Alignment::HORIZONTAL_GENERAL;
  82. $this->backgroundImage = new Drawing();
  83. }
  84. /**
  85. * Get Author.
  86. */
  87. public function getAuthor(): string
  88. {
  89. return $this->author;
  90. }
  91. /**
  92. * Set Author.
  93. */
  94. public function setAuthor(string $author): self
  95. {
  96. $this->author = $author;
  97. return $this;
  98. }
  99. /**
  100. * Get Rich text comment.
  101. */
  102. public function getText(): RichText
  103. {
  104. return $this->text;
  105. }
  106. /**
  107. * Set Rich text comment.
  108. */
  109. public function setText(RichText $text): self
  110. {
  111. $this->text = $text;
  112. return $this;
  113. }
  114. /**
  115. * Get comment width (CSS style, i.e. XXpx or YYpt).
  116. */
  117. public function getWidth(): string
  118. {
  119. return $this->width;
  120. }
  121. /**
  122. * Set comment width (CSS style, i.e. XXpx or YYpt). Default unit is pt.
  123. */
  124. public function setWidth(string $width): self
  125. {
  126. $width = new Size($width);
  127. if ($width->valid()) {
  128. $this->width = (string) $width;
  129. }
  130. return $this;
  131. }
  132. /**
  133. * Get comment height (CSS style, i.e. XXpx or YYpt).
  134. */
  135. public function getHeight(): string
  136. {
  137. return $this->height;
  138. }
  139. /**
  140. * Set comment height (CSS style, i.e. XXpx or YYpt). Default unit is pt.
  141. */
  142. public function setHeight(string $height): self
  143. {
  144. $height = new Size($height);
  145. if ($height->valid()) {
  146. $this->height = (string) $height;
  147. }
  148. return $this;
  149. }
  150. /**
  151. * Get left margin (CSS style, i.e. XXpx or YYpt).
  152. */
  153. public function getMarginLeft(): string
  154. {
  155. return $this->marginLeft;
  156. }
  157. /**
  158. * Set left margin (CSS style, i.e. XXpx or YYpt). Default unit is pt.
  159. */
  160. public function setMarginLeft(string $margin): self
  161. {
  162. $margin = new Size($margin);
  163. if ($margin->valid()) {
  164. $this->marginLeft = (string) $margin;
  165. }
  166. return $this;
  167. }
  168. /**
  169. * Get top margin (CSS style, i.e. XXpx or YYpt).
  170. */
  171. public function getMarginTop(): string
  172. {
  173. return $this->marginTop;
  174. }
  175. /**
  176. * Set top margin (CSS style, i.e. XXpx or YYpt). Default unit is pt.
  177. */
  178. public function setMarginTop(string $margin): self
  179. {
  180. $margin = new Size($margin);
  181. if ($margin->valid()) {
  182. $this->marginTop = (string) $margin;
  183. }
  184. return $this;
  185. }
  186. /**
  187. * Is the comment visible by default?
  188. */
  189. public function getVisible(): bool
  190. {
  191. return $this->visible;
  192. }
  193. /**
  194. * Set comment default visibility.
  195. */
  196. public function setVisible(bool $visibility): self
  197. {
  198. $this->visible = $visibility;
  199. return $this;
  200. }
  201. /**
  202. * Set fill color.
  203. */
  204. public function setFillColor(Color $color): self
  205. {
  206. $this->fillColor = $color;
  207. return $this;
  208. }
  209. /**
  210. * Get fill color.
  211. */
  212. public function getFillColor(): Color
  213. {
  214. return $this->fillColor;
  215. }
  216. /**
  217. * Set Alignment.
  218. */
  219. public function setAlignment(string $alignment): self
  220. {
  221. $this->alignment = $alignment;
  222. return $this;
  223. }
  224. /**
  225. * Get Alignment.
  226. */
  227. public function getAlignment(): string
  228. {
  229. return $this->alignment;
  230. }
  231. /**
  232. * Get hash code.
  233. */
  234. public function getHashCode(): string
  235. {
  236. return md5(
  237. $this->author .
  238. $this->text->getHashCode() .
  239. $this->width .
  240. $this->height .
  241. $this->marginLeft .
  242. $this->marginTop .
  243. ($this->visible ? 1 : 0) .
  244. $this->fillColor->getHashCode() .
  245. $this->alignment .
  246. ($this->hasBackgroundImage() ? $this->backgroundImage->getHashCode() : '') .
  247. __CLASS__
  248. );
  249. }
  250. /**
  251. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  252. */
  253. public function __clone()
  254. {
  255. $vars = get_object_vars($this);
  256. foreach ($vars as $key => $value) {
  257. if (is_object($value)) {
  258. $this->$key = clone $value;
  259. } else {
  260. $this->$key = $value;
  261. }
  262. }
  263. }
  264. /**
  265. * Convert to string.
  266. */
  267. public function __toString(): string
  268. {
  269. return $this->text->getPlainText();
  270. }
  271. /**
  272. * Check is background image exists.
  273. */
  274. public function hasBackgroundImage(): bool
  275. {
  276. $path = $this->backgroundImage->getPath();
  277. if (empty($path)) {
  278. return false;
  279. }
  280. return getimagesize($path) !== false;
  281. }
  282. /**
  283. * Returns background image.
  284. */
  285. public function getBackgroundImage(): Drawing
  286. {
  287. return $this->backgroundImage;
  288. }
  289. /**
  290. * Sets background image.
  291. */
  292. public function setBackgroundImage(Drawing $objDrawing): self
  293. {
  294. if (!array_key_exists($objDrawing->getType(), Drawing::IMAGE_TYPES_CONVERTION_MAP)) {
  295. throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
  296. }
  297. $this->backgroundImage = $objDrawing;
  298. return $this;
  299. }
  300. /**
  301. * Sets size of comment as size of background image.
  302. */
  303. public function setSizeAsBackgroundImage(): self
  304. {
  305. if ($this->hasBackgroundImage()) {
  306. $this->setWidth(SharedDrawing::pixelsToPoints($this->backgroundImage->getWidth()) . 'pt');
  307. $this->setHeight(SharedDrawing::pixelsToPoints($this->backgroundImage->getHeight()) . 'pt');
  308. }
  309. return $this;
  310. }
  311. }