1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_auth_sources\Sources\Mapper\Database; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\MapperException; |
7
|
|
|
use kalanis\kw_mapper\Mappers\Database\ADatabase; |
8
|
|
|
use kalanis\kw_mapper\Records\ARecord; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class GroupsMapper |
13
|
|
|
* @package kalanis\kw_auth_sources\Data\Mapper\Database |
14
|
|
|
* @codeCoverageIgnore remote source |
15
|
|
|
*/ |
16
|
|
|
class GroupsMapper extends ADatabase |
17
|
|
|
{ |
18
|
|
|
protected function setMap(): void |
19
|
|
|
{ |
20
|
|
|
$this->setSource('default_database'); |
21
|
|
|
$this->setTable('groups'); |
22
|
|
|
$this->setRelation('id', 'gr_id'); |
23
|
|
|
$this->setRelation('name', 'gr_name'); |
24
|
|
|
$this->setRelation('authorId', 'u_id'); |
25
|
|
|
$this->setRelation('parents', 'gr_parents'); |
26
|
|
|
$this->setRelation('desc', 'gr_desc'); |
27
|
|
|
$this->setRelation('status', 'gr_status'); |
28
|
|
|
$this->setRelation('extra', 'gr_extra'); |
29
|
|
|
$this->addPrimaryKey('id'); |
30
|
|
|
$this->addForeignKey('authors', UsersRecord::class, 'authorId', 'id'); |
31
|
|
|
$this->addForeignKey('members', UsersRecord::class, 'id', 'groupId'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ARecord $record |
36
|
|
|
* @throws MapperException |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
|
|
protected function afterLoad(ARecord $record): bool |
40
|
|
|
{ |
41
|
|
|
$entry = $record->getEntry('extra'); |
42
|
|
|
$entry->setData(json_decode(strval($entry->getData()), true), true); |
43
|
|
|
return parent::afterLoad($record); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param ARecord $record |
48
|
|
|
* @throws MapperException |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
protected function beforeInsert(ARecord $record): bool |
52
|
|
|
{ |
53
|
|
|
$entry = $record->getEntry('extra'); |
54
|
|
|
$entry->setData(json_encode($entry->getData())); |
55
|
|
|
return parent::beforeInsert($record); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param ARecord $record |
60
|
|
|
* @throws MapperException |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
protected function beforeUpdate(ARecord $record): bool |
64
|
|
|
{ |
65
|
|
|
$entry = $record->getEntry('extra'); |
66
|
|
|
$entry->setData(json_encode($entry->getData())); |
67
|
|
|
return parent::beforeUpdate($record); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|