1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hgraca\MicroOrm; |
4
|
|
|
|
5
|
|
|
use Hgraca\Helper\ClassHelper; |
6
|
|
|
use Hgraca\Helper\StringHelper; |
7
|
|
|
use Hgraca\MicroOrm\DataMapper\DataMapper; |
8
|
|
|
use Hgraca\MicroOrm\DataMapper\DataMapperInterface; |
9
|
|
|
|
10
|
|
|
class ArrayConfig implements ConfigInterface |
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 = ''): DataMapperInterface |
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 DataMapper($entityFqcn, $dataMapperArray); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|