|
1
|
|
|
<?php |
|
2
|
|
|
namespace Hgraca\MicroOrm; |
|
3
|
|
|
|
|
4
|
|
|
use Hgraca\Helper\ClassHelper; |
|
5
|
|
|
use Hgraca\Helper\StringHelper; |
|
6
|
|
|
use Hgraca\MicroOrm\Config\Contract\MicroOrmConfigInterface; |
|
7
|
|
|
use Hgraca\MicroOrm\Entity\Contract\EntityDataMapperInterface; |
|
8
|
|
|
use Hgraca\MicroOrm\Entity\EntityDataMapper; |
|
9
|
|
|
|
|
10
|
|
|
class ArrayMicroOrmConfig implements MicroOrmConfigInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var array */ |
|
13
|
|
|
protected $config; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(array $config) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->config = $config; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function getDefaultDbName(): string |
|
21
|
|
|
{ |
|
22
|
|
|
reset($this->config); |
|
23
|
|
|
|
|
24
|
|
|
return key($this->config); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getDataMapperFor(string $entityFqcn, string $dbName = ''): EntityDataMapperInterface |
|
28
|
|
|
{ |
|
29
|
|
|
$dbName = $dbName ? $dbName : $this->getDefaultDbName(); |
|
30
|
|
|
|
|
31
|
|
|
$dataMapperArray = $this->config[$dbName]['entityDataMapper'][$entityFqcn] ?? []; |
|
32
|
|
|
|
|
33
|
|
|
$dataMapperArray['repositoryFqcn'] = $dataMapperArray['repositoryFqcn'] |
|
34
|
|
|
?? $this->config[$dbName]['repositoryFqcn'] |
|
35
|
|
|
?? StringHelper::replace( |
|
36
|
|
|
'Domain', |
|
37
|
|
|
'Persistence', |
|
38
|
|
|
StringHelper::replace('Entity', 'Repository', $entityFqcn) |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
$dataMapperArray['collectionFqcn'] = $dataMapperArray['collectionFqcn'] |
|
42
|
|
|
?? $this->config[$dbName]['collectionFqcn'] |
|
43
|
|
|
?? StringHelper::replace('Entity', 'Collection', $entityFqcn); |
|
44
|
|
|
|
|
45
|
|
|
$dataMapperArray['dateTimeFormat'] = $dataMapperArray['dateTimeFormat'] |
|
46
|
|
|
?? $this->config[$dbName]['dateTimeFormat'] |
|
47
|
|
|
?? 'Y-m-d H:i:s'; |
|
48
|
|
|
|
|
49
|
|
|
$dataMapperArray['tableName'] = $dataMapperArray['tableName'] |
|
50
|
|
|
?? ClassHelper::extractCanonicalClassName($entityFqcn); |
|
51
|
|
|
|
|
52
|
|
|
$dataMapperArray['attributes'] = $dataMapperArray['attributes'] ?? []; |
|
53
|
|
|
|
|
54
|
|
|
return new EntityDataMapper($entityFqcn, $dataMapperArray); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|