id = $id; if (is_dir('database/'.Comment::PATH.'/'.$id)) { $isExists = true; $content = new String(file_get_contents('database/'.Comment::PATH.'/'.$this->id.'/content')); $topic = new Topic(file_get_contents('database/'.Comment::PATH.'/'.$this->id.'/topic')); $lastedit = file_get_contents('database/'.Comment::PATH.'/'.$this->id.'/lastedit'); $posttime = file_get_contents('database/'.Comment::PATH.'/'.$this->id.'/posttime'); $author = new User(file_get_contents('database/'.Comment::PATH.'/'.$this->id.'/author')); } } function isExists() { return $this->isExists; } function getAuthor() { return $this->author; } function getLastEdit() { return $this->lastedit; } function getPostTime() { return $this->posttime; } function getContent() { return $this->content; } function setContent(String $content) { if ($this->isExists()) { if ($this->setLastEdit(time())) { Functional::writeFile('database/'.Comment::PATH.'/'.$this->id.'/content', $content->getText(), 'w'); return true; } } return false; } private function setLastEdit($time) { if ($this->isExists()) { Functional::writeFile('database/'.Comment::PATH.'/'.$this->id.'/lastedit', $time, 'w'); return true; } return false; } static function isValid(String $comment, User $author) { if (strlen($comment->getText()) < Comment::MIN_CONTENT_LENGTH) return Comment::CONTENT_TOO_SHORT; return NotificationCode::VALID; } private function setAuthor(User $author) { Functional::writeFile('database/'.Comment::PATH.'/'.$this->id.'/author', $author->getID(), 'w'); } private function setPostTime($time) { Functional::writeFile('database/'.Comment::PATH.'/'.$this->id.'/posttime', $time, 'w'); } //Mỗi comment sẽ thuộc 1 topic nào đó private function setTopic(Topic $topic) { if ($this->isExists()) { Functional::writeFile('database/'.Comment::PATH.'/'.$this->id.'/topic', $topic->getID(), 'w'); $this->topic = $topic; return true; } return false; } function getTopic() { return $this->topic; } static function createComment(Topic $topic, String $content, User $author) { if (Comment::isValid($content, $author) != NotificationCode::VALID) return Comment::isValidComment($content, $author); $id = count(scandir('database/'.Comment::PATH)) + 1; $comment = new Comment($id); if ($comment->isExists()) return Comment::CREATE_ERROR; mkdir('database/'.Comment::PATH.'/'.$id); //Add comment vào topic Functional::writeFile('database/'.Topic::PATH.'/'.$topic->getID().'/comment/'.$id, '', 'w'); //Create comment $posttime = time(); Functional::writeFile('database/'.Comment::PATH.'/'.$id.'/content', $content->getText(), 'w'); //Ghi content Functional::writeFile('database/'.Comment::PATH.'/'.$id.'/author', $author->getID(), 'w'); //Ghi author Functional::writeFile('database/'.Comment::PATH.'/'.$id.'/posttime', $posttime, 'w'); //Ghi posttime Functional::writeFile('database/'.Comment::PATH.'/'.$id.'/lastedit', $posttime, 'w'); //Ghi lastedit Functional::writeFile('database/'.Comment::PATH.'/'.$id.'/topic', $topic->getID(), 'w'); $comment = new Comment($id); return $comment; } function getID() { return $this->id; } static function getInstance($id) { $comment = new Comment($id); if ($comment->isExists()) return $comment; return null; } } ?>