Comment.php 5.6 KB

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