Comment.php 5.7 KB

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