Skip to content

Commit 83348f0

Browse files
committed
coming along with the config...
1 parent 92324c2 commit 83348f0

27 files changed

+788
-39
lines changed

bin/config.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
// If true, will skip generation of test classes
3535
'skipTests' => false,
3636

37-
// If you wish to specify alternative libxml opts, do so here.
37+
// The libxml opts to use for parsing source XSD's.
3838
'libxmlOpts' => LIBXML_NONET | LIBXML_BIGLINES | LIBXML_PARSEHUGE | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL,
3939

4040
// Map of versions and configurations to generate.
@@ -45,10 +45,27 @@
4545
'DSTU1' => [
4646
// Source URL
4747
'sourceUrl' => 'https://hl7.org/fhir/DSTU1/fhir-all-xsd.zip',
48-
// Namespace
48+
// Namespace to generate classes into
4949
'namespace' => 'Versions\\DSTU1',
50-
// If defined, enables integration and validation test generation against the provided endpoint.
50+
// If defined, enables integration test generation against the provided endpoint.
5151
'testEndpoint' => '',
52+
53+
// The default configuration for all instances of this version. May be overridden per version during
54+
// instantiation.
55+
'defaultConfig' => [
56+
'unserializeConfig' => [
57+
// Libxml options to use when unserialize types from XML
58+
'libxmlOpts' => LIBXML_NONET | LIBXML_BIGLINES | LIBXML_PARSEHUGE | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOXMLDECL,
59+
// Maximum depth to allow when decoding JSON
60+
'jsonDecodeMaxDepth' => 512,
61+
],
62+
'serializeConfig' => [
63+
// If true, will override the default xmlns value with the value provided in the rootXmlns key
64+
'overrideSourceXmlns' => false,
65+
// If overrideSourceXmlns is true, this value will be used as the root xmlns value
66+
'rootXmlns' => 'http://hl7.org/fhir',
67+
]
68+
]
5269
],
5370
'DSTU2' => [
5471
'sourceUrl' => 'https://hl7.org/fhir/DSTU2/fhir-all-xsd.zip',

files/constants.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
// Core class names
7676
const PHPFHIR_CLASSNAME_AUTOLOADER = 'Autoloader';
7777
const PHPFHIR_CLASSNAME_CONFIG = 'Config';
78+
const PHPFHIR_CLASSNAME_ABSTRACT_VERSION_CONFIG = 'AbstractVersionConfig';
79+
const PHPFHIR_CLASSNAME_UNSERIALIZE_CONFIG = 'UnserializeConfig';
80+
const PHPFHIR_CLASSNAME_SERIALIZE_CONFIG = 'SerializeConfig';
7881
const PHPFHIR_CLASSNAME_RESPONSE_PARSER = 'ResponseParser';
7982
const PHPFHIR_CLASSNAME_CONSTANTS = 'Constants';
8083
const PHPFHIR_CLASSNAME_API_CLIENT = 'APIClient';
@@ -99,7 +102,9 @@
99102

100103
// Core enums
101104
const PHPFHIR_ENUM_CONFIG_KEY = 'ConfigKeyEnum';
102-
const PHPFHIR_ENUM_TYPE = 'TypeEnum';
105+
const PHPFHIR_ENUM_VERSION_CONFIG_KEY = 'VersionConfigKeyEnum';
106+
const PHPFHIR_ENUM_UNSERIALIZE_CONFIG_KEY = 'UnserializeConfigKeyEnum';
107+
const PHPFHIR_ENUM_SERIALIZE_CONFIG_KEY = 'SerializeConfigKey';
103108
const PHPFHIR_ENUM_API_FORMAT = 'ApiFormatEnum';
104109
const PHPFHIR_ENUM_XML_LOCATION = 'XMLLocationEnum';
105110
const PHPFHIR_ENUM_API_RESOURCE_LIST = 'APIResourceListEnum';
@@ -109,6 +114,7 @@
109114
const PHPFHIR_CLASSNAME_VERSION = 'Version';
110115
const PHPFHIR_CLASSNAME_VERSION_CONFIG = 'VersionConfig';
111116
const PHPFHIR_CLASSNAME_VERSION_CONSTANTS = 'VersionConstants';
117+
const PHPFHIR_VERSION_ENUM_TYPE = 'VersionTypeEnum';
112118
const PHPFHIR_CLASSNAME_VERSION_TYPEMAP = 'VersionTypeMap';
113119
const PHPFHIR_INTERFACE_VERSION_CONTAINED_TYPE = 'VersionContainedTypeInterface';
114120

files/funcs.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,34 @@ function require_with(string $requiredFile, array $vars): mixed
4242
unset($vars, $num);
4343
return require $requiredFile;
4444
}
45+
46+
/**
47+
* Makes array var exporting less terrible.
48+
*
49+
* @param mixed $var root var
50+
* @param int $indent current indent level, only used during array exporting
51+
* @param bool $indentFirst if true, indents the first line of the array
52+
* @param int $indentSize number of spaces to output per indent level
53+
* @return string
54+
*/
55+
function pretty_var_export(mixed $var, int $indent = 0, bool $indentFirst = false, int $indentSize = 4): string
56+
{
57+
if (!is_array($var)) {
58+
return var_export($var, true);
59+
}
60+
61+
if ([] === $var) {
62+
return '[]';
63+
}
64+
65+
$out = sprintf("%s[", str_repeat(' ', $indentFirst ? $indent * $indentSize : 0));
66+
foreach ($var as $k => $v) {
67+
$out = sprintf("%s\n%s%s => %s,",
68+
$out,
69+
str_repeat(' ', ($indent + 1) * $indentSize),
70+
var_export($k, true),
71+
pretty_var_export($v, $indent + 1, true, $indentSize)
72+
);
73+
}
74+
return sprintf("%s\n%s]", $out, str_repeat(' ', $indent * $indentSize));
75+
}

src/Config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ class Config implements LoggerAwareInterface
6767
*/
6868
public function __construct(array $params = [], LoggerInterface $logger = null)
6969
{
70-
foreach (ConfigKeys::required() as $key) {
70+
foreach (ConfigKeyEnum::required() as $key) {
7171
if (!isset($params[$key->value])) {
7272
throw new \DomainException(sprintf('Missing required configuration key "%s"', $key->value));
7373
}
7474
$this->{"set$key->value"}($params[$key->value]);
7575
}
7676

77-
foreach (ConfigKeys::optional() as $key) {
77+
foreach (ConfigKeyEnum::optional() as $key) {
7878
if (isset($params[$key->value])) {
7979
$this->{"set$key->value"}($params[$key->value]);
8080
}

src/ConfigKeys.php renamed to src/ConfigKeyEnum.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* limitations under the License.
1919
*/
2020

21-
enum ConfigKeys: string
21+
enum ConfigKeyEnum: string
2222
{
2323
case SCHEMA_PATH = 'schemaPath';
2424
case CLASSES_PATH = 'classesPath';
@@ -29,7 +29,7 @@ enum ConfigKeys: string
2929
case LIBXML_OPTS = 'libxmlOpts';
3030

3131
/**
32-
* @return \DCarbone\PHPFHIR\ConfigKeys[]
32+
* @return \DCarbone\PHPFHIR\ConfigKeyEnum[]
3333
*/
3434
public static function required(): array
3535
{
@@ -42,7 +42,7 @@ public static function required(): array
4242
}
4343

4444
/**
45-
* @return \DCarbone\PHPFHIR\ConfigKeys[]
45+
* @return \DCarbone\PHPFHIR\ConfigKeyEnum[]
4646
*/
4747
public static function optional(): array
4848
{

src/Version.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use DCarbone\PHPFHIR\Utilities\NameUtils;
2323
use DCarbone\PHPFHIR\Version\SourceMetadata;
2424
use DCarbone\PHPFHIR\Version\Definition;
25+
use DCarbone\PHPFHIR\Version\VersionDefaultConfig;
2526
use InvalidArgumentException;
2627

2728
/**
@@ -36,13 +37,14 @@ class Version
3637
/** @var \DCarbone\PHPFHIR\Version\SourceMetadata */
3738
private SourceMetadata $copyright;
3839

40+
/** @var \DCarbone\PHPFHIR\Version\VersionDefaultConfig */
41+
private VersionDefaultConfig $defaultConfig;
42+
3943
/** @var string */
4044
private string $name;
4145
/** @var string */
4246
private string $sourceUrl;
4347
/** @var string */
44-
private string $sourcePath;
45-
/** @var string */
4648
private string $namespace;
4749
/** @var string */
4850
private string $testEndpoint;
@@ -101,6 +103,10 @@ public function __construct(Config $config, string $name, array $params = [])
101103
);
102104
}
103105

106+
if (!isset($this->defaultConfig)) {
107+
$this->defaultConfig = new VersionDefaultConfig([]);
108+
}
109+
104110
$this->copyright = new SourceMetadata($config, $this);
105111
}
106112

@@ -184,6 +190,27 @@ public function setNamespace(string $namespace): self
184190
return $this;
185191
}
186192

193+
/**
194+
* @return \DCarbone\PHPFHIR\VersionDefaultConfig
195+
*/
196+
public function getDefaultConfig(): VersionDefaultConfig
197+
{
198+
return $this->defaultConfig;
199+
}
200+
201+
/**
202+
* @param array|\DCarbone\PHPFHIR\VersionDefaultConfig $defaultConfig
203+
* @return $this
204+
*/
205+
public function setDefaultConfig(array|VersionDefaultConfig $defaultConfig): self
206+
{
207+
if (is_array($defaultConfig)) {
208+
$defaultConfig = new VersionDefaultConfig($defaultConfig);
209+
}
210+
$this->defaultConfig = $defaultConfig;
211+
return $this;
212+
}
213+
187214
/**
188215
* @return string|null
189216
*/

src/Version/VersionDefaultConfig.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DCarbone\PHPFHIR\Version;
4+
5+
/*
6+
* Copyright 2018-2024 Daniel Carbone (daniel.p.carbone@gmail.com)
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
/**
22+
* Class VersoinConfig
23+
* @package DCarbone\PHPFHIR\Version
24+
*/
25+
class VersionDefaultConfig
26+
{
27+
private const _UNSERIALIZE_CONFIG_KEYS = [
28+
'libxmlOpts',
29+
'jsonDecodeMaxDepth',
30+
];
31+
32+
private const _SERIALIZE_CONFIG_KEYS = [
33+
'overrideSourceXmlns',
34+
'rootXmlns',
35+
];
36+
37+
/** @var array */
38+
private array $_unserializeConfig = [];
39+
/** @var array */
40+
private array $_serializeConfig = [];
41+
42+
/**
43+
* @param array $config
44+
*/
45+
public function __construct(array $config)
46+
{
47+
foreach (VersionDefaultConfigKeyEnum::cases() as $k) {
48+
if (isset($config[$k->value]) || array_key_exists($k->value, $config)) {
49+
$this->{"set{$k->value}"}($config[$k->value]);
50+
}
51+
}
52+
}
53+
54+
/**
55+
* @param array $config
56+
* @return self
57+
*/
58+
public function setUnserializeConfig(array $config): self
59+
{
60+
$this->_unserializeConfig = [];
61+
if ([] === $config) {
62+
return $this;
63+
}
64+
foreach (self::_UNSERIALIZE_CONFIG_KEYS as $k) {
65+
if (isset($config[$k]) || array_key_exists($k, $config)) {
66+
$this->_unserializeConfig[$k] = $config[$k];
67+
}
68+
}
69+
return $this;
70+
}
71+
72+
/**
73+
* @return array
74+
*/
75+
public function getUnserializeConfig(): array
76+
{
77+
return $this->_unserializeConfig;
78+
}
79+
80+
/**
81+
* @param array $config
82+
* @return self
83+
*/
84+
public function setSerializeConfig(array $config): self
85+
{
86+
$this->_serializeConfig = [];
87+
if ([] === $config) {
88+
return $this;
89+
}
90+
foreach (self::_SERIALIZE_CONFIG_KEYS as $k) {
91+
if (isset($config[$k]) || array_key_exists($k, $config)) {
92+
$this->_serializeConfig[$k] = $config[$k];
93+
}
94+
}
95+
return $this;
96+
}
97+
98+
/**
99+
* @return array
100+
*/
101+
public function getSerializeConfig(): array
102+
{
103+
return $this->_serializeConfig;
104+
}
105+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace DCarbone\PHPFHIR\Version;
4+
5+
/*
6+
* Copyright 2024 Daniel Carbone (daniel.p.carbone@gmail.com)
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
enum VersionDefaultConfigKeyEnum: string
22+
{
23+
case UNSERIALIZE_CONFIG = 'unserializeConfig';
24+
case SERIALIZE_CONFIG = 'serializeConfig';
25+
}

src/VersionKeys.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ enum VersionKeys: string
2323
case SOURCE_URL = 'sourceUrl';
2424
case NAMESPACE = 'namespace';
2525
case TEST_ENDPOINT = 'testEndpoint';
26+
case DEFAULT_CONFIG = 'defaultConfig';
2627

2728
/**
2829
* @return \DCarbone\PHPFHIR\VersionKeys[]

0 commit comments

Comments
 (0)