diff --git a/Document.php b/Document.php index 7803df6..d76f068 100644 --- a/Document.php +++ b/Document.php @@ -1,49 +1,92 @@ + * @copyright 2016-2017 Foo Inc. + * @license Foo License + * @link Link + */ +/** + * The Document class. + * + * @category Category + * @package Package + * @author Display Name + * @license Foo License + * @link Link + */ +class Document +{ public $user; public $name; - public function init($name, User $user) { + public $database; + + /** + * The constructor + * + * I think constructor is better than init, + * and better to make static initialization of + * database as constructor's argument + * + * @param string $name Name parameter + * @param object $user User parameter + * @param object $database Database parameter + * + * @return void + */ + public function __construct($name, User $user, Database $database) + { assert(strlen($name) > 5); $this->user = $user; $this->name = $name; + // set database here, so it can be use throughout the class + $this->database = $database; } - public function getTitle() { - $db = Database::getInstance(); - $row = $db->query('SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'); + /** + * Get document title. + * + * @return string document title + */ + public function title() + { + // move query to different line, so that the next line isn't too long + $sql = 'SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'; + $row = $this->database->query($sql); return $row[3]; // third column in a row } - public function getContent() { - $db = Database::getInstance(); - $row = $db->query('SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'); + /** + * Get document content. + * + * @return string document content + */ + public function content() + { + $sql = 'SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'; + $row = $this->database->query($sql); return $row[6]; // sixth column in a row } - public static function getAllDocuments() { + /** + * Get all document. + * + * @return array all documents + */ + public function all() + { // to be implemented later } -} - -class User { - - public function makeNewDocument($name) { - $doc = new Document(); - $doc->init($name, $this); - return $doc; - } - - public function getMyDocuments() { - $list = array(); - foreach (Document::getAllDocuments() as $doc) { - if ($doc->user == $this) - $list[] = $doc; - } - return $list; - } - -} +} \ No newline at end of file diff --git a/ParsedFile.java b/ParsedFile.java new file mode 100644 index 0000000..1e527ac --- /dev/null +++ b/ParsedFile.java @@ -0,0 +1,91 @@ + +package quiz; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * I change Parser to ParsedFile because + * Parser isn't an object and I avoid class + * using -er. + * + * @author Display Name (email@domain.com) + * @version $Id$ + * @since 0.0 + */ +final class ParsedFile { + + private File file; + + /** + * Because we're not providing any instance members. + * @param fil File input. + */ + public void parsedFile(final File fil) { + this.file = fil; + } + + /** + * Getting file. + * @return File. + */ + public static synchronized File fileOf() { + return this.file; + } + + /** + * Getting content with options. + * @param isunicode Option to return ascii or unicode content. + * @return String file content. + * @throws IOException. + */ + public String content(final boolean isunicode) throws IOException { + final FileInputStream stream = new FileInputStream(this.file); + String output = ""; + int data; + final long limit = 0x80; + while ((data = stream.read()) > 0) { + if (isunicode) { + if (data < limit) { + output += (char) data; + } + } else { + output += (char) data; + } + } + return output; + } + + /** + * Getting content. + * @return String file content without unicode. + * @throws IOException. + */ + public String content() throws IOException { + return this.content(false); + } + + /** + * Getting unicode content. + * @return String file content without unicode. + * @throws IOException. + */ + public String unicodeContent() throws IOException { + return this.content(true); + } + + /** + * Saving file content. + * @param content File content. + * @throws IOException. + */ + public void save(final String content) + throws IOException { + final FileOutputStream stream = new FileOutputStream(this.file); + for (int pos = 0; pos < content.length(); pos += 1) { + stream.write(content.charAt(pos)); + } + } +} diff --git a/Parser.java b/Parser.java deleted file mode 100644 index d6d65d3..0000000 --- a/Parser.java +++ /dev/null @@ -1,42 +0,0 @@ -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -/** - * This class is thread safe. - */ -public class Parser { - private File file; - public synchronized void setFile(File f) { - file = f; - } - public synchronized File getFile() { - return file; - } - public String getContent() throws IOException { - FileInputStream i = new FileInputStream(file); - String output = ""; - int data; - while ((data = i.read()) > 0) { - output += (char) data; - } - return output; - } - public String getContentWithoutUnicode() throws IOException { - FileInputStream i = new FileInputStream(file); - String output = ""; - int data; - while ((data = i.read()) > 0) { - if (data < 0x80) { - output += (char) data; - } - } - return output; - } - public void saveContent(String content) throws IOException { - FileOutputStream o = new FileOutputStream(file); - for (int i = 0; i < content.length(); i += 1) { - o.write(content.charAt(i)); - } - } -} diff --git a/UserDocument.php b/UserDocument.php new file mode 100644 index 0000000..50adc0b --- /dev/null +++ b/UserDocument.php @@ -0,0 +1,76 @@ + + * @copyright 2016-2017 Foo Inc. + * @license Foo License + * @link Link + */ + +// Document File required to make new Document +require_once 'Document.php'; + +/** + * The User class. + * + * @category Category + * @package Package + * @author Display Name + * @license Foo License + * @link Link + */ +class UserDocument +{ + protected $document; + + /** + * Better create constructor and pass Document + * as constructor argument + * + * @param Document $document document parameter + * + * @return void + */ + public function __construct(Document $document) + { + // set document here, so it can be use troughout the class + $this->document = $document; + } + + /** + * Create user document. + * + * @param string $name Name parameter + * + * @return Document New Document + */ + public function create($name) + { + // use document constructor to create new document + // instead of init method + $document = new Document($name, $this, $this->document->database); + return $document; + } + + /** + * List of user document. + * + * @return array list of my documents + */ + public function listOf() + { + $list = array(); + foreach ($this->document->all() as $doc) { + if ($doc->user == $this) { + $list[] = $doc; + } + } + return $list; + } + +}