@@ -32,234 +32,234 @@ |
||
| 32 | 32 | * @since 9.0.0 |
| 33 | 33 | */ |
| 34 | 34 | interface IComment { |
| 35 | - const MAX_MESSAGE_LENGTH = 1000; |
|
| 35 | + const MAX_MESSAGE_LENGTH = 1000; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * returns the ID of the comment |
|
| 39 | - * |
|
| 40 | - * It may return an empty string, if the comment was not stored. |
|
| 41 | - * It is expected that the concrete Comment implementation gives an ID |
|
| 42 | - * by itself (e.g. after saving). |
|
| 43 | - * |
|
| 44 | - * @return string |
|
| 45 | - * @since 9.0.0 |
|
| 46 | - */ |
|
| 47 | - public function getId(); |
|
| 37 | + /** |
|
| 38 | + * returns the ID of the comment |
|
| 39 | + * |
|
| 40 | + * It may return an empty string, if the comment was not stored. |
|
| 41 | + * It is expected that the concrete Comment implementation gives an ID |
|
| 42 | + * by itself (e.g. after saving). |
|
| 43 | + * |
|
| 44 | + * @return string |
|
| 45 | + * @since 9.0.0 |
|
| 46 | + */ |
|
| 47 | + public function getId(); |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * sets the ID of the comment and returns itself |
|
| 51 | - * |
|
| 52 | - * It is only allowed to set the ID only, if the current id is an empty |
|
| 53 | - * string (which means it is not stored in a database, storage or whatever |
|
| 54 | - * the concrete implementation does), or vice versa. Changing a given ID is |
|
| 55 | - * not permitted and must result in an IllegalIDChangeException. |
|
| 56 | - * |
|
| 57 | - * @param string $id |
|
| 58 | - * @return IComment |
|
| 59 | - * @throws IllegalIDChangeException |
|
| 60 | - * @since 9.0.0 |
|
| 61 | - */ |
|
| 62 | - public function setId($id); |
|
| 49 | + /** |
|
| 50 | + * sets the ID of the comment and returns itself |
|
| 51 | + * |
|
| 52 | + * It is only allowed to set the ID only, if the current id is an empty |
|
| 53 | + * string (which means it is not stored in a database, storage or whatever |
|
| 54 | + * the concrete implementation does), or vice versa. Changing a given ID is |
|
| 55 | + * not permitted and must result in an IllegalIDChangeException. |
|
| 56 | + * |
|
| 57 | + * @param string $id |
|
| 58 | + * @return IComment |
|
| 59 | + * @throws IllegalIDChangeException |
|
| 60 | + * @since 9.0.0 |
|
| 61 | + */ |
|
| 62 | + public function setId($id); |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * returns the parent ID of the comment |
|
| 66 | - * |
|
| 67 | - * @return string |
|
| 68 | - * @since 9.0.0 |
|
| 69 | - */ |
|
| 70 | - public function getParentId(); |
|
| 64 | + /** |
|
| 65 | + * returns the parent ID of the comment |
|
| 66 | + * |
|
| 67 | + * @return string |
|
| 68 | + * @since 9.0.0 |
|
| 69 | + */ |
|
| 70 | + public function getParentId(); |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * sets the parent ID and returns itself |
|
| 74 | - * @param string $parentId |
|
| 75 | - * @return IComment |
|
| 76 | - * @since 9.0.0 |
|
| 77 | - */ |
|
| 78 | - public function setParentId($parentId); |
|
| 72 | + /** |
|
| 73 | + * sets the parent ID and returns itself |
|
| 74 | + * @param string $parentId |
|
| 75 | + * @return IComment |
|
| 76 | + * @since 9.0.0 |
|
| 77 | + */ |
|
| 78 | + public function setParentId($parentId); |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * returns the topmost parent ID of the comment |
|
| 82 | - * |
|
| 83 | - * @return string |
|
| 84 | - * @since 9.0.0 |
|
| 85 | - */ |
|
| 86 | - public function getTopmostParentId(); |
|
| 80 | + /** |
|
| 81 | + * returns the topmost parent ID of the comment |
|
| 82 | + * |
|
| 83 | + * @return string |
|
| 84 | + * @since 9.0.0 |
|
| 85 | + */ |
|
| 86 | + public function getTopmostParentId(); |
|
| 87 | 87 | |
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * sets the topmost parent ID and returns itself |
|
| 91 | - * |
|
| 92 | - * @param string $id |
|
| 93 | - * @return IComment |
|
| 94 | - * @since 9.0.0 |
|
| 95 | - */ |
|
| 96 | - public function setTopmostParentId($id); |
|
| 89 | + /** |
|
| 90 | + * sets the topmost parent ID and returns itself |
|
| 91 | + * |
|
| 92 | + * @param string $id |
|
| 93 | + * @return IComment |
|
| 94 | + * @since 9.0.0 |
|
| 95 | + */ |
|
| 96 | + public function setTopmostParentId($id); |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * returns the number of children |
|
| 100 | - * |
|
| 101 | - * @return int |
|
| 102 | - * @since 9.0.0 |
|
| 103 | - */ |
|
| 104 | - public function getChildrenCount(); |
|
| 98 | + /** |
|
| 99 | + * returns the number of children |
|
| 100 | + * |
|
| 101 | + * @return int |
|
| 102 | + * @since 9.0.0 |
|
| 103 | + */ |
|
| 104 | + public function getChildrenCount(); |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * sets the number of children |
|
| 108 | - * |
|
| 109 | - * @param int $count |
|
| 110 | - * @return IComment |
|
| 111 | - * @since 9.0.0 |
|
| 112 | - */ |
|
| 113 | - public function setChildrenCount($count); |
|
| 106 | + /** |
|
| 107 | + * sets the number of children |
|
| 108 | + * |
|
| 109 | + * @param int $count |
|
| 110 | + * @return IComment |
|
| 111 | + * @since 9.0.0 |
|
| 112 | + */ |
|
| 113 | + public function setChildrenCount($count); |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * returns the message of the comment |
|
| 117 | - * |
|
| 118 | - * @return string |
|
| 119 | - * @since 9.0.0 |
|
| 120 | - */ |
|
| 121 | - public function getMessage(); |
|
| 115 | + /** |
|
| 116 | + * returns the message of the comment |
|
| 117 | + * |
|
| 118 | + * @return string |
|
| 119 | + * @since 9.0.0 |
|
| 120 | + */ |
|
| 121 | + public function getMessage(); |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * sets the message of the comment and returns itself |
|
| 125 | - * |
|
| 126 | - * When the given message length exceeds MAX_MESSAGE_LENGTH an |
|
| 127 | - * MessageTooLongException shall be thrown. |
|
| 128 | - * |
|
| 129 | - * @param string $message |
|
| 130 | - * @param int $maxLength |
|
| 131 | - * @return IComment |
|
| 132 | - * @throws MessageTooLongException |
|
| 133 | - * @since 9.0.0 - $maxLength added in 16.0.2 |
|
| 134 | - */ |
|
| 135 | - public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH); |
|
| 123 | + /** |
|
| 124 | + * sets the message of the comment and returns itself |
|
| 125 | + * |
|
| 126 | + * When the given message length exceeds MAX_MESSAGE_LENGTH an |
|
| 127 | + * MessageTooLongException shall be thrown. |
|
| 128 | + * |
|
| 129 | + * @param string $message |
|
| 130 | + * @param int $maxLength |
|
| 131 | + * @return IComment |
|
| 132 | + * @throws MessageTooLongException |
|
| 133 | + * @since 9.0.0 - $maxLength added in 16.0.2 |
|
| 134 | + */ |
|
| 135 | + public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH); |
|
| 136 | 136 | |
| 137 | - /** |
|
| 138 | - * returns an array containing mentions that are included in the comment |
|
| 139 | - * |
|
| 140 | - * @return array each mention provides a 'type' and an 'id', see example below |
|
| 141 | - * @since 11.0.0 |
|
| 142 | - * |
|
| 143 | - * The return array looks like: |
|
| 144 | - * [ |
|
| 145 | - * [ |
|
| 146 | - * 'type' => 'user', |
|
| 147 | - * 'id' => 'citizen4' |
|
| 148 | - * ], |
|
| 149 | - * [ |
|
| 150 | - * 'type' => 'group', |
|
| 151 | - * 'id' => 'media' |
|
| 152 | - * ], |
|
| 153 | - * … |
|
| 154 | - * ] |
|
| 155 | - * |
|
| 156 | - */ |
|
| 157 | - public function getMentions(); |
|
| 137 | + /** |
|
| 138 | + * returns an array containing mentions that are included in the comment |
|
| 139 | + * |
|
| 140 | + * @return array each mention provides a 'type' and an 'id', see example below |
|
| 141 | + * @since 11.0.0 |
|
| 142 | + * |
|
| 143 | + * The return array looks like: |
|
| 144 | + * [ |
|
| 145 | + * [ |
|
| 146 | + * 'type' => 'user', |
|
| 147 | + * 'id' => 'citizen4' |
|
| 148 | + * ], |
|
| 149 | + * [ |
|
| 150 | + * 'type' => 'group', |
|
| 151 | + * 'id' => 'media' |
|
| 152 | + * ], |
|
| 153 | + * … |
|
| 154 | + * ] |
|
| 155 | + * |
|
| 156 | + */ |
|
| 157 | + public function getMentions(); |
|
| 158 | 158 | |
| 159 | - /** |
|
| 160 | - * returns the verb of the comment |
|
| 161 | - * |
|
| 162 | - * @return string |
|
| 163 | - * @since 9.0.0 |
|
| 164 | - */ |
|
| 165 | - public function getVerb(); |
|
| 159 | + /** |
|
| 160 | + * returns the verb of the comment |
|
| 161 | + * |
|
| 162 | + * @return string |
|
| 163 | + * @since 9.0.0 |
|
| 164 | + */ |
|
| 165 | + public function getVerb(); |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * sets the verb of the comment, e.g. 'comment' or 'like' |
|
| 169 | - * |
|
| 170 | - * @param string $verb |
|
| 171 | - * @return IComment |
|
| 172 | - * @since 9.0.0 |
|
| 173 | - */ |
|
| 174 | - public function setVerb($verb); |
|
| 167 | + /** |
|
| 168 | + * sets the verb of the comment, e.g. 'comment' or 'like' |
|
| 169 | + * |
|
| 170 | + * @param string $verb |
|
| 171 | + * @return IComment |
|
| 172 | + * @since 9.0.0 |
|
| 173 | + */ |
|
| 174 | + public function setVerb($verb); |
|
| 175 | 175 | |
| 176 | - /** |
|
| 177 | - * returns the actor type |
|
| 178 | - * |
|
| 179 | - * @return string |
|
| 180 | - * @since 9.0.0 |
|
| 181 | - */ |
|
| 182 | - public function getActorType(); |
|
| 176 | + /** |
|
| 177 | + * returns the actor type |
|
| 178 | + * |
|
| 179 | + * @return string |
|
| 180 | + * @since 9.0.0 |
|
| 181 | + */ |
|
| 182 | + public function getActorType(); |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * returns the actor ID |
|
| 186 | - * |
|
| 187 | - * @return string |
|
| 188 | - * @since 9.0.0 |
|
| 189 | - */ |
|
| 190 | - public function getActorId(); |
|
| 184 | + /** |
|
| 185 | + * returns the actor ID |
|
| 186 | + * |
|
| 187 | + * @return string |
|
| 188 | + * @since 9.0.0 |
|
| 189 | + */ |
|
| 190 | + public function getActorId(); |
|
| 191 | 191 | |
| 192 | - /** |
|
| 193 | - * sets (overwrites) the actor type and id |
|
| 194 | - * |
|
| 195 | - * @param string $actorType e.g. 'users' |
|
| 196 | - * @param string $actorId e.g. 'zombie234' |
|
| 197 | - * @return IComment |
|
| 198 | - * @since 9.0.0 |
|
| 199 | - */ |
|
| 200 | - public function setActor($actorType, $actorId); |
|
| 192 | + /** |
|
| 193 | + * sets (overwrites) the actor type and id |
|
| 194 | + * |
|
| 195 | + * @param string $actorType e.g. 'users' |
|
| 196 | + * @param string $actorId e.g. 'zombie234' |
|
| 197 | + * @return IComment |
|
| 198 | + * @since 9.0.0 |
|
| 199 | + */ |
|
| 200 | + public function setActor($actorType, $actorId); |
|
| 201 | 201 | |
| 202 | - /** |
|
| 203 | - * returns the creation date of the comment. |
|
| 204 | - * |
|
| 205 | - * If not explicitly set, it shall default to the time of initialization. |
|
| 206 | - * |
|
| 207 | - * @return \DateTime |
|
| 208 | - * @since 9.0.0 |
|
| 209 | - */ |
|
| 210 | - public function getCreationDateTime(); |
|
| 202 | + /** |
|
| 203 | + * returns the creation date of the comment. |
|
| 204 | + * |
|
| 205 | + * If not explicitly set, it shall default to the time of initialization. |
|
| 206 | + * |
|
| 207 | + * @return \DateTime |
|
| 208 | + * @since 9.0.0 |
|
| 209 | + */ |
|
| 210 | + public function getCreationDateTime(); |
|
| 211 | 211 | |
| 212 | - /** |
|
| 213 | - * sets the creation date of the comment and returns itself |
|
| 214 | - * |
|
| 215 | - * @param \DateTime $dateTime |
|
| 216 | - * @return IComment |
|
| 217 | - * @since 9.0.0 |
|
| 218 | - */ |
|
| 219 | - public function setCreationDateTime(\DateTime $dateTime); |
|
| 212 | + /** |
|
| 213 | + * sets the creation date of the comment and returns itself |
|
| 214 | + * |
|
| 215 | + * @param \DateTime $dateTime |
|
| 216 | + * @return IComment |
|
| 217 | + * @since 9.0.0 |
|
| 218 | + */ |
|
| 219 | + public function setCreationDateTime(\DateTime $dateTime); |
|
| 220 | 220 | |
| 221 | - /** |
|
| 222 | - * returns the date of the most recent child |
|
| 223 | - * |
|
| 224 | - * @return \DateTime |
|
| 225 | - * @since 9.0.0 |
|
| 226 | - */ |
|
| 227 | - public function getLatestChildDateTime(); |
|
| 221 | + /** |
|
| 222 | + * returns the date of the most recent child |
|
| 223 | + * |
|
| 224 | + * @return \DateTime |
|
| 225 | + * @since 9.0.0 |
|
| 226 | + */ |
|
| 227 | + public function getLatestChildDateTime(); |
|
| 228 | 228 | |
| 229 | - /** |
|
| 230 | - * sets the date of the most recent child |
|
| 231 | - * |
|
| 232 | - * @param \DateTime $dateTime |
|
| 233 | - * @return IComment |
|
| 234 | - * @since 9.0.0 |
|
| 235 | - */ |
|
| 236 | - public function setLatestChildDateTime(\DateTime $dateTime); |
|
| 229 | + /** |
|
| 230 | + * sets the date of the most recent child |
|
| 231 | + * |
|
| 232 | + * @param \DateTime $dateTime |
|
| 233 | + * @return IComment |
|
| 234 | + * @since 9.0.0 |
|
| 235 | + */ |
|
| 236 | + public function setLatestChildDateTime(\DateTime $dateTime); |
|
| 237 | 237 | |
| 238 | - /** |
|
| 239 | - * returns the object type the comment is attached to |
|
| 240 | - * |
|
| 241 | - * @return string |
|
| 242 | - * @since 9.0.0 |
|
| 243 | - */ |
|
| 244 | - public function getObjectType(); |
|
| 238 | + /** |
|
| 239 | + * returns the object type the comment is attached to |
|
| 240 | + * |
|
| 241 | + * @return string |
|
| 242 | + * @since 9.0.0 |
|
| 243 | + */ |
|
| 244 | + public function getObjectType(); |
|
| 245 | 245 | |
| 246 | - /** |
|
| 247 | - * returns the object id the comment is attached to |
|
| 248 | - * |
|
| 249 | - * @return string |
|
| 250 | - * @since 9.0.0 |
|
| 251 | - */ |
|
| 252 | - public function getObjectId(); |
|
| 246 | + /** |
|
| 247 | + * returns the object id the comment is attached to |
|
| 248 | + * |
|
| 249 | + * @return string |
|
| 250 | + * @since 9.0.0 |
|
| 251 | + */ |
|
| 252 | + public function getObjectId(); |
|
| 253 | 253 | |
| 254 | - /** |
|
| 255 | - * sets (overwrites) the object of the comment |
|
| 256 | - * |
|
| 257 | - * @param string $objectType e.g. 'files' |
|
| 258 | - * @param string $objectId e.g. '16435' |
|
| 259 | - * @return IComment |
|
| 260 | - * @since 9.0.0 |
|
| 261 | - */ |
|
| 262 | - public function setObject($objectType, $objectId); |
|
| 254 | + /** |
|
| 255 | + * sets (overwrites) the object of the comment |
|
| 256 | + * |
|
| 257 | + * @param string $objectType e.g. 'files' |
|
| 258 | + * @param string $objectId e.g. '16435' |
|
| 259 | + * @return IComment |
|
| 260 | + * @since 9.0.0 |
|
| 261 | + */ |
|
| 262 | + public function setObject($objectType, $objectId); |
|
| 263 | 263 | |
| 264 | 264 | } |
| 265 | 265 | |
@@ -30,390 +30,390 @@ |
||
| 30 | 30 | |
| 31 | 31 | class Comment implements IComment { |
| 32 | 32 | |
| 33 | - protected $data = [ |
|
| 34 | - 'id' => '', |
|
| 35 | - 'parentId' => '0', |
|
| 36 | - 'topmostParentId' => '0', |
|
| 37 | - 'childrenCount' => '0', |
|
| 38 | - 'message' => '', |
|
| 39 | - 'verb' => '', |
|
| 40 | - 'actorType' => '', |
|
| 41 | - 'actorId' => '', |
|
| 42 | - 'objectType' => '', |
|
| 43 | - 'objectId' => '', |
|
| 44 | - 'creationDT' => null, |
|
| 45 | - 'latestChildDT' => null, |
|
| 46 | - ]; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Comment constructor. |
|
| 50 | - * |
|
| 51 | - * @param array $data optional, array with keys according to column names from |
|
| 52 | - * the comments database scheme |
|
| 53 | - */ |
|
| 54 | - public function __construct(array $data = null) { |
|
| 55 | - if(is_array($data)) { |
|
| 56 | - $this->fromArray($data); |
|
| 57 | - } |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * returns the ID of the comment |
|
| 62 | - * |
|
| 63 | - * It may return an empty string, if the comment was not stored. |
|
| 64 | - * It is expected that the concrete Comment implementation gives an ID |
|
| 65 | - * by itself (e.g. after saving). |
|
| 66 | - * |
|
| 67 | - * @return string |
|
| 68 | - * @since 9.0.0 |
|
| 69 | - */ |
|
| 70 | - public function getId() { |
|
| 71 | - return $this->data['id']; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * sets the ID of the comment and returns itself |
|
| 76 | - * |
|
| 77 | - * It is only allowed to set the ID only, if the current id is an empty |
|
| 78 | - * string (which means it is not stored in a database, storage or whatever |
|
| 79 | - * the concrete implementation does), or vice versa. Changing a given ID is |
|
| 80 | - * not permitted and must result in an IllegalIDChangeException. |
|
| 81 | - * |
|
| 82 | - * @param string $id |
|
| 83 | - * @return IComment |
|
| 84 | - * @throws IllegalIDChangeException |
|
| 85 | - * @since 9.0.0 |
|
| 86 | - */ |
|
| 87 | - public function setId($id) { |
|
| 88 | - if(!is_string($id)) { |
|
| 89 | - throw new \InvalidArgumentException('String expected.'); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $id = trim($id); |
|
| 93 | - if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
| 94 | - $this->data['id'] = $id; |
|
| 95 | - return $this; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - throw new IllegalIDChangeException('Not allowed to assign a new ID to an already saved comment.'); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * returns the parent ID of the comment |
|
| 103 | - * |
|
| 104 | - * @return string |
|
| 105 | - * @since 9.0.0 |
|
| 106 | - */ |
|
| 107 | - public function getParentId() { |
|
| 108 | - return $this->data['parentId']; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * sets the parent ID and returns itself |
|
| 113 | - * |
|
| 114 | - * @param string $parentId |
|
| 115 | - * @return IComment |
|
| 116 | - * @since 9.0.0 |
|
| 117 | - */ |
|
| 118 | - public function setParentId($parentId) { |
|
| 119 | - if(!is_string($parentId)) { |
|
| 120 | - throw new \InvalidArgumentException('String expected.'); |
|
| 121 | - } |
|
| 122 | - $this->data['parentId'] = trim($parentId); |
|
| 123 | - return $this; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * returns the topmost parent ID of the comment |
|
| 128 | - * |
|
| 129 | - * @return string |
|
| 130 | - * @since 9.0.0 |
|
| 131 | - */ |
|
| 132 | - public function getTopmostParentId() { |
|
| 133 | - return $this->data['topmostParentId']; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * sets the topmost parent ID and returns itself |
|
| 139 | - * |
|
| 140 | - * @param string $id |
|
| 141 | - * @return IComment |
|
| 142 | - * @since 9.0.0 |
|
| 143 | - */ |
|
| 144 | - public function setTopmostParentId($id) { |
|
| 145 | - if(!is_string($id)) { |
|
| 146 | - throw new \InvalidArgumentException('String expected.'); |
|
| 147 | - } |
|
| 148 | - $this->data['topmostParentId'] = trim($id); |
|
| 149 | - return $this; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * returns the number of children |
|
| 154 | - * |
|
| 155 | - * @return int |
|
| 156 | - * @since 9.0.0 |
|
| 157 | - */ |
|
| 158 | - public function getChildrenCount() { |
|
| 159 | - return $this->data['childrenCount']; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * sets the number of children |
|
| 164 | - * |
|
| 165 | - * @param int $count |
|
| 166 | - * @return IComment |
|
| 167 | - * @since 9.0.0 |
|
| 168 | - */ |
|
| 169 | - public function setChildrenCount($count) { |
|
| 170 | - if(!is_int($count)) { |
|
| 171 | - throw new \InvalidArgumentException('Integer expected.'); |
|
| 172 | - } |
|
| 173 | - $this->data['childrenCount'] = $count; |
|
| 174 | - return $this; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * returns the message of the comment |
|
| 179 | - * |
|
| 180 | - * @return string |
|
| 181 | - * @since 9.0.0 |
|
| 182 | - */ |
|
| 183 | - public function getMessage() { |
|
| 184 | - return $this->data['message']; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * sets the message of the comment and returns itself |
|
| 189 | - * |
|
| 190 | - * @param string $message |
|
| 191 | - * @param int $maxLength |
|
| 192 | - * @return IComment |
|
| 193 | - * @throws MessageTooLongException |
|
| 194 | - * @since 9.0.0 |
|
| 195 | - */ |
|
| 196 | - public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH) { |
|
| 197 | - if(!is_string($message)) { |
|
| 198 | - throw new \InvalidArgumentException('String expected.'); |
|
| 199 | - } |
|
| 200 | - $message = trim($message); |
|
| 201 | - if ($maxLength && mb_strlen($message, 'UTF-8') > $maxLength) { |
|
| 202 | - throw new MessageTooLongException('Comment message must not exceed ' . $maxLength. ' characters'); |
|
| 203 | - } |
|
| 204 | - $this->data['message'] = $message; |
|
| 205 | - return $this; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * returns an array containing mentions that are included in the comment |
|
| 210 | - * |
|
| 211 | - * @return array each mention provides a 'type' and an 'id', see example below |
|
| 212 | - * @since 11.0.0 |
|
| 213 | - * |
|
| 214 | - * The return array looks like: |
|
| 215 | - * [ |
|
| 216 | - * [ |
|
| 217 | - * 'type' => 'user', |
|
| 218 | - * 'id' => 'citizen4' |
|
| 219 | - * ], |
|
| 220 | - * [ |
|
| 221 | - * 'type' => 'group', |
|
| 222 | - * 'id' => 'media' |
|
| 223 | - * ], |
|
| 224 | - * … |
|
| 225 | - * ] |
|
| 226 | - * |
|
| 227 | - */ |
|
| 228 | - public function getMentions() { |
|
| 229 | - $ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions); |
|
| 230 | - if(!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { |
|
| 231 | - return []; |
|
| 232 | - } |
|
| 233 | - $uids = array_unique($mentions[0]); |
|
| 234 | - $result = []; |
|
| 235 | - foreach ($uids as $uid) { |
|
| 236 | - $result[] = ['type' => 'user', 'id' => trim(substr($uid, 1), '"')]; |
|
| 237 | - } |
|
| 238 | - return $result; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * returns the verb of the comment |
|
| 243 | - * |
|
| 244 | - * @return string |
|
| 245 | - * @since 9.0.0 |
|
| 246 | - */ |
|
| 247 | - public function getVerb() { |
|
| 248 | - return $this->data['verb']; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * sets the verb of the comment, e.g. 'comment' or 'like' |
|
| 253 | - * |
|
| 254 | - * @param string $verb |
|
| 255 | - * @return IComment |
|
| 256 | - * @since 9.0.0 |
|
| 257 | - */ |
|
| 258 | - public function setVerb($verb) { |
|
| 259 | - if(!is_string($verb) || !trim($verb)) { |
|
| 260 | - throw new \InvalidArgumentException('Non-empty String expected.'); |
|
| 261 | - } |
|
| 262 | - $this->data['verb'] = trim($verb); |
|
| 263 | - return $this; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * returns the actor type |
|
| 268 | - * |
|
| 269 | - * @return string |
|
| 270 | - * @since 9.0.0 |
|
| 271 | - */ |
|
| 272 | - public function getActorType() { |
|
| 273 | - return $this->data['actorType']; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * returns the actor ID |
|
| 278 | - * |
|
| 279 | - * @return string |
|
| 280 | - * @since 9.0.0 |
|
| 281 | - */ |
|
| 282 | - public function getActorId() { |
|
| 283 | - return $this->data['actorId']; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * sets (overwrites) the actor type and id |
|
| 288 | - * |
|
| 289 | - * @param string $actorType e.g. 'users' |
|
| 290 | - * @param string $actorId e.g. 'zombie234' |
|
| 291 | - * @return IComment |
|
| 292 | - * @since 9.0.0 |
|
| 293 | - */ |
|
| 294 | - public function setActor($actorType, $actorId) { |
|
| 295 | - if( |
|
| 296 | - !is_string($actorType) || !trim($actorType) |
|
| 297 | - || !is_string($actorId) || !trim($actorId) |
|
| 298 | - ) { |
|
| 299 | - throw new \InvalidArgumentException('String expected.'); |
|
| 300 | - } |
|
| 301 | - $this->data['actorType'] = trim($actorType); |
|
| 302 | - $this->data['actorId'] = trim($actorId); |
|
| 303 | - return $this; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * returns the creation date of the comment. |
|
| 308 | - * |
|
| 309 | - * If not explicitly set, it shall default to the time of initialization. |
|
| 310 | - * |
|
| 311 | - * @return \DateTime |
|
| 312 | - * @since 9.0.0 |
|
| 313 | - */ |
|
| 314 | - public function getCreationDateTime() { |
|
| 315 | - return $this->data['creationDT']; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * sets the creation date of the comment and returns itself |
|
| 320 | - * |
|
| 321 | - * @param \DateTime $timestamp |
|
| 322 | - * @return IComment |
|
| 323 | - * @since 9.0.0 |
|
| 324 | - */ |
|
| 325 | - public function setCreationDateTime(\DateTime $timestamp) { |
|
| 326 | - $this->data['creationDT'] = $timestamp; |
|
| 327 | - return $this; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * returns the DateTime of the most recent child, if set, otherwise null |
|
| 332 | - * |
|
| 333 | - * @return \DateTime|null |
|
| 334 | - * @since 9.0.0 |
|
| 335 | - */ |
|
| 336 | - public function getLatestChildDateTime() { |
|
| 337 | - return $this->data['latestChildDT']; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * sets the date of the most recent child |
|
| 342 | - * |
|
| 343 | - * @param \DateTime $dateTime |
|
| 344 | - * @return IComment |
|
| 345 | - * @since 9.0.0 |
|
| 346 | - */ |
|
| 347 | - public function setLatestChildDateTime(\DateTime $dateTime = null) { |
|
| 348 | - $this->data['latestChildDT'] = $dateTime; |
|
| 349 | - return $this; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * returns the object type the comment is attached to |
|
| 354 | - * |
|
| 355 | - * @return string |
|
| 356 | - * @since 9.0.0 |
|
| 357 | - */ |
|
| 358 | - public function getObjectType() { |
|
| 359 | - return $this->data['objectType']; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * returns the object id the comment is attached to |
|
| 364 | - * |
|
| 365 | - * @return string |
|
| 366 | - * @since 9.0.0 |
|
| 367 | - */ |
|
| 368 | - public function getObjectId() { |
|
| 369 | - return $this->data['objectId']; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * sets (overwrites) the object of the comment |
|
| 374 | - * |
|
| 375 | - * @param string $objectType e.g. 'files' |
|
| 376 | - * @param string $objectId e.g. '16435' |
|
| 377 | - * @return IComment |
|
| 378 | - * @since 9.0.0 |
|
| 379 | - */ |
|
| 380 | - public function setObject($objectType, $objectId) { |
|
| 381 | - if( |
|
| 382 | - !is_string($objectType) || !trim($objectType) |
|
| 383 | - || !is_string($objectId) || !trim($objectId) |
|
| 384 | - ) { |
|
| 385 | - throw new \InvalidArgumentException('String expected.'); |
|
| 386 | - } |
|
| 387 | - $this->data['objectType'] = trim($objectType); |
|
| 388 | - $this->data['objectId'] = trim($objectId); |
|
| 389 | - return $this; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * sets the comment data based on an array with keys as taken from the |
|
| 394 | - * database. |
|
| 395 | - * |
|
| 396 | - * @param array $data |
|
| 397 | - * @return IComment |
|
| 398 | - */ |
|
| 399 | - protected function fromArray($data) { |
|
| 400 | - foreach(array_keys($data) as $key) { |
|
| 401 | - // translate DB keys to internal setter names |
|
| 402 | - $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); |
|
| 403 | - $setter = str_replace('Timestamp', 'DateTime', $setter); |
|
| 404 | - |
|
| 405 | - if(method_exists($this, $setter)) { |
|
| 406 | - $this->$setter($data[$key]); |
|
| 407 | - } |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - foreach(['actor', 'object'] as $role) { |
|
| 411 | - if(isset($data[$role . '_type']) && isset($data[$role . '_id'])) { |
|
| 412 | - $setter = 'set' . ucfirst($role); |
|
| 413 | - $this->$setter($data[$role . '_type'], $data[$role . '_id']); |
|
| 414 | - } |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - return $this; |
|
| 418 | - } |
|
| 33 | + protected $data = [ |
|
| 34 | + 'id' => '', |
|
| 35 | + 'parentId' => '0', |
|
| 36 | + 'topmostParentId' => '0', |
|
| 37 | + 'childrenCount' => '0', |
|
| 38 | + 'message' => '', |
|
| 39 | + 'verb' => '', |
|
| 40 | + 'actorType' => '', |
|
| 41 | + 'actorId' => '', |
|
| 42 | + 'objectType' => '', |
|
| 43 | + 'objectId' => '', |
|
| 44 | + 'creationDT' => null, |
|
| 45 | + 'latestChildDT' => null, |
|
| 46 | + ]; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Comment constructor. |
|
| 50 | + * |
|
| 51 | + * @param array $data optional, array with keys according to column names from |
|
| 52 | + * the comments database scheme |
|
| 53 | + */ |
|
| 54 | + public function __construct(array $data = null) { |
|
| 55 | + if(is_array($data)) { |
|
| 56 | + $this->fromArray($data); |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * returns the ID of the comment |
|
| 62 | + * |
|
| 63 | + * It may return an empty string, if the comment was not stored. |
|
| 64 | + * It is expected that the concrete Comment implementation gives an ID |
|
| 65 | + * by itself (e.g. after saving). |
|
| 66 | + * |
|
| 67 | + * @return string |
|
| 68 | + * @since 9.0.0 |
|
| 69 | + */ |
|
| 70 | + public function getId() { |
|
| 71 | + return $this->data['id']; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * sets the ID of the comment and returns itself |
|
| 76 | + * |
|
| 77 | + * It is only allowed to set the ID only, if the current id is an empty |
|
| 78 | + * string (which means it is not stored in a database, storage or whatever |
|
| 79 | + * the concrete implementation does), or vice versa. Changing a given ID is |
|
| 80 | + * not permitted and must result in an IllegalIDChangeException. |
|
| 81 | + * |
|
| 82 | + * @param string $id |
|
| 83 | + * @return IComment |
|
| 84 | + * @throws IllegalIDChangeException |
|
| 85 | + * @since 9.0.0 |
|
| 86 | + */ |
|
| 87 | + public function setId($id) { |
|
| 88 | + if(!is_string($id)) { |
|
| 89 | + throw new \InvalidArgumentException('String expected.'); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $id = trim($id); |
|
| 93 | + if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
| 94 | + $this->data['id'] = $id; |
|
| 95 | + return $this; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + throw new IllegalIDChangeException('Not allowed to assign a new ID to an already saved comment.'); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * returns the parent ID of the comment |
|
| 103 | + * |
|
| 104 | + * @return string |
|
| 105 | + * @since 9.0.0 |
|
| 106 | + */ |
|
| 107 | + public function getParentId() { |
|
| 108 | + return $this->data['parentId']; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * sets the parent ID and returns itself |
|
| 113 | + * |
|
| 114 | + * @param string $parentId |
|
| 115 | + * @return IComment |
|
| 116 | + * @since 9.0.0 |
|
| 117 | + */ |
|
| 118 | + public function setParentId($parentId) { |
|
| 119 | + if(!is_string($parentId)) { |
|
| 120 | + throw new \InvalidArgumentException('String expected.'); |
|
| 121 | + } |
|
| 122 | + $this->data['parentId'] = trim($parentId); |
|
| 123 | + return $this; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * returns the topmost parent ID of the comment |
|
| 128 | + * |
|
| 129 | + * @return string |
|
| 130 | + * @since 9.0.0 |
|
| 131 | + */ |
|
| 132 | + public function getTopmostParentId() { |
|
| 133 | + return $this->data['topmostParentId']; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * sets the topmost parent ID and returns itself |
|
| 139 | + * |
|
| 140 | + * @param string $id |
|
| 141 | + * @return IComment |
|
| 142 | + * @since 9.0.0 |
|
| 143 | + */ |
|
| 144 | + public function setTopmostParentId($id) { |
|
| 145 | + if(!is_string($id)) { |
|
| 146 | + throw new \InvalidArgumentException('String expected.'); |
|
| 147 | + } |
|
| 148 | + $this->data['topmostParentId'] = trim($id); |
|
| 149 | + return $this; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * returns the number of children |
|
| 154 | + * |
|
| 155 | + * @return int |
|
| 156 | + * @since 9.0.0 |
|
| 157 | + */ |
|
| 158 | + public function getChildrenCount() { |
|
| 159 | + return $this->data['childrenCount']; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * sets the number of children |
|
| 164 | + * |
|
| 165 | + * @param int $count |
|
| 166 | + * @return IComment |
|
| 167 | + * @since 9.0.0 |
|
| 168 | + */ |
|
| 169 | + public function setChildrenCount($count) { |
|
| 170 | + if(!is_int($count)) { |
|
| 171 | + throw new \InvalidArgumentException('Integer expected.'); |
|
| 172 | + } |
|
| 173 | + $this->data['childrenCount'] = $count; |
|
| 174 | + return $this; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * returns the message of the comment |
|
| 179 | + * |
|
| 180 | + * @return string |
|
| 181 | + * @since 9.0.0 |
|
| 182 | + */ |
|
| 183 | + public function getMessage() { |
|
| 184 | + return $this->data['message']; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * sets the message of the comment and returns itself |
|
| 189 | + * |
|
| 190 | + * @param string $message |
|
| 191 | + * @param int $maxLength |
|
| 192 | + * @return IComment |
|
| 193 | + * @throws MessageTooLongException |
|
| 194 | + * @since 9.0.0 |
|
| 195 | + */ |
|
| 196 | + public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH) { |
|
| 197 | + if(!is_string($message)) { |
|
| 198 | + throw new \InvalidArgumentException('String expected.'); |
|
| 199 | + } |
|
| 200 | + $message = trim($message); |
|
| 201 | + if ($maxLength && mb_strlen($message, 'UTF-8') > $maxLength) { |
|
| 202 | + throw new MessageTooLongException('Comment message must not exceed ' . $maxLength. ' characters'); |
|
| 203 | + } |
|
| 204 | + $this->data['message'] = $message; |
|
| 205 | + return $this; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * returns an array containing mentions that are included in the comment |
|
| 210 | + * |
|
| 211 | + * @return array each mention provides a 'type' and an 'id', see example below |
|
| 212 | + * @since 11.0.0 |
|
| 213 | + * |
|
| 214 | + * The return array looks like: |
|
| 215 | + * [ |
|
| 216 | + * [ |
|
| 217 | + * 'type' => 'user', |
|
| 218 | + * 'id' => 'citizen4' |
|
| 219 | + * ], |
|
| 220 | + * [ |
|
| 221 | + * 'type' => 'group', |
|
| 222 | + * 'id' => 'media' |
|
| 223 | + * ], |
|
| 224 | + * … |
|
| 225 | + * ] |
|
| 226 | + * |
|
| 227 | + */ |
|
| 228 | + public function getMentions() { |
|
| 229 | + $ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions); |
|
| 230 | + if(!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { |
|
| 231 | + return []; |
|
| 232 | + } |
|
| 233 | + $uids = array_unique($mentions[0]); |
|
| 234 | + $result = []; |
|
| 235 | + foreach ($uids as $uid) { |
|
| 236 | + $result[] = ['type' => 'user', 'id' => trim(substr($uid, 1), '"')]; |
|
| 237 | + } |
|
| 238 | + return $result; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * returns the verb of the comment |
|
| 243 | + * |
|
| 244 | + * @return string |
|
| 245 | + * @since 9.0.0 |
|
| 246 | + */ |
|
| 247 | + public function getVerb() { |
|
| 248 | + return $this->data['verb']; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * sets the verb of the comment, e.g. 'comment' or 'like' |
|
| 253 | + * |
|
| 254 | + * @param string $verb |
|
| 255 | + * @return IComment |
|
| 256 | + * @since 9.0.0 |
|
| 257 | + */ |
|
| 258 | + public function setVerb($verb) { |
|
| 259 | + if(!is_string($verb) || !trim($verb)) { |
|
| 260 | + throw new \InvalidArgumentException('Non-empty String expected.'); |
|
| 261 | + } |
|
| 262 | + $this->data['verb'] = trim($verb); |
|
| 263 | + return $this; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * returns the actor type |
|
| 268 | + * |
|
| 269 | + * @return string |
|
| 270 | + * @since 9.0.0 |
|
| 271 | + */ |
|
| 272 | + public function getActorType() { |
|
| 273 | + return $this->data['actorType']; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * returns the actor ID |
|
| 278 | + * |
|
| 279 | + * @return string |
|
| 280 | + * @since 9.0.0 |
|
| 281 | + */ |
|
| 282 | + public function getActorId() { |
|
| 283 | + return $this->data['actorId']; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * sets (overwrites) the actor type and id |
|
| 288 | + * |
|
| 289 | + * @param string $actorType e.g. 'users' |
|
| 290 | + * @param string $actorId e.g. 'zombie234' |
|
| 291 | + * @return IComment |
|
| 292 | + * @since 9.0.0 |
|
| 293 | + */ |
|
| 294 | + public function setActor($actorType, $actorId) { |
|
| 295 | + if( |
|
| 296 | + !is_string($actorType) || !trim($actorType) |
|
| 297 | + || !is_string($actorId) || !trim($actorId) |
|
| 298 | + ) { |
|
| 299 | + throw new \InvalidArgumentException('String expected.'); |
|
| 300 | + } |
|
| 301 | + $this->data['actorType'] = trim($actorType); |
|
| 302 | + $this->data['actorId'] = trim($actorId); |
|
| 303 | + return $this; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * returns the creation date of the comment. |
|
| 308 | + * |
|
| 309 | + * If not explicitly set, it shall default to the time of initialization. |
|
| 310 | + * |
|
| 311 | + * @return \DateTime |
|
| 312 | + * @since 9.0.0 |
|
| 313 | + */ |
|
| 314 | + public function getCreationDateTime() { |
|
| 315 | + return $this->data['creationDT']; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * sets the creation date of the comment and returns itself |
|
| 320 | + * |
|
| 321 | + * @param \DateTime $timestamp |
|
| 322 | + * @return IComment |
|
| 323 | + * @since 9.0.0 |
|
| 324 | + */ |
|
| 325 | + public function setCreationDateTime(\DateTime $timestamp) { |
|
| 326 | + $this->data['creationDT'] = $timestamp; |
|
| 327 | + return $this; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * returns the DateTime of the most recent child, if set, otherwise null |
|
| 332 | + * |
|
| 333 | + * @return \DateTime|null |
|
| 334 | + * @since 9.0.0 |
|
| 335 | + */ |
|
| 336 | + public function getLatestChildDateTime() { |
|
| 337 | + return $this->data['latestChildDT']; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * sets the date of the most recent child |
|
| 342 | + * |
|
| 343 | + * @param \DateTime $dateTime |
|
| 344 | + * @return IComment |
|
| 345 | + * @since 9.0.0 |
|
| 346 | + */ |
|
| 347 | + public function setLatestChildDateTime(\DateTime $dateTime = null) { |
|
| 348 | + $this->data['latestChildDT'] = $dateTime; |
|
| 349 | + return $this; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * returns the object type the comment is attached to |
|
| 354 | + * |
|
| 355 | + * @return string |
|
| 356 | + * @since 9.0.0 |
|
| 357 | + */ |
|
| 358 | + public function getObjectType() { |
|
| 359 | + return $this->data['objectType']; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * returns the object id the comment is attached to |
|
| 364 | + * |
|
| 365 | + * @return string |
|
| 366 | + * @since 9.0.0 |
|
| 367 | + */ |
|
| 368 | + public function getObjectId() { |
|
| 369 | + return $this->data['objectId']; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * sets (overwrites) the object of the comment |
|
| 374 | + * |
|
| 375 | + * @param string $objectType e.g. 'files' |
|
| 376 | + * @param string $objectId e.g. '16435' |
|
| 377 | + * @return IComment |
|
| 378 | + * @since 9.0.0 |
|
| 379 | + */ |
|
| 380 | + public function setObject($objectType, $objectId) { |
|
| 381 | + if( |
|
| 382 | + !is_string($objectType) || !trim($objectType) |
|
| 383 | + || !is_string($objectId) || !trim($objectId) |
|
| 384 | + ) { |
|
| 385 | + throw new \InvalidArgumentException('String expected.'); |
|
| 386 | + } |
|
| 387 | + $this->data['objectType'] = trim($objectType); |
|
| 388 | + $this->data['objectId'] = trim($objectId); |
|
| 389 | + return $this; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * sets the comment data based on an array with keys as taken from the |
|
| 394 | + * database. |
|
| 395 | + * |
|
| 396 | + * @param array $data |
|
| 397 | + * @return IComment |
|
| 398 | + */ |
|
| 399 | + protected function fromArray($data) { |
|
| 400 | + foreach(array_keys($data) as $key) { |
|
| 401 | + // translate DB keys to internal setter names |
|
| 402 | + $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); |
|
| 403 | + $setter = str_replace('Timestamp', 'DateTime', $setter); |
|
| 404 | + |
|
| 405 | + if(method_exists($this, $setter)) { |
|
| 406 | + $this->$setter($data[$key]); |
|
| 407 | + } |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + foreach(['actor', 'object'] as $role) { |
|
| 411 | + if(isset($data[$role . '_type']) && isset($data[$role . '_id'])) { |
|
| 412 | + $setter = 'set' . ucfirst($role); |
|
| 413 | + $this->$setter($data[$role . '_type'], $data[$role . '_id']); |
|
| 414 | + } |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + return $this; |
|
| 418 | + } |
|
| 419 | 419 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | * the comments database scheme |
| 53 | 53 | */ |
| 54 | 54 | public function __construct(array $data = null) { |
| 55 | - if(is_array($data)) { |
|
| 55 | + if (is_array($data)) { |
|
| 56 | 56 | $this->fromArray($data); |
| 57 | 57 | } |
| 58 | 58 | } |
@@ -85,12 +85,12 @@ discard block |
||
| 85 | 85 | * @since 9.0.0 |
| 86 | 86 | */ |
| 87 | 87 | public function setId($id) { |
| 88 | - if(!is_string($id)) { |
|
| 88 | + if (!is_string($id)) { |
|
| 89 | 89 | throw new \InvalidArgumentException('String expected.'); |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | $id = trim($id); |
| 93 | - if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
| 93 | + if ($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
| 94 | 94 | $this->data['id'] = $id; |
| 95 | 95 | return $this; |
| 96 | 96 | } |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | * @since 9.0.0 |
| 117 | 117 | */ |
| 118 | 118 | public function setParentId($parentId) { |
| 119 | - if(!is_string($parentId)) { |
|
| 119 | + if (!is_string($parentId)) { |
|
| 120 | 120 | throw new \InvalidArgumentException('String expected.'); |
| 121 | 121 | } |
| 122 | 122 | $this->data['parentId'] = trim($parentId); |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | * @since 9.0.0 |
| 143 | 143 | */ |
| 144 | 144 | public function setTopmostParentId($id) { |
| 145 | - if(!is_string($id)) { |
|
| 145 | + if (!is_string($id)) { |
|
| 146 | 146 | throw new \InvalidArgumentException('String expected.'); |
| 147 | 147 | } |
| 148 | 148 | $this->data['topmostParentId'] = trim($id); |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | * @since 9.0.0 |
| 168 | 168 | */ |
| 169 | 169 | public function setChildrenCount($count) { |
| 170 | - if(!is_int($count)) { |
|
| 170 | + if (!is_int($count)) { |
|
| 171 | 171 | throw new \InvalidArgumentException('Integer expected.'); |
| 172 | 172 | } |
| 173 | 173 | $this->data['childrenCount'] = $count; |
@@ -194,12 +194,12 @@ discard block |
||
| 194 | 194 | * @since 9.0.0 |
| 195 | 195 | */ |
| 196 | 196 | public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH) { |
| 197 | - if(!is_string($message)) { |
|
| 197 | + if (!is_string($message)) { |
|
| 198 | 198 | throw new \InvalidArgumentException('String expected.'); |
| 199 | 199 | } |
| 200 | 200 | $message = trim($message); |
| 201 | 201 | if ($maxLength && mb_strlen($message, 'UTF-8') > $maxLength) { |
| 202 | - throw new MessageTooLongException('Comment message must not exceed ' . $maxLength. ' characters'); |
|
| 202 | + throw new MessageTooLongException('Comment message must not exceed '.$maxLength.' characters'); |
|
| 203 | 203 | } |
| 204 | 204 | $this->data['message'] = $message; |
| 205 | 205 | return $this; |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | */ |
| 228 | 228 | public function getMentions() { |
| 229 | 229 | $ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions); |
| 230 | - if(!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { |
|
| 230 | + if (!$ok || !isset($mentions[0]) || !is_array($mentions[0])) { |
|
| 231 | 231 | return []; |
| 232 | 232 | } |
| 233 | 233 | $uids = array_unique($mentions[0]); |
@@ -256,7 +256,7 @@ discard block |
||
| 256 | 256 | * @since 9.0.0 |
| 257 | 257 | */ |
| 258 | 258 | public function setVerb($verb) { |
| 259 | - if(!is_string($verb) || !trim($verb)) { |
|
| 259 | + if (!is_string($verb) || !trim($verb)) { |
|
| 260 | 260 | throw new \InvalidArgumentException('Non-empty String expected.'); |
| 261 | 261 | } |
| 262 | 262 | $this->data['verb'] = trim($verb); |
@@ -292,9 +292,9 @@ discard block |
||
| 292 | 292 | * @since 9.0.0 |
| 293 | 293 | */ |
| 294 | 294 | public function setActor($actorType, $actorId) { |
| 295 | - if( |
|
| 295 | + if ( |
|
| 296 | 296 | !is_string($actorType) || !trim($actorType) |
| 297 | - || !is_string($actorId) || !trim($actorId) |
|
| 297 | + || !is_string($actorId) || !trim($actorId) |
|
| 298 | 298 | ) { |
| 299 | 299 | throw new \InvalidArgumentException('String expected.'); |
| 300 | 300 | } |
@@ -378,9 +378,9 @@ discard block |
||
| 378 | 378 | * @since 9.0.0 |
| 379 | 379 | */ |
| 380 | 380 | public function setObject($objectType, $objectId) { |
| 381 | - if( |
|
| 381 | + if ( |
|
| 382 | 382 | !is_string($objectType) || !trim($objectType) |
| 383 | - || !is_string($objectId) || !trim($objectId) |
|
| 383 | + || !is_string($objectId) || !trim($objectId) |
|
| 384 | 384 | ) { |
| 385 | 385 | throw new \InvalidArgumentException('String expected.'); |
| 386 | 386 | } |
@@ -397,20 +397,20 @@ discard block |
||
| 397 | 397 | * @return IComment |
| 398 | 398 | */ |
| 399 | 399 | protected function fromArray($data) { |
| 400 | - foreach(array_keys($data) as $key) { |
|
| 400 | + foreach (array_keys($data) as $key) { |
|
| 401 | 401 | // translate DB keys to internal setter names |
| 402 | - $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); |
|
| 402 | + $setter = 'set'.implode('', array_map('ucfirst', explode('_', $key))); |
|
| 403 | 403 | $setter = str_replace('Timestamp', 'DateTime', $setter); |
| 404 | 404 | |
| 405 | - if(method_exists($this, $setter)) { |
|
| 405 | + if (method_exists($this, $setter)) { |
|
| 406 | 406 | $this->$setter($data[$key]); |
| 407 | 407 | } |
| 408 | 408 | } |
| 409 | 409 | |
| 410 | - foreach(['actor', 'object'] as $role) { |
|
| 411 | - if(isset($data[$role . '_type']) && isset($data[$role . '_id'])) { |
|
| 412 | - $setter = 'set' . ucfirst($role); |
|
| 413 | - $this->$setter($data[$role . '_type'], $data[$role . '_id']); |
|
| 410 | + foreach (['actor', 'object'] as $role) { |
|
| 411 | + if (isset($data[$role.'_type']) && isset($data[$role.'_id'])) { |
|
| 412 | + $setter = 'set'.ucfirst($role); |
|
| 413 | + $this->$setter($data[$role.'_type'], $data[$role.'_id']); |
|
| 414 | 414 | } |
| 415 | 415 | } |
| 416 | 416 | |