<?php
class DocumentManager
{
private $_db;
public function __construct(DatabaseHelper $db)
{
$this->_db = $db;
}
public function init($name, $content, User $user)
{
if (strlen($name) < 5) {
throw new Exception("Document name must be 5 or more characters long.");
}
$doc = new Document();
$doc->name = $name;
$doc->content = $content;
$doc->user = $user;
return $doc;
}
public function getByName($name)
{
if (strlen($name) < 5) {
throw new Exception("Document name must be 5 or more characters long.");
}
$sql = 'SELECT * FROM document WHERE name = "' . mysql_real_escape_string($this->name) . '" LIMIT 1';
$row = $this->_db->getRow($sql);
return $this->_map($row, new Document());
}
public function getByUser(User $user)
{
$sql = 'SELECT * FROM document WHERE user_id = ' . $user->id;
$rows = $this->_db->getResult($sql);
foreach ($rows as $row) {
$documents[] = $this->_map($row, new Document());
}
return $documents;
}
private function _map($row, Document $doc)
{
$doc->name = $row['name'];
$doc->content = $row['body'];
$doc->user = UserManager::getById($row['user_id']);
return $doc;
}
}
class UserManager
{
// TODO: UserManager implementation pending
}
class User
{
public $id;
public $name;
}
class Document
{
public $name;
public $content;
public $user;
}