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

Refactoring Document.php #428

Open
wants to merge 3 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
101 changes: 72 additions & 29 deletions Document.php
Original file line number Diff line number Diff line change
@@ -1,49 +1,92 @@
<?php
class Document {
/**
* The Document file.
*
* PHP version 5
*
* I seperate Document.php into two file,
* so each class has it's own file
*
* @category Category
* @package Package
* @author Display Name <[email protected]>
* @copyright 2016-2017 Foo Inc.
* @license Foo License
* @link Link
*/

/**
* The Document class.
*
* @category Category
* @package Package
* @author Display Name <[email protected]>
* @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;
}

}
}
91 changes: 91 additions & 0 deletions ParsedFile.java
Original file line number Diff line number Diff line change
@@ -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 protected])
* @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));
}
}
}
42 changes: 0 additions & 42 deletions Parser.java

This file was deleted.

76 changes: 76 additions & 0 deletions UserDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* The User file.
*
* PHP version 5
*
* @category Category
* @package Package
* @author Display Name <[email protected]>
* @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 <[email protected]>
* @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;
}

}