Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The main refactoring #445

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 9 additions & 30 deletions Document.php
Original file line number Diff line number Diff line change
@@ -1,49 +1,28 @@
<?php
class Document {

public $user;
private $user;

public $name;
private $name;

public function init($name, User $user) {
public function __construct($name, User $user) {
assert(strlen($name) > 5);
$this->user = $user;
$this->name = $name;
}

public function getTitle() {
$db = Database::getInstance();
$row = $db->query('SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1');
return $row[3]; // third column in a row
return $this->getColumn(3); // third column in a row
}

public function getContent() {
$db = Database::getInstance();
$row = $db->query('SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1');
return $row[6]; // sixth column in a row
return $this->getColumn(6); // sixth column in a row
}

public static function getAllDocuments() {
// 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;
private function getColumn($column) {
$db = Database::getInstance();
$row = $db->query('SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1');
return $row[$column]; // sixth column in a row
}

}
40 changes: 40 additions & 0 deletions FileWithContent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* This class is thread safe.
*/
public class FileWithContent {
private File file;
public FileWithContent(File f) {
this.file = f;
}
public String getContent() throws IOException {
return getContent(true);
}
public String getContentWithoutUnicode() throws IOException {
return getContent(false);
}
public synchronized void saveContent(String content) {
try (FileOutputStream o = new FileOutputStream(file)) {
for (int i = 0; i < content.length(); i += 1) {
o.write(content.charAt(i));
}
} catch (IOException e) {
e.printStackTrace();
}
}
private synchronized String getContent(boolean withUnicode) throws IOException {
try (FileInputStream i = new FileInputStream(file)) {
String output = "";
int data;
while ((data = i.read()) > 0) {
if(withUnicode || data >= 0x80) {
output += (char) data;
})
}
return output;
}
}
}
46 changes: 0 additions & 46 deletions Parser.java

This file was deleted.

23 changes: 23 additions & 0 deletions User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
class User {

private $id;

public function __construct($id) {
$this->id = $id;
}

public function createDocument($name) {
return new Document($name, $this);
}

public function getMyDocuments() {
$db = Database::getInstance();
$list = array();
foreach ($db->query('SELECT name FROM document WHERE user_id = ' . $this->id) as $name) {
$list[] = $this->createDocument($name);
}
return $list;
}

}