|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Bluz PHP Team |
|
4
|
|
|
* @link https://github.com/bluzphp/skeleton |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace Application\Categories; |
|
10
|
|
|
|
|
11
|
|
|
use Bluz\Validator\Traits\Validator; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Row |
|
15
|
|
|
* @package Application\Categories |
|
16
|
|
|
* |
|
17
|
|
|
* @property integer $id |
|
18
|
|
|
* @property integer $rootId |
|
19
|
|
|
* @property integer $parentId |
|
20
|
|
|
* @property string $name |
|
21
|
|
|
* @property string $alias |
|
22
|
|
|
* @property string $created |
|
23
|
|
|
* @property string $updated |
|
24
|
|
|
*/ |
|
25
|
|
|
class Row extends \Bluz\Db\Row |
|
26
|
|
|
{ |
|
27
|
|
|
use Validator; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Row[] |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $children = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return void |
|
36
|
|
|
* @throws \Bluz\Validator\Exception\ComponentException |
|
37
|
|
|
*/ |
|
38
|
|
|
public function beforeSave() : void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->addValidator('name') |
|
41
|
|
|
->required() |
|
42
|
|
|
->length(2, 128); |
|
43
|
|
|
|
|
44
|
|
|
$this->addValidator('alias') |
|
45
|
|
|
->required() |
|
46
|
|
|
->length(2, 64) |
|
47
|
|
|
->slug() |
|
48
|
|
|
->callback( |
|
49
|
|
|
function ($input) { |
|
50
|
|
|
$select = $this->getTable()->select() |
|
51
|
|
|
->where('alias = ?', $input); |
|
52
|
|
|
|
|
53
|
|
|
if ($this->id) { |
|
54
|
|
|
$select->andWhere('id != ?', $this->id); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($this->parentId) { |
|
58
|
|
|
$select->andWhere('parentId = ?', $this->parentId); |
|
59
|
|
|
} else { |
|
60
|
|
|
$select->andWhere('parentId IS NULL'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return !\count($select->execute()); |
|
64
|
|
|
}, |
|
65
|
|
|
'Category with this alias is already exists' |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
public function beforeInsert(): void |
|
73
|
|
|
{ |
|
74
|
|
|
$this->created = gmdate('Y-m-d H:i:s'); |
|
75
|
|
|
if (empty($this->parentId)) { |
|
76
|
|
|
$this->parentId = null; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
public function beforeUpdate(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->updated = gmdate('Y-m-d H:i:s'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Add child |
|
90
|
|
|
* |
|
91
|
|
|
* @param Row $row |
|
92
|
|
|
*/ |
|
93
|
|
|
public function addChild(Row $row): void |
|
94
|
|
|
{ |
|
95
|
|
|
$this->children[$row->id] = $row; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get children directories |
|
100
|
|
|
* |
|
101
|
|
|
* @return Row[] |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getChildren(): array |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->children; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|