/
Comment.php
110 lines (95 loc) · 2.5 KB
/
Comment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace Filehosting\Models;
use Illuminate\Database\Eloquent\Model;
use Filehosting\Models\File;
/**
* Class Comment
* @package Filehosting\Models
*/
class Comment extends Model
{
/**
* @var bool
*/
public $timestamps = false;
/**
* @var array
*/
protected $fillable = ['file_id', 'parent_id', 'author', 'comment_text'];
/**
* Generates matpath for a comment
* If a comment has parent_id, makes it root comment
* If not, makes it child comment
*/
public function generateMatpath()
{
if ($this->parent_id == null) {
$this->makeRoot();
} else {
$this->makeChild();
}
}
/**
* Generates root matpaht for a comment
*/
protected function makeRoot() // void
{
$maxRootPath = intval($this->where('file_id', $this->file_id)->where('parent_id', null)->get()->max('matpath'));
$path = $maxRootPath + 1;
$this->matpath = $this->toPath($path);
}
/**
* Generates child matpath for a comment
*/
protected function makeChild() // void
{
$parentMatpath = $this->whereId($this->parent_id)->value('matpath');
$childMaxPath = ($this->whereParentId($this->parent_id)->max('matpath'));
if ($childMaxPath == null) {
$this->matpath = "{$parentMatpath}.001";
} else {
$children = $this->countChildren($childMaxPath) + 1;
$this->matpath = "{$parentMatpath}.{$this->toPath($children)}";
}
}
/**
* Returns depth of a comment
* @return int
*/
public function getDepth(): int
{
return count($this->getExplodedMatpath($this->matpath));
}
/**
* Counts children of a comment
* @param string $childMaxMatpath
* @return int
*/
private function countChildren(string $childMaxMatpath): int
{
$lastpart = $this->getExplodedMatpath($childMaxMatpath);
if (count($lastpart) == 1) {
return 0;
} else {
return intval(end($lastpart));
}
}
/**
* Returns matpath array
* @param string $matpath
* @return array
*/
private function getExplodedMatpath(string $matpath): array
{
return explode('.', $matpath);
}
/**
* Generates string in the matpath format - 000
* @param int $int
* @return string
*/
private function toPath(int $int): string
{
return str_pad($int, 3, '0', STR_PAD_LEFT);
}
}