-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.php
More file actions
92 lines (72 loc) · 1.99 KB
/
Config.php
File metadata and controls
92 lines (72 loc) · 1.99 KB
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
<?php
namespace SyncEngine\Model\Trait;
use SyncEngine\Model\Interface\Persistable;
use SyncEngine\Service\ModelNormalizer;
use SyncEngine\Structure\Data\ConfigData;
trait Config
{
protected ConfigData $config;
protected function initConfig(): void
{
$this->config = new ConfigData();
if ( $this instanceof Persistable && is_callable( [ $this->getEntity(), 'getConfig' ] ) ) {
$this->config->set( (array) $this->getEntity()->getConfig() );
}
}
public function getConfig( $key = null, $default = null ): mixed
{
if ( ! isset( $this->config ) ) {
$this->initConfig();
}
$value = $this->config->get( $key, $default );
if ( ! $key ) {
// This isn't part of the model config but for internal use only.
unset( $value['_dependencies'] );
}
return $value;
}
public function setConfig( $value, $key = null ): void
{
if ( ! isset( $this->config ) ) {
$this->initConfig();
}
$this->config->set( $value, $key );
$this->updateConfig( $this->config->sanitize( $this->getConfigFields() ) );
}
public function exportConfig(): array
{
if ( $this instanceof Persistable ) {
$config = $this->getEntity()->getConfig();
} else {
$config = $this->getConfig();
}
// Depencies are internal only.
unset( $config['_dependencies'] );
return $config;
}
public function updateConfig( $config ): void
{
if ( $this instanceof Persistable && is_callable( [ $this->getEntity(), 'setConfig' ] ) ) {
$this->getEntity()->setConfig( $config );
}
}
public function fetchConfigDependencies(): void
{
$this->setConfig( array_keys( $this->getConfigDependencies() ), '_dependencies' );
}
public function getConfigDependencies( array|bool $recursive = false ): array
{
return $this->getContainer()->get( ModelNormalizer::class )->getConfigDependencies(
$this->getConfig(),
$this->getConfigFields(),
$recursive
);
}
public function getConfigFields(): array
{
if ( method_exists( $this, 'getFields' ) ) {
return $this->getFields();
}
return [];
}
}