104 lines
2.4 KiB
PHP
104 lines
2.4 KiB
PHP
<?php
|
|
|
|
class DbDocProvider implements DocProvider
|
|
{
|
|
/**
|
|
* @param DocTarget $target
|
|
* @param $id
|
|
* @return Document
|
|
* @throws DocNotFoundException
|
|
*/
|
|
public function getDirById(DocTarget $target, $id)
|
|
{
|
|
$dir = $this->getEntityById($target, $id);
|
|
if (!$dir->isDir()) {
|
|
throw new DocNotFoundException();
|
|
}
|
|
return $dir;
|
|
}
|
|
|
|
/**
|
|
* @param DocTarget $target
|
|
* @param $id
|
|
* @return Document
|
|
* @throws DocNotFoundException
|
|
*/
|
|
public function getFileById(DocTarget $target, $id)
|
|
{
|
|
$file = $this->getEntityById($target, $id);
|
|
|
|
if ($file->isDir()) {
|
|
throw new DocNotFoundException();
|
|
}
|
|
|
|
return $file;
|
|
}
|
|
|
|
/**
|
|
* @param DocTarget $target
|
|
* @param $id
|
|
* @return Document
|
|
* @throws DocNotFoundException
|
|
*/
|
|
public function getEntityById(DocTarget $target, $id)
|
|
{
|
|
$doc = Document::findById($target->getDocTargetId(), $target->getDocTargetType(), $id);
|
|
|
|
if (!$doc) {
|
|
throw new DocNotFoundException();
|
|
}
|
|
|
|
return $doc;
|
|
}
|
|
|
|
/**
|
|
* @param DocTarget $target
|
|
* @param $id
|
|
* @return Document[]
|
|
*/
|
|
public function getChildren(DocTarget $target, $id)
|
|
{
|
|
return Document::getChildren($target->getDocTargetId(), $target->getDocTargetType(), $id);
|
|
|
|
}
|
|
|
|
/**
|
|
* @param DocTarget $target
|
|
* @param $name
|
|
* @param Document $parent
|
|
* @return Document
|
|
*/
|
|
public function createDir(DocTarget $target, $name, Document $parent = null)
|
|
{
|
|
$dir = new Document();
|
|
$dir->target_id = $target->getDocTargetId();
|
|
$dir->target_type = $target->getDocTargetType();
|
|
$dir->parent = $parent ? $parent->id : 0;
|
|
$dir->file = '';
|
|
$dir->name = $name;
|
|
|
|
$dir->save();
|
|
|
|
return $dir;
|
|
}
|
|
|
|
/**
|
|
* @param DocTarget $target
|
|
* @param $name
|
|
* @param $file
|
|
* @param Document $parent
|
|
* @return Document
|
|
*/
|
|
public function createFile(DocTarget $target, $name, $file, Document $parent = null)
|
|
{
|
|
$dir = new Document();
|
|
$dir->target_id = $target->getDocTargetId();
|
|
$dir->target_type = $target->getDocTargetType();
|
|
$dir->parent = $parent ? $parent->id : 0;
|
|
$dir->file = $file;
|
|
$dir->name = $name;
|
|
$dir->save();
|
|
|
|
return $dir;
|
|
}
|
|
} |