1
0

Comment.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Comments;
  26. use OCP\Comments\IComment;
  27. use OCP\Comments\IllegalIDChangeException;
  28. use OCP\Comments\MessageTooLongException;
  29. class Comment implements IComment {
  30. protected $data = [
  31. 'id' => '',
  32. 'parentId' => '0',
  33. 'topmostParentId' => '0',
  34. 'childrenCount' => '0',
  35. 'message' => '',
  36. 'verb' => '',
  37. 'actorType' => '',
  38. 'actorId' => '',
  39. 'objectType' => '',
  40. 'objectId' => '',
  41. 'creationDT' => null,
  42. 'latestChildDT' => null,
  43. ];
  44. /**
  45. * Comment constructor.
  46. *
  47. * @param array $data optional, array with keys according to column names from
  48. * the comments database scheme
  49. */
  50. public function __construct(array $data = null) {
  51. if(is_array($data)) {
  52. $this->fromArray($data);
  53. }
  54. }
  55. /**
  56. * returns the ID of the comment
  57. *
  58. * It may return an empty string, if the comment was not stored.
  59. * It is expected that the concrete Comment implementation gives an ID
  60. * by itself (e.g. after saving).
  61. *
  62. * @return string
  63. * @since 9.0.0
  64. */
  65. public function getId() {
  66. return $this->data['id'];
  67. }
  68. /**
  69. * sets the ID of the comment and returns itself
  70. *
  71. * It is only allowed to set the ID only, if the current id is an empty
  72. * string (which means it is not stored in a database, storage or whatever
  73. * the concrete implementation does), or vice versa. Changing a given ID is
  74. * not permitted and must result in an IllegalIDChangeException.
  75. *
  76. * @param string $id
  77. * @return IComment
  78. * @throws IllegalIDChangeException
  79. * @since 9.0.0
  80. */
  81. public function setId($id) {
  82. if(!is_string($id)) {
  83. throw new \InvalidArgumentException('String expected.');
  84. }
  85. $id = trim($id);
  86. if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) {
  87. $this->data['id'] = $id;
  88. return $this;
  89. }
  90. throw new IllegalIDChangeException('Not allowed to assign a new ID to an already saved comment.');
  91. }
  92. /**
  93. * returns the parent ID of the comment
  94. *
  95. * @return string
  96. * @since 9.0.0
  97. */
  98. public function getParentId() {
  99. return $this->data['parentId'];
  100. }
  101. /**
  102. * sets the parent ID and returns itself
  103. *
  104. * @param string $parentId
  105. * @return IComment
  106. * @since 9.0.0
  107. */
  108. public function setParentId($parentId) {
  109. if(!is_string($parentId)) {
  110. throw new \InvalidArgumentException('String expected.');
  111. }
  112. $this->data['parentId'] = trim($parentId);
  113. return $this;
  114. }
  115. /**
  116. * returns the topmost parent ID of the comment
  117. *
  118. * @return string
  119. * @since 9.0.0
  120. */
  121. public function getTopmostParentId() {
  122. return $this->data['topmostParentId'];
  123. }
  124. /**
  125. * sets the topmost parent ID and returns itself
  126. *
  127. * @param string $id
  128. * @return IComment
  129. * @since 9.0.0
  130. */
  131. public function setTopmostParentId($id) {
  132. if(!is_string($id)) {
  133. throw new \InvalidArgumentException('String expected.');
  134. }
  135. $this->data['topmostParentId'] = trim($id);
  136. return $this;
  137. }
  138. /**
  139. * returns the number of children
  140. *
  141. * @return int
  142. * @since 9.0.0
  143. */
  144. public function getChildrenCount() {
  145. return $this->data['childrenCount'];
  146. }
  147. /**
  148. * sets the number of children
  149. *
  150. * @param int $count
  151. * @return IComment
  152. * @since 9.0.0
  153. */
  154. public function setChildrenCount($count) {
  155. if(!is_int($count)) {
  156. throw new \InvalidArgumentException('Integer expected.');
  157. }
  158. $this->data['childrenCount'] = $count;
  159. return $this;
  160. }
  161. /**
  162. * returns the message of the comment
  163. *
  164. * @return string
  165. * @since 9.0.0
  166. */
  167. public function getMessage() {
  168. return $this->data['message'];
  169. }
  170. /**
  171. * sets the message of the comment and returns itself
  172. *
  173. * @param string $message
  174. * @return IComment
  175. * @throws MessageTooLongException
  176. * @since 9.0.0
  177. */
  178. public function setMessage($message) {
  179. if(!is_string($message)) {
  180. throw new \InvalidArgumentException('String expected.');
  181. }
  182. $message = trim($message);
  183. if(mb_strlen($message, 'UTF-8') > IComment::MAX_MESSAGE_LENGTH) {
  184. throw new MessageTooLongException('Comment message must not exceed ' . IComment::MAX_MESSAGE_LENGTH . ' characters');
  185. }
  186. $this->data['message'] = $message;
  187. return $this;
  188. }
  189. /**
  190. * returns an array containing mentions that are included in the comment
  191. *
  192. * @return array each mention provides a 'type' and an 'id', see example below
  193. * @since 11.0.0
  194. *
  195. * The return array looks like:
  196. * [
  197. * [
  198. * 'type' => 'user',
  199. * 'id' => 'citizen4'
  200. * ],
  201. * [
  202. * 'type' => 'group',
  203. * 'id' => 'media'
  204. * ],
  205. * …
  206. * ]
  207. *
  208. */
  209. public function getMentions() {
  210. $ok = preg_match_all('/\B@[a-z0-9_\-@\.\']+/i', $this->getMessage(), $mentions);
  211. if(!$ok || !isset($mentions[0]) || !is_array($mentions[0])) {
  212. return [];
  213. }
  214. $uids = array_unique($mentions[0]);
  215. $result = [];
  216. foreach ($uids as $uid) {
  217. $result[] = ['type' => 'user', 'id' => substr($uid, 1)];
  218. }
  219. return $result;
  220. }
  221. /**
  222. * returns the verb of the comment
  223. *
  224. * @return string
  225. * @since 9.0.0
  226. */
  227. public function getVerb() {
  228. return $this->data['verb'];
  229. }
  230. /**
  231. * sets the verb of the comment, e.g. 'comment' or 'like'
  232. *
  233. * @param string $verb
  234. * @return IComment
  235. * @since 9.0.0
  236. */
  237. public function setVerb($verb) {
  238. if(!is_string($verb) || !trim($verb)) {
  239. throw new \InvalidArgumentException('Non-empty String expected.');
  240. }
  241. $this->data['verb'] = trim($verb);
  242. return $this;
  243. }
  244. /**
  245. * returns the actor type
  246. *
  247. * @return string
  248. * @since 9.0.0
  249. */
  250. public function getActorType() {
  251. return $this->data['actorType'];
  252. }
  253. /**
  254. * returns the actor ID
  255. *
  256. * @return string
  257. * @since 9.0.0
  258. */
  259. public function getActorId() {
  260. return $this->data['actorId'];
  261. }
  262. /**
  263. * sets (overwrites) the actor type and id
  264. *
  265. * @param string $actorType e.g. 'users'
  266. * @param string $actorId e.g. 'zombie234'
  267. * @return IComment
  268. * @since 9.0.0
  269. */
  270. public function setActor($actorType, $actorId) {
  271. if(
  272. !is_string($actorType) || !trim($actorType)
  273. || !is_string($actorId) || !trim($actorId)
  274. ) {
  275. throw new \InvalidArgumentException('String expected.');
  276. }
  277. $this->data['actorType'] = trim($actorType);
  278. $this->data['actorId'] = trim($actorId);
  279. return $this;
  280. }
  281. /**
  282. * returns the creation date of the comment.
  283. *
  284. * If not explicitly set, it shall default to the time of initialization.
  285. *
  286. * @return \DateTime
  287. * @since 9.0.0
  288. */
  289. public function getCreationDateTime() {
  290. return $this->data['creationDT'];
  291. }
  292. /**
  293. * sets the creation date of the comment and returns itself
  294. *
  295. * @param \DateTime $timestamp
  296. * @return IComment
  297. * @since 9.0.0
  298. */
  299. public function setCreationDateTime(\DateTime $timestamp) {
  300. $this->data['creationDT'] = $timestamp;
  301. return $this;
  302. }
  303. /**
  304. * returns the DateTime of the most recent child, if set, otherwise null
  305. *
  306. * @return \DateTime|null
  307. * @since 9.0.0
  308. */
  309. public function getLatestChildDateTime() {
  310. return $this->data['latestChildDT'];
  311. }
  312. /**
  313. * sets the date of the most recent child
  314. *
  315. * @param \DateTime $dateTime
  316. * @return IComment
  317. * @since 9.0.0
  318. */
  319. public function setLatestChildDateTime(\DateTime $dateTime = null) {
  320. $this->data['latestChildDT'] = $dateTime;
  321. return $this;
  322. }
  323. /**
  324. * returns the object type the comment is attached to
  325. *
  326. * @return string
  327. * @since 9.0.0
  328. */
  329. public function getObjectType() {
  330. return $this->data['objectType'];
  331. }
  332. /**
  333. * returns the object id the comment is attached to
  334. *
  335. * @return string
  336. * @since 9.0.0
  337. */
  338. public function getObjectId() {
  339. return $this->data['objectId'];
  340. }
  341. /**
  342. * sets (overwrites) the object of the comment
  343. *
  344. * @param string $objectType e.g. 'files'
  345. * @param string $objectId e.g. '16435'
  346. * @return IComment
  347. * @since 9.0.0
  348. */
  349. public function setObject($objectType, $objectId) {
  350. if(
  351. !is_string($objectType) || !trim($objectType)
  352. || !is_string($objectId) || !trim($objectId)
  353. ) {
  354. throw new \InvalidArgumentException('String expected.');
  355. }
  356. $this->data['objectType'] = trim($objectType);
  357. $this->data['objectId'] = trim($objectId);
  358. return $this;
  359. }
  360. /**
  361. * sets the comment data based on an array with keys as taken from the
  362. * database.
  363. *
  364. * @param array $data
  365. * @return IComment
  366. */
  367. protected function fromArray($data) {
  368. foreach(array_keys($data) as $key) {
  369. // translate DB keys to internal setter names
  370. $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key)));
  371. $setter = str_replace('Timestamp', 'DateTime', $setter);
  372. if(method_exists($this, $setter)) {
  373. $this->$setter($data[$key]);
  374. }
  375. }
  376. foreach(['actor', 'object'] as $role) {
  377. if(isset($data[$role . '_type']) && isset($data[$role . '_id'])) {
  378. $setter = 'set' . ucfirst($role);
  379. $this->$setter($data[$role . '_type'], $data[$role . '_id']);
  380. }
  381. }
  382. return $this;
  383. }
  384. }