-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigManager.php
More file actions
153 lines (128 loc) · 4.8 KB
/
ConfigManager.php
File metadata and controls
153 lines (128 loc) · 4.8 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace Astral\Serialize\Support\Config;
use Astral\Serialize\Casts\InputValue\InputArrayBestMatchChildCast;
use Astral\Serialize\Casts\InputValue\InputArraySingleChildCast;
use Astral\Serialize\Casts\InputValue\InputObjectBestMatchChildCast;
use Astral\Serialize\Casts\InputValue\InputValueEnumCast;
use Astral\Serialize\Casts\InputValue\InputValueNullCast;
use Astral\Serialize\Casts\InputValue\InputValueOnlyBaseTypeCast;
use Astral\Serialize\Casts\Normalizer\ArrayNormalizerCast;
use Astral\Serialize\Casts\Normalizer\DateTimeNormalizerCast;
use Astral\Serialize\Casts\Normalizer\JsonNormalizerCast;
use Astral\Serialize\Casts\Normalizer\ObjectNormalizerCast;
use Astral\Serialize\Casts\OutValue\OutValueEnumCast;
use Astral\Serialize\Casts\OutValue\OutValueGetterCast;
use Astral\Serialize\Contracts\Attribute\DataCollectionCastInterface;
use Astral\Serialize\Contracts\Attribute\InputValueCastInterface;
use Astral\Serialize\Contracts\Attribute\OutValueCastInterface;
use Astral\Serialize\Contracts\Normalizer\NormalizerCastInterface;
use Astral\Serialize\Enums\CacheDriverEnum;
use Astral\Serialize\Enums\ConfigCastEnum;
use Astral\Serialize\Support\Caching\MemoryCache;
use RuntimeException;
class ConfigManager
{
public static ConfigManager $instance;
/** @var DataCollectionCastInterface[] $attributePropertyResolver */
private array $attributePropertyResolver = [];
/** @var (InputValueCastInterface|string)[] $inputValueCasts */
private array $inputValueCasts = [
InputValueNullCast::class,
InputObjectBestMatchChildCast::class,
InputArraySingleChildCast::class,
InputArrayBestMatchChildCast::class,
InputValueEnumCast::class,
InputValueOnlyBaseTypeCast::class,
];
/** @var (OutValueCastInterface|string)[] $outputValueCasts */
private array $outputValueCasts = [
OutValueEnumCast::class,
OutValueGetterCast::class,
];
/** @var (NormalizerCastInterface|string)[] $inputNormalizerCasts */
private array $inputNormalizerCasts = [
// JsonNormalizerCast::class,
ArrayNormalizerCast::class,
];
/** @var (NormalizerCastInterface|string)[] $inputNormalizerCasts */
private array $outNormalizerCasts = [
DateTimeNormalizerCast::class,
ArrayNormalizerCast::class,
ObjectNormalizerCast::class
];
/** @var CacheDriverEnum|class-string $cacheDriver */
private string|CacheDriverEnum $cacheDriver = MemoryCache::class;
public static function getInstance(): ConfigManager
{
return self::$instance ??= new self();
}
public function __construct()
{
$this->instantiateArrayProperties(ConfigCastEnum::getValues());
}
/**
* @param array $propertyNames
* @return void
*/
private function instantiateArrayProperties(array $propertyNames): void
{
foreach ($propertyNames as $property) {
if (!property_exists($this, $property)) {
throw new RuntimeException("Property $property does not exist");
}
$this->$property = array_map(
fn ($class) => is_string($class) ? new $class() : $class,
$this->$property
);
}
}
/**
* @param object|string $castClass
* @param ConfigCastEnum $castEnum
* @return static
* @throws RuntimeException
*/
public function addCast(object|string $castClass, ConfigCastEnum $castEnum): static
{
if (is_string($castClass) && !is_subclass_of($castClass, $castEnum->getCastInterface())) {
throw new RuntimeException("Cast class must be an instance of {$castEnum->getCastInterface()}");
}
if (!property_exists($this, $castEnum->value)) {
throw new RuntimeException("Property {$castEnum->value} does not exist");
}
$this->{$castEnum->value}[] = is_string($castClass) ? new $castClass() : $castClass;
return $this;
}
public function getAttributePropertyResolver(): array
{
return $this->attributePropertyResolver;
}
public function getInputValueCasts(): array
{
return $this->inputValueCasts;
}
public function getOutValueCasts(): array
{
return $this->outputValueCasts;
}
public function getInputNormalizerCasts(): array
{
return $this->inputNormalizerCasts;
}
public function getOutNormalizerCasts(): array
{
return $this->outNormalizerCasts;
}
public function getCacheDriver(): string
{
if ($this->cacheDriver instanceof CacheDriverEnum) {
return $this->cacheDriver->value;
}
return $this->cacheDriver;
}
public function setCacheDriver(string|CacheDriverEnum $cacheDriver): static
{
$this->cacheDriver = $cacheDriver;
return $this;
}
}