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

Elegant refactoring #450

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
77 changes: 53 additions & 24 deletions Document.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,74 @@
<?php
class Document {

public $user;
final class Document
{
/**
* @var string
*/
private $name;

public $name;
/**
* @var Database
*/
private $db;

public function init($name, User $user) {
assert(strlen($name) > 5);
$this->user = $user;
/**
* Document constructor.
* @param string $name
* @param Database $db
*/
public function __construct($name, $db)
{
$this->name = $name;
$this->db = $db;
}

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
public function title()
{
return $this->dbRowColumn(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
public function content()
{
return $this->dbRowColumn(6); // sixth column in a row
}

public static function getAllDocuments() {
// to be implemented later
private function dbRowColumn($column)
{
$pattern = 'SELECT * FROM document WHERE name = "%s" LIMIT 1';
$sql = sprintf($pattern, $this->name);
$row = $this->db->query($sql);
return $row[$column];
}
}

final class Documents
{
public function all()
{
// to be implemented later
return [];
}
}

class User {
final class User
{
private $db;

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

public function makeNewDocument($name) {
$doc = new Document();
$doc->init($name, $this);
return $doc;
public function newDocument($name)
{
return new Document($name, $this->db);
}

public function getMyDocuments() {
$list = array();
foreach (Document::getAllDocuments() as $doc) {
public function ownDocuments()
{
$list = [];
foreach ((new Documents)->all() as $doc) {
if ($doc->user == $this)
$list[] = $doc;
}
Expand Down