From 2cb6b31d7289e4dd7b465811bd0c47063aa8536a Mon Sep 17 00:00:00 2001 From: Daniel Carbone Date: Mon, 15 Apr 2024 17:51:56 -0500 Subject: [PATCH] PHP 8.1+ updates (#95) PHP 8.1+ updates --- .github/dependabot.yml | 26 ++ .github/workflows/tests.yaml | 68 +++-- README.md | 4 +- bin/config.php | 2 +- bin/generate.php | 35 ++- composer.json | 16 +- files/constants.php | 146 +++++----- files/funcs.php | 6 +- src/Builder.php | 76 ++--- src/Builder/TypeBuilder.php | 4 +- src/Config.php | 260 ++++++++++++++++- src/Config/AbstractConfig.php | 264 ------------------ src/Config/Config80.php | 37 --- src/Config/Version.php | 4 +- src/Config/VersionConfig.php | 2 +- src/Definition.php | 2 +- ...AnnotationElementPropertyTypeDecorator.php | 15 +- .../AnnotationElementTypeDecorator.php | 8 +- .../Decorator/AnyElementTypeDecorator.php | 10 +- .../AttributeElementTypeDecorator.php | 20 +- .../ChoiceElementElementPropertyDecorator.php | 14 +- .../Decorator/ChoiceElementTypeDecorator.php | 14 +- .../ComplexContentElementTypeDecorator.php | 10 +- .../ComplexTypeElementTypeDecorator.php | 22 +- .../Decorator/ElementElementTypeDecorator.php | 30 +- .../ExtensionElementTypeDecorator.php | 12 +- .../RestrictionElementTypeDecorator.php | 22 +- .../SequenceElementTypeDecorator.php | 12 +- .../SimpleContentElementTypeDecorator.php | 6 +- .../SimpleTypeElementPropertyDecorator.php | 2 +- .../SimpleTypeElementTypeDecorator.php | 12 +- .../Decorator/UnionElementTypeDecorator.php | 10 +- src/Definition/DocumentationTrait.php | 10 +- src/Definition/Enumeration.php | 6 +- src/Definition/EnumerationValue.php | 8 +- src/Definition/Properties.php | 26 +- src/Definition/Property.php | 26 +- src/Definition/SourceTrait.php | 2 +- src/Definition/Type.php | 53 ++-- src/Definition/TypeDecorationValidator.php | 12 +- src/Definition/TypeDecorator.php | 94 ++++--- src/Definition/TypeExtractor.php | 18 +- src/Definition/TypeImport.php | 2 +- src/Definition/TypeImports.php | 20 +- src/Definition/TypePropertyDecorator.php | 25 +- src/Definition/Types.php | 30 +- src/Enum/AttributeName.php | 57 ++++ src/Enum/AttributeNameEnum.php | 39 --- src/Enum/ElementName.php | 66 +++++ src/Enum/ElementNameEnum.php | 53 ---- src/Enum/PrimitiveType.php | 161 +++++++++++ src/Enum/PrimitiveTypeEnum.php | 112 -------- .../{AbstractEnum.php => PropertyUse.php} | 34 +-- src/Enum/PropertyUseEnum.php | 30 -- src/Enum/TypeKind.php | 82 ++++++ src/Enum/TypeKindEnum.php | 141 ---------- .../json/list.php => src/Enum/ValuesTrait.php | 15 +- src/{Config/Config74.php => LogLevel.php} | 29 +- src/Logger.php | 66 ++++- src/Logger/Logger74.php | 73 ----- src/Logger/Logger80.php | 73 ----- .../Templates.php} | 75 ++--- src/Utilities/CopyrightUtils.php | 8 +- src/Utilities/DocumentationUtils.php | 2 +- src/Utilities/ExceptionUtils.php | 18 +- src/Utilities/FileUtils.php | 25 +- src/Utilities/NameUtils.php | 9 +- src/Utilities/TypeBuilderUtils.php | 24 +- src/Utilities/TypeHintUtils.php | 210 +++++++++----- template/file/header_type.php | 2 +- .../interfaces/phpfhir_comment_container.php | 6 +- .../interfaces/phpfhir_contained_type.php | 8 +- template/interfaces/phpfhir_type.php | 13 +- template/methods/constructors/primitive.php | 60 ---- template/properties/methods/default.php | 104 ------- template/properties/methods/primitive.php | 108 ------- template/tests/test_class_constants.php | 2 +- template/tests/test_class_type_map.php | 8 +- .../integration/body_domain_resource.php | 2 +- template/tests/types/integration/class.php | 6 +- .../integration/header_domain_resource.php | 2 +- template/tests/types/unit/body_base.php | 6 +- template/tests/types/unit/body_primitive.php | 37 +-- template/tests/types/unit/class.php | 12 +- template/tests/types/unit/header.php | 2 +- template/traits/phpfhir_change_tracking.php | 4 +- template/traits/phpfhir_comment_container.php | 6 +- .../traits/phpfhir_validation_assertions.php | 4 +- template/traits/phpfhir_xml_namespace.php | 68 +++++ template/types/class.php | 59 ++-- template/types/definition.php | 2 +- template/{ => types}/methods/common.php | 27 +- template/{ => types}/methods/constructor.php | 20 +- .../methods/constructors/default.php | 14 +- .../default_property_setter_call.php | 14 +- .../types/methods/constructors/primitive.php | 75 +++++ .../constructors/primitive_container.php | 22 +- .../constructors/property_setter_default.php | 2 +- .../property_setter_primitive.php | 2 +- .../property_setter_primitive_container.php | 2 +- ...esource_container_property_setter_call.php | 2 +- .../{ => types}/methods/contained_type.php | 2 +- template/{ => types}/properties/constants.php | 10 +- .../{ => types}/properties/declaration.php | 4 +- template/{ => types}/properties/methods.php | 10 +- template/types/properties/methods/default.php | 104 +++++++ .../methods/default/getter_default.php | 4 +- .../default/getter_primitive_value.php | 4 +- .../methods/default/setter_collection.php | 5 +- .../default/setter_contained_resource.php | 4 +- .../setter_contained_resource_collection.php | 6 +- .../methods/default/setter_default.php | 9 +- .../methods/default/setter_primitive.php | 13 +- .../types/properties/methods/primitive.php | 108 +++++++ .../methods/primitive/base64_binary_type.php | 12 +- .../methods/primitive/bool_type.php | 8 +- .../methods/primitive/date_type.php | 17 +- .../methods/primitive/datetime_type.php | 17 +- .../methods/primitive/decimal_type.php | 27 +- .../methods/primitive/instant_type.php | 15 +- .../methods/primitive/integer_type.php | 13 +- .../methods/primitive/string_type.php | 12 +- .../methods/primitive/time_type.php | 15 +- .../primitive/unsigned_integer_type.php | 14 +- template/{ => types}/serialization/json.php | 20 +- .../serialization/json/default.php | 16 +- .../json/default_property_default.php | 2 +- .../json/default_property_primitive_list.php | 2 +- ...ult_property_value_primitive_container.php | 2 +- .../serialization/json/primitive.php | 12 +- .../serialization/json/resource_container.php | 6 +- template/{ => types}/serialization/xml.php | 25 +- .../serialization/xml/serialize/body.php} | 10 +- .../xml/serialize/body_typed.php} | 10 +- .../xml/serialize/body_typed_default.php} | 6 +- .../body_typed_resource_container.php} | 2 +- .../xml/serialize/body_untyped.php} | 2 +- .../serialization/xml/serialize/header.php} | 2 +- .../xml/serialize/resource_container.php | 6 +- .../serialization/xml/unserialize/body.php | 10 +- .../unserialize/body_parse_attr_primitive.php | 6 +- .../xml/unserialize/body_parse_attr_typed.php | 6 +- .../unserialize/body_parse_node_primitive.php | 6 +- .../xml/unserialize/body_parse_node_typed.php | 10 +- .../unserialize/body_parse_typed_default.php | 2 +- .../body_parse_typed_resource_container.php | 2 +- .../serialization/xml/unserialize/header.php | 6 +- template/{ => types}/validation/field_map.php | 6 +- template/{ => types}/validation/methods.php | 16 +- .../methods/collection_primitive.php | 2 +- .../validation/methods/collection_typed.php | 2 +- .../validation/methods/primitive.php | 2 +- .../{ => types}/validation/methods/typed.php | 2 +- template/types/xhtml.php | 16 +- template/utilities/autoloader_class.php | 5 +- template/utilities/constants.php | 6 +- template/utilities/response_parser_class.php | 2 +- .../response_parser_config_class.php | 2 +- template/utilities/typemap_class.php | 12 +- 159 files changed, 2100 insertions(+), 2183 deletions(-) create mode 100644 .github/dependabot.yml delete mode 100644 src/Config/AbstractConfig.php delete mode 100644 src/Config/Config80.php create mode 100644 src/Enum/AttributeName.php delete mode 100644 src/Enum/AttributeNameEnum.php create mode 100644 src/Enum/ElementName.php delete mode 100644 src/Enum/ElementNameEnum.php create mode 100644 src/Enum/PrimitiveType.php delete mode 100644 src/Enum/PrimitiveTypeEnum.php rename src/Enum/{AbstractEnum.php => PropertyUse.php} (58%) delete mode 100644 src/Enum/PropertyUseEnum.php create mode 100644 src/Enum/TypeKind.php delete mode 100644 src/Enum/TypeKindEnum.php rename template/serialization/json/list.php => src/Enum/ValuesTrait.php (72%) rename src/{Config/Config74.php => LogLevel.php} (56%) delete mode 100644 src/Logger/Logger74.php delete mode 100644 src/Logger/Logger80.php rename src/{Generator/TemplateBuilder.php => Render/Templates.php} (51%) delete mode 100644 template/methods/constructors/primitive.php delete mode 100644 template/properties/methods/default.php delete mode 100644 template/properties/methods/primitive.php create mode 100644 template/traits/phpfhir_xml_namespace.php rename template/{ => types}/methods/common.php (67%) rename template/{ => types}/methods/constructor.php (62%) rename template/{ => types}/methods/constructors/default.php (82%) rename template/{ => types}/methods/constructors/default_property_setter_call.php (65%) create mode 100644 template/types/methods/constructors/primitive.php rename template/{ => types}/methods/constructors/primitive_container.php (74%) rename template/{ => types}/methods/constructors/property_setter_default.php (97%) rename template/{ => types}/methods/constructors/property_setter_primitive.php (95%) rename template/{ => types}/methods/constructors/property_setter_primitive_container.php (98%) rename template/{ => types}/methods/constructors/resource_container_property_setter_call.php (98%) rename template/{ => types}/methods/contained_type.php (92%) rename template/{ => types}/properties/constants.php (82%) rename template/{ => types}/properties/declaration.php (89%) rename template/{ => types}/properties/methods.php (73%) create mode 100644 template/types/properties/methods/default.php rename template/{ => types}/properties/methods/default/getter_default.php (89%) rename template/{ => types}/properties/methods/default/getter_primitive_value.php (86%) rename template/{ => types}/properties/methods/default/setter_collection.php (93%) rename template/{ => types}/properties/methods/default/setter_contained_resource.php (93%) rename template/{ => types}/properties/methods/default/setter_contained_resource_collection.php (95%) rename template/{ => types}/properties/methods/default/setter_default.php (81%) rename template/{ => types}/properties/methods/default/setter_primitive.php (74%) create mode 100644 template/types/properties/methods/primitive.php rename template/{ => types}/properties/methods/primitive/base64_binary_type.php (75%) rename template/{ => types}/properties/methods/primitive/bool_type.php (75%) rename template/{ => types}/properties/methods/primitive/date_type.php (80%) rename template/{ => types}/properties/methods/primitive/datetime_type.php (81%) rename template/{ => types}/properties/methods/primitive/decimal_type.php (56%) rename template/{ => types}/properties/methods/primitive/instant_type.php (74%) rename template/{ => types}/properties/methods/primitive/integer_type.php (72%) rename template/{ => types}/properties/methods/primitive/string_type.php (69%) rename template/{ => types}/properties/methods/primitive/time_type.php (74%) rename template/{ => types}/properties/methods/primitive/unsigned_integer_type.php (76%) rename template/{ => types}/serialization/json.php (59%) rename template/{ => types}/serialization/json/default.php (80%) rename template/{ => types}/serialization/json/default_property_default.php (95%) rename template/{ => types}/serialization/json/default_property_primitive_list.php (94%) rename template/{ => types}/serialization/json/default_property_value_primitive_container.php (97%) rename template/{ => types}/serialization/json/primitive.php (68%) rename template/{ => types}/serialization/json/resource_container.php (87%) rename template/{ => types}/serialization/xml.php (64%) rename template/{serialization/xml/serialize/default_body.php => types/serialization/xml/serialize/body.php} (79%) rename template/{serialization/xml/serialize/default_body_typed.php => types/serialization/xml/serialize/body_typed.php} (76%) rename template/{serialization/xml/serialize/default_body_typed_default.php => types/serialization/xml/serialize/body_typed_default.php} (92%) rename template/{serialization/xml/serialize/default_body_typed_resource_container.php => types/serialization/xml/serialize/body_typed_resource_container.php} (96%) rename template/{serialization/xml/serialize/default_body_untyped.php => types/serialization/xml/serialize/body_untyped.php} (91%) rename template/{serialization/xml/serialize/default_header.php => types/serialization/xml/serialize/header.php} (95%) rename template/{ => types}/serialization/xml/serialize/resource_container.php (88%) rename template/{ => types}/serialization/xml/unserialize/body.php (84%) rename template/{ => types}/serialization/xml/unserialize/body_parse_attr_primitive.php (83%) rename template/{ => types}/serialization/xml/unserialize/body_parse_attr_typed.php (85%) rename template/{ => types}/serialization/xml/unserialize/body_parse_node_primitive.php (87%) rename template/{ => types}/serialization/xml/unserialize/body_parse_node_typed.php (77%) rename template/{ => types}/serialization/xml/unserialize/body_parse_typed_default.php (94%) rename template/{ => types}/serialization/xml/unserialize/body_parse_typed_resource_container.php (95%) rename template/{ => types}/serialization/xml/unserialize/header.php (87%) rename template/{ => types}/validation/field_map.php (87%) rename template/{ => types}/validation/methods.php (81%) rename template/{ => types}/validation/methods/collection_primitive.php (95%) rename template/{ => types}/validation/methods/collection_typed.php (94%) rename template/{ => types}/validation/methods/primitive.php (95%) rename template/{ => types}/validation/methods/typed.php (93%) diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 00000000..2016525a --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,26 @@ + version: 2 + updates: + - package-ecosystem: 'github-actions' + directory: '/' + schedule: + interval: 'weekly' + assignees: + - 'dcarbone' + groups: + github-actions: + applies-to: version-updates + patterns: + - '*' + + - package-ecosystem: 'composer' + directory: '/' + schedule: + interval: 'weekly' + assignees: + - 'dcarbone' + + groups: + composer: + applies-to: version-updates + patterns: + - '*' diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index e97ddff6..6eb73cae 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -16,7 +16,7 @@ on: pull_request: branches: - - master + - main paths: - '.github/workflows/tests.yaml' - '**.php' @@ -31,75 +31,71 @@ jobs: strategy: fail-fast: false matrix: - php-version: ["7.4", "8.0", "8.1"] - fhir-version: ["DSTU1", "DSTU2", "STU3", "R4"] + php-version: [ '8.1', '8.2', '8.3' ] + fhir-version: ['DSTU1', 'DSTU2', 'STU3', 'R4'] include: - - php-version: "7.4" - phpunit-version: "9" - paratest-version: "6" - - php-version: "8.0" - phpunit-version: "9" - paratest-version: "6" - - php-version: "8.1" - phpunit-version: "9" - paratest-version: "6" + - php-version: '8.1' + phpunit-version: '10.5' + paratest-version: '7' + - php-version: '8.2' + phpunit-version: '11.1' + paratest-version: '7' + - php-version: '8.3' + phpunit-version: '11.1' + paratest-version: '7' name: ${{ matrix.fhir-version }} - PHP ${{ matrix.php-version }} - PHPUnit ${{ matrix.phpunit-version }} steps: - - uses: actions/checkout@v3.1.0 + - uses: actions/checkout@v4 - - uses: shivammathur/setup-php@2.22.0 + - uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php-version }} extensions: libxml simplexml json zip # https://github.com/mheap/phpunit-github-actions-printer/blob/master/.github/workflows/ci.yml#L32 - - name: Set PHPUnit Version - shell: pwsh - run: | - $content = Get-Content -Path 'composer.json' | ConvertFrom-Json - $content.{require-dev}.{phpunit/phpunit} = "^${{ matrix.phpunit-version }}" - $content | ConvertTo-Json | Set-Content -Path 'composer.json' + - uses: dcarbone/install-jq-action@v2 + with: + force: true - - name: Set Paratest Version - shell: pwsh + - name: 'Set Composer Dep Versions' run: | - $content = Get-Content -Path 'composer.json' | ConvertFrom-Json - $content.{require-dev}.{brianium/paratest} = "^${{ matrix.paratest-version }}" - $content | ConvertTo-Json | Set-Content -Path 'composer.json' + tmpf="$(mktemp)" + jq -rcM '."require-dev"."phpunit/phpunit" = "^${{ matrix.phpunit-version }}" | ."require-dev"."brianium/paratest" = "^${{ matrix.paratest-version }}"' composer.json > "${tmpf}" + mv "${tmpf}" composer.json - - name: Get Composer Cache Directory + - name: 'Get Composer Cache Directory' id: composer-cache run: | - echo "::set-output name=dir::$(composer config cache-files-dir)" + echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - - uses: actions/cache@v3.0.11 + - uses: actions/cache@v4 with: path: ${{ steps.composer-cache.outputs.dir }} key: "composer-${{ runner.os }}-${{ matrix.php-version }}-${{ matrix.phpunit-version }}-${{ hashFiles('**/composer.lock') }}" - - name: Cache downloaded FHIR sources - uses: actions/cache@v2 + - name: 'Cache downloaded FHIR sources' + uses: actions/cache@v4 with: path: ./input key: "fhir-${{ runner.os }}-${{ matrix.php-version }}-${{ matrix.phpunit-version }}-${{ hashFiles('**/composer.lock') }}" - - name: Install Composer deps + - name: 'Install Composer deps' run: | composer install --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist - - name: Generate code + - name: 'Generate code' run: | ./bin/generate.sh --useExisting --versions '${{ matrix.fhir-version }}' - - uses: actions/setup-java@v3.6.0 + - uses: actions/setup-java@v4 with: distribution: 'zulu' java-version: 18 - - name: Install FHIR validator + - name: 'Install FHIR validator' run: | - wget --quiet https://github.com/hapifhir/org.hl7.fhir.core/releases/download/5.6.75/validator_cli.jar -O ./bin/validator_cli.jar + wget --quiet https://github.com/hapifhir/org.hl7.fhir.core/releases/download/6.3.4/validator_cli.jar -O ./bin/validator_cli.jar - - name: Run tests + - name: 'Run tests' run: | ./vendor/bin/paratest -c 'phpunit/${{ matrix.fhir-version }}.xml' diff --git a/README.md b/README.md index 0da681e8..b0485a25 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ configure and execute the generator. | PHPFHIR Version | PHP Versions | FHIR Versions | |-----------------|--------------|----------------------------------| | v2 | 5.4-7.4 | DSTU1, DSTU2, STU3, R4 (buildDefinition(); $builder = new \DCarbone\PHPFHIR\Builder($config, $definition); -$builder->build(); +$builder->render(); ``` Using the above code will generate class files under the included [output](./output) directory, under the namespace diff --git a/bin/config.php b/bin/config.php index 3590862c..5f6214cd 100644 --- a/bin/config.php +++ b/bin/config.php @@ -4,7 +4,7 @@ * Generator default configuration file * * Copyright 2017 Pim Koeman (pim@dataground.com) - * Copyright 2017-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2017-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/bin/generate.php b/bin/generate.php index 13d0fbaf..10d2626d 100644 --- a/bin/generate.php +++ b/bin/generate.php @@ -4,7 +4,7 @@ * Download and generation script for all major FHIR versions * * Copyright 2017 Pim Koeman (pim@dataground.com) - * Copyright 2017-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2017-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,16 +21,26 @@ namespace PHPFHIRCLI; +date_default_timezone_set('UTC'); + // --- autoload setup +const AUTOLOAD_CLASS_FILEPATH = __DIR__ . '/../vendor/autoload.php'; -date_default_timezone_set('UTC'); -require __DIR__ . '/../vendor/autoload.php'; +// ensure composer autoload class exists. +if (!file_exists(AUTOLOAD_CLASS_FILEPATH)) { + echo sprintf("Unable to locate composer autoload file expected at path: %s\n\n", AUTOLOAD_CLASS_FILEPATH); + echo "Please run \"composer install\" from the root of the project directory\n\n"; + exit(1); +} + +require AUTOLOAD_CLASS_FILEPATH; // --- use statements use DCarbone\PHPFHIR\Builder; use DCarbone\PHPFHIR\Config; use DCarbone\PHPFHIR\Definition; +use JetBrains\PhpStorm\NoReturn; use Monolog\Formatter\LineFormatter; use Monolog\Handler\StreamHandler; use Monolog\Logger; @@ -114,7 +124,7 @@ function missing_config_text(bool $return): string /** * @param bool $err */ -function exit_with_help(bool $err = false): void +#[NoReturn] function exit_with_help(bool $err = false): void { global $config_location_def; $env_var = ENV_GENERATE_CONFIG_FILE; @@ -122,7 +132,7 @@ function exit_with_help(bool $err = false): void PHP-FHIR: Tools for creating PHP classes from the HL7 FHIR Specification -Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) +Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) - Links: Source: https://github.com/dcarbone/php-fhir @@ -178,7 +188,7 @@ function ask(string $q): bool foreach ($ins as $in) { $resp = stream_get_line($in, 25, "\n"); if (is_string($resp)) { - return substr(strtolower($resp), 0, 1) === 'y'; + return str_starts_with(strtolower($resp), 'y'); } return false; } @@ -210,7 +220,7 @@ function is_dir_empty(string $dir): bool { $res = glob($dir, GLOB_NOSORT); foreach ($res as $r) { - if (0 === strpos($r, '.')) { + if (str_starts_with($r, '.')) { continue; } return false; @@ -230,7 +240,7 @@ function is_dir_empty(string $dir): bool } else { $next = trim($argv[$i + 1]); } - if (false !== strpos($arg, '=')) { + if (str_contains($arg, '=')) { list($arg, $next) = explode('=', $arg, 2); $found_equal = true; } @@ -326,6 +336,7 @@ function is_dir_empty(string $dir): bool exit(1); } +// determine if monolog is present, otherwise use null logger if (class_exists('\\Monolog\\Logger')) { $formatter = new LineFormatter(LineFormatter::SIMPLE_FORMAT); $handler = new StreamHandler('php://stdout', $log_level); @@ -436,7 +447,7 @@ function is_dir_empty(string $dir): bool } if ($unzip) { - if (class_exists('\\ZipArchive', true)) { + if (class_exists('\\ZipArchive')) { echo "ext-zip found\n"; $zip = new \ZipArchive(); @@ -480,11 +491,11 @@ function is_dir_empty(string $dir): bool $builder = new Builder($build_config, $definition); if ($only_library) { - $builder->buildFHIRClasses(); + $builder->renderFHIRClasses(); } elseif ($only_tests) { - $builder->buildTestClasses(); + $builder->renderTestClasses(); } else { - $builder->build(); + $builder->render(); } } diff --git a/composer.json b/composer.json index f97787f7..1859d088 100644 --- a/composer.json +++ b/composer.json @@ -6,8 +6,9 @@ "homepage": "https://github.com/dcarbone/php-fhir", "authors": [ { - "name": "Daniel Paul Carbone", - "email": "daniel.p.carbone@gmail.com" + "name": "Daniel Carbone", + "email": "daniel.p.carbone@gmail.com", + "homepage": "https://github.com/dcarbone" } ], "keywords": [ @@ -18,19 +19,18 @@ "php-fhir" ], "require": { - "php": "7.4.* || 8.*", + "php": "^8.1", "ext-simplexml": "*", "ext-json": "*", "ext-libxml": "*", "ext-dom": "*", - "myclabs/php-enum": "^1.8", - "psr/log": "^1.1 || ^3.0" + "psr/log": "^3.0" }, "require-dev": { "ext-curl": "*", - "phpunit/phpunit": "^9.5", - "brianium/paratest": "^v6.4", - "monolog/monolog": "^2.8.0 || ^3.2.0" + "phpunit/phpunit": "^11.0", + "brianium/paratest": "^v7.4", + "monolog/monolog": "^3.2.0" }, "replace": { "php-fhir/parser": "*", diff --git a/files/constants.php b/files/constants.php index b86dc39e..e55958ce 100644 --- a/files/constants.php +++ b/files/constants.php @@ -1,7 +1,7 @@ $config(%s)', $requiredFile, - \DCarbone\PHPFHIR\Config\VersionConfig::class, + VersionConfig::class, )); } // unset vars defined by this func diff --git a/src/Builder.php b/src/Builder.php index d10676c6..09f4c4ef 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,7 @@ */ use DCarbone\PHPFHIR\Config\VersionConfig; -use DCarbone\PHPFHIR\Generator\TemplateBuilder; +use DCarbone\PHPFHIR\Render\Templates; use DCarbone\PHPFHIR\Utilities\CopyrightUtils; use DCarbone\PHPFHIR\Utilities\FileUtils; use RuntimeException; @@ -75,7 +75,7 @@ public function getDefinition(): Definition * Generate FHIR classes only. * @throws \ErrorException */ - public function buildFHIRClasses(): void + public function renderFHIRClasses(): void { set_error_handler(function ($errNum, $errStr, $errFile, $errLine) { throw new \ErrorException($errStr, $errNum, 1, $errFile, $errLine); @@ -83,11 +83,11 @@ public function buildFHIRClasses(): void $log = $this->config->getLogger(); - $this->beforeGeneration(); + $this->prerender(); $definition = $this->getDefinition(); - $this->staticClassGeneration(); + $this->renderStaticClasses(); $types = $definition->getTypes(); @@ -97,9 +97,9 @@ public function buildFHIRClasses(): void // TODO: eventually merge "raw" into typical workflow? if (PHPFHIR_XHTML_TYPE_NAME === $type->getFHIRName()) { - $classDefinition = TemplateBuilder::generateRawTypeClass($this->config, $types, $type); + $classDefinition = Templates::renderRawTypeClass($this->config, $types, $type); } else { - $classDefinition = TemplateBuilder::generateTypeClass($this->config, $types, $type); + $classDefinition = Templates::renderTypeClass($this->config, $types, $type); } $filepath = FileUtils::buildTypeFilePath($this->config, $type); if (!(bool)file_put_contents($filepath, $classDefinition)) { @@ -120,11 +120,11 @@ public function buildFHIRClasses(): void /** * Generate Test classes only. Tests will not pass if FHIR classes have not been built. */ - public function buildTestClasses(): void + public function renderTestClasses(): void { $log = $this->config->getLogger(); - $this->beforeGeneration(); + $this->prerender(); $definition = $this->getDefinition(); $types = $definition->getTypes(); @@ -137,7 +137,7 @@ public function buildTestClasses(): void $this->config->getTestsNamespace(PHPFHIR_TEST_TYPE_BASE, true), PHPFHIR_TEST_CLASSNAME_CONSTANTS ), - TemplateBuilder::generateConstantsTestClass($this->config, $types) + Templates::renderConstantsTestClass($this->config, $types) ); $this->writeClassFile( @@ -146,7 +146,7 @@ public function buildTestClasses(): void $this->config->getTestsNamespace(PHPFHIR_TEST_TYPE_BASE, true), PHPFHIR_TEST_CLASSNAME_TYPEMAP ), - TemplateBuilder::generateTypeMapTestClass($this->config, $types) + Templates::renderTypeMapTestClass($this->config, $types) ); $testTypes = [PHPFHIR_TEST_TYPE_UNIT]; @@ -159,7 +159,7 @@ public function buildTestClasses(): void continue; } $log->debug("Generated ${testType} test class for type {$type}..."); - $classDefinition = TemplateBuilder::generateTypeTestClass($this->config, $types, $type, $testType); + $classDefinition = Templates::renderTypeTestClass($this->config, $types, $type, $testType); $filepath = FileUtils::buildTypeTestFilePath($this->config, $type, $testType); if (!(bool)file_put_contents($filepath, $classDefinition)) { throw new RuntimeException( @@ -181,30 +181,30 @@ public function buildTestClasses(): void * Generate FHIR object classes based on XSD * @throws \ErrorException */ - public function build(): void + public function render(): void { - $this->beforeGeneration(); + $this->prerender(); - $this->buildFHIRClasses(); + $this->renderFHIRClasses(); if (!$this->config->isSkipTests()) { - $this->buildTestClasses(); + $this->renderTestClasses(); } - $this->staticClassGeneration(); + $this->renderStaticClasses(); } /** * Commands to run prior to class generation */ - protected function beforeGeneration(): void + protected function prerender(): void { // Initialize some classes and things. if (!$this->preGenerationCompleted) { - $this->log->startBreak('Generator Class Initialization'); + $this->log->startBreak('Prerender'); $this->log->info('Compiling Copyrights...'); CopyrightUtils::compileCopyrights($this->config); - $this->log->endBreak('Generator Class Initialization'); + $this->log->endBreak('Prerender Class Initialization'); $this->preGenerationCompleted = true; } } @@ -228,10 +228,7 @@ private function writeClassFile(string $filePath, string $fileContents): void $this->log->debug(sprintf('%d bytes written to file %s', $b, $filePath)); } - /** - * Commands to run after class generation - */ - protected function staticClassGeneration(): void + protected function renderStaticClasses(): void { $types = $this->definition->getTypes(); @@ -241,7 +238,7 @@ protected function staticClassGeneration(): void $this->config->getNamespace(true), PHPFHIR_CLASSNAME_CONSTANTS ), - TemplateBuilder::generateConstants($this->config, $types) + Templates::renderConstants($this->config, $types) ); $this->writeClassFile( @@ -250,7 +247,7 @@ protected function staticClassGeneration(): void $this->config->getNamespace(true), PHPFHIR_CLASSNAME_TYPEMAP ), - TemplateBuilder::generateTypeMapClass($this->config, $types) + Templates::renderTypeMapClass($this->config, $types) ); $this->writeClassFile( @@ -259,7 +256,7 @@ protected function staticClassGeneration(): void $this->config->getNamespace(true), PHPFHIR_CLASSNAME_AUTOLOADER ), - TemplateBuilder::generateAutoloaderClass($this->config, $types) + Templates::renderAutoloaderClass($this->config, $types) ); $this->writeClassFile( @@ -268,7 +265,7 @@ protected function staticClassGeneration(): void $this->config->getNamespace(true), PHPFHIR_INTERFACE_TYPE ), - TemplateBuilder::generatePHPFHIRTypeInterface($this->config, $types) + Templates::renderPHPFHIRTypeInterface($this->config, $types) ); $this->writeClassFile( @@ -277,7 +274,7 @@ protected function staticClassGeneration(): void $this->config->getNamespace(true), PHPFHIR_INTERFACE_CONTAINED_TYPE ), - TemplateBuilder::generatePHPFHIRContainedTypeInterface($this->config, $types) + Templates::renderPHPFHIRContainedTypeInterface($this->config, $types) ); $this->writeClassFile( @@ -286,7 +283,7 @@ protected function staticClassGeneration(): void $this->config->getNamespace(true), PHPFHIR_INTERFACE_COMMENT_CONTAINER ), - TemplateBuilder::generatePHPFHIRCommentContainerInterface($this->config, $types) + Templates::renderPHPFHIRCommentContainerInterface($this->config, $types) ); $this->writeClassFile( @@ -295,7 +292,7 @@ protected function staticClassGeneration(): void $this->config->getNamespace(true), PHPFHIR_TRAIT_COMMENT_CONTAINER ), - TemplateBuilder::generatePHPFHIRCommentContainerTrait($this->config, $types) + Templates::renderPHPFHIRCommentContainerTrait($this->config, $types) ); $this->writeClassFile( @@ -304,7 +301,7 @@ protected function staticClassGeneration(): void $this->config->getNamespace(true), PHPFHIR_TRAIT_VALIDATION_ASSERTIONS ), - TemplateBuilder::generatePHPFHIRValidationAssertionsTrait($this->config, $types) + Templates::renderPHPFHIRValidationAssertionsTrait($this->config, $types) ); $this->writeClassFile( @@ -313,7 +310,16 @@ protected function staticClassGeneration(): void $this->config->getNamespace(true), PHPFHIR_TRAIT_CHANGE_TRACKING ), - TemplateBuilder::generatePHPFHIRChangeTrackingTrait($this->config, $types) + Templates::renderPHPFHIRChangeTrackingTrait($this->config, $types) + ); + + $this->writeClassFile( + FileUtils::buildGenericFilePath( + $this->config, + $this->config->getNamespace(true), + PHPFHIR_TRAIT_XMLNS + ), + Templates::renderPHPFHIRXMLNamespaceTrait($this->config, $types) ); $this->writeClassFile( @@ -322,7 +328,7 @@ protected function staticClassGeneration(): void $this->config->getNamespace(true), PHPFHIR_CLASSNAME_RESPONSE_PARSER_CONFIG ), - TemplateBuilder::generatePHPFHIRResponseParserConfigClass($this->config, $types) + Templates::renderPHPFHIRResponseParserConfigClass($this->config, $types) ); $this->writeClassFile( @@ -331,7 +337,7 @@ protected function staticClassGeneration(): void $this->config->getNamespace(true), PHPFHIR_CLASSNAME_RESPONSE_PARSER ), - TemplateBuilder::generatePHPFHIRResponseParserClass($this->config, $types) + Templates::renderPHPFHIRResponseParserClass($this->config, $types) ); } } diff --git a/src/Builder/TypeBuilder.php b/src/Builder/TypeBuilder.php index 6b5a47c7..e705f9f0 100644 --- a/src/Builder/TypeBuilder.php +++ b/src/Builder/TypeBuilder.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Builder; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -74,7 +74,7 @@ private static function buildPrimitiveType( */ public static function build(VersionConfig $config, string $fhirName, SimpleXMLElement $sxe, string $sourceFilename): Type { - if (false !== strpos($fhirName, PHPFHIR_PRIMITIVE_SUFFIX) || false !== strpos($fhirName, PHPFHIR_LIST_SUFFIX)) { + if (str_contains($fhirName, PHPFHIR_PRIMITIVE_SUFFIX) || str_contains($fhirName, PHPFHIR_LIST_SUFFIX)) { return self::buildPrimitiveType($config, $fhirName, $sxe, $sourceFilename); } return self::buildDefaultType($config, $fhirName, $sxe, $sourceFilename); diff --git a/src/Config.php b/src/Config.php index 852922d0..d54494d7 100644 --- a/src/Config.php +++ b/src/Config.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,8 +18,258 @@ * limitations under the License. */ -if (80000 <= PHP_VERSION_ID) { - require __DIR__ . '/Config/Config80.php'; -} else { - require __DIR__ . '/Config/Config74.php'; +use DCarbone\PHPFHIR\Config\Version; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; + +/** + * Class Config + * @package DCarbone\PHPFHIR + */ +class Config +{ + public const KEY_SCHEMA_PATH = 'schemaPath'; + public const KEY_CLASSES_PATH = 'classesPath'; + public const KEY_VERSIONS = 'versions'; + public const KEY_SILENT = 'silent'; + public const KEY_SKIP_TESTS = 'skipTests'; + public const KEY_LIBXML_OPTS = 'libxmlOpts'; + + /** @var \DCarbone\PHPFHIR\Logger */ + protected Logger $_log; + + /** @var string */ + private string $schemaPath; + + /** @var string */ + private string $classesPath = PHPFHIR_DEFAULT_OUTPUT_DIR; + /** @var \DCarbone\PHPFHIR\Config\Version[] */ + private array $versions = []; + + /** @var bool */ + private bool $silent = false; + /** @var bool */ + private bool $skipTests = false; + /** @var int|null */ + private ?int $libxmlOpts; + + /** + * @param \Psr\Log\LoggerInterface $logger + */ + public function setLogger(LoggerInterface $logger): void + { + $this->_log = new Logger($logger); + } + + /** + * Config constructor. + * @param array $conf + * @param \Psr\Log\LoggerInterface|null $logger + */ + public function __construct(array $conf = [], LoggerInterface $logger = null) + { + if (!isset($conf[self::KEY_SCHEMA_PATH])) { + throw new \DomainException('Required configuration key "' . self::KEY_SCHEMA_PATH . '" missing'); + } + if (!isset($conf[self::KEY_CLASSES_PATH])) { + throw new \DomainException('Required configuration key "' . self::KEY_CLASSES_PATH . '" missing'); + } + if (!isset($conf[self::KEY_VERSIONS]) || !is_array($conf[self::KEY_VERSIONS]) || 0 == count( + $conf[self::KEY_VERSIONS] + )) { + throw new \DomainException( + 'Configuration key "' . self::KEY_VERSIONS . '" must be an array with at least 1 configured version.' + ); + } + $this->setSchemaPath($conf[self::KEY_SCHEMA_PATH]); + $this->setClassesPath($conf[self::KEY_CLASSES_PATH]); + $this->setVersions($conf[self::KEY_VERSIONS]); + $this->setSilent(isset($conf[self::KEY_SILENT]) && (bool)$conf[self::KEY_SILENT]); + $this->setSkipTests($conf[self::KEY_SKIP_TESTS] ?? false); + $this->setLibxmlOpts($conf[self::KEY_LIBXML_OPTS] ?? null); + if ($logger && !$this->isSilent()) { + $this->_log = new Logger($logger); + } else { + $this->_log = new Logger(new NullLogger()); + } + } + + /** + * @return bool + */ + public function isSilent(): bool + { + return $this->silent; + } + + /** + * @param bool $silent + * @return static + */ + public function setSilent(bool $silent): self + { + $this->silent = $silent; + return $this; + } + + /** + * @return bool + */ + public function isSkipTests(): bool + { + return $this->skipTests; + } + + /** + * @param bool $skipTests + * @return static + */ + public function setSkipTests(bool $skipTests): self + { + $this->skipTests = $skipTests; + return $this; + } + + /** + * @return int|null + */ + public function getLibxmlOpts(): ?int + { + return $this->libxmlOpts; + } + + /** + * @param int|null $libxmlOpts + * @return static + */ + public function setLibxmlOpts(?int $libxmlOpts): self + { + $this->libxmlOpts = $libxmlOpts; + return $this; + } + + /** + * @return \DCarbone\PHPFHIR\Logger + */ + public function getLogger(): Logger + { + return $this->_log; + } + + /** + * @return string + */ + public function getSchemaPath(): string + { + return $this->schemaPath; + } + + /** + * @param string $schemaPath + * @return $this + */ + public function setSchemaPath(string $schemaPath): self + { + // Bunch'o validation + if (false === is_dir($schemaPath)) { + throw new \RuntimeException('Unable to locate XSD dir "' . $schemaPath . '"'); + } + if (false === is_readable($schemaPath)) { + throw new \RuntimeException('This process does not have read access to directory "' . $schemaPath . '"'); + } + $this->schemaPath = rtrim($schemaPath, "/\\"); + return $this; + } + + /** + * @return string + */ + public function getClassesPath(): string + { + return $this->classesPath; + } + + /** + * @param string $classesPath + * @return $this + */ + public function setClassesPath(string $classesPath): self + { + if (!is_dir($classesPath)) { + throw new \RuntimeException('Unable to locate output dir "' . $classesPath . '"'); + } + if (!is_writable($classesPath)) { + throw new \RuntimeException( + sprintf( + 'Specified output path "%s" is not writable by this process.', + $classesPath + ) + ); + } + if (!is_readable($classesPath)) { + throw new \RuntimeException( + sprintf( + 'Specified output path "%s" is not readable by this process.', + $classesPath + ) + ); + } + $this->classesPath = $classesPath; + return $this; + } + + /** + * @param array $versions + * @return $this + */ + public function setVersions(array $versions): self + { + $this->versions = []; + foreach ($versions as $name => $version) { + $this->versions[$name] = ($version instanceof Version) ? $version : new Version($name, $version); + } + return $this; + } + + /** + * @return \DCarbone\PHPFHIR\Config\Version[] + */ + public function getVersions(): array + { + return $this->versions; + } + + /** + * @param string $version + * @return bool + */ + public function hasVersion(string $version): bool + { + return isset($this->versions[$version]); + } + + /** + * @param string $version + * @return \DCarbone\PHPFHIR\Config\Version + */ + public function getVersion(string $version): Version + { + if (!$this->hasVersion($version)) { + throw new \OutOfBoundsException( + 'No version with name "' . $version . '" has been configured. Available: ["' . implode( + '", "', + array_keys($this->versions) + ) . '"]' + ); + } + return $this->versions[$version]; + } + + /** + * @return array + */ + public function listVersions(): array + { + return array_keys($this->versions); + } } \ No newline at end of file diff --git a/src/Config/AbstractConfig.php b/src/Config/AbstractConfig.php deleted file mode 100644 index 897c8c75..00000000 --- a/src/Config/AbstractConfig.php +++ /dev/null @@ -1,264 +0,0 @@ -setSchemaPath($conf[self::KEY_SCHEMA_PATH]); - $this->setClassesPath($conf[self::KEY_CLASSES_PATH]); - $this->setVersions($conf[self::KEY_VERSIONS]); - $this->setSilent(isset($conf[self::KEY_SILENT]) && (bool)$conf[self::KEY_SILENT]); - $this->setSkipTests($conf[self::KEY_SKIP_TESTS] ?? false); - $this->setLibxmlOpts($conf[self::KEY_LIBXML_OPTS] ?? null); - if ($logger && !$this->isSilent()) { - $this->_log = new Logger($logger); - } else { - $this->_log = new Logger(new NullLogger()); - } - } - - /** - * @return bool - */ - public function isSilent(): bool - { - return $this->silent; - } - - /** - * @param bool $silent - * @return static - */ - public function setSilent(bool $silent): AbstractConfig - { - $this->silent = $silent; - return $this; - } - - /** - * @return bool - */ - public function isSkipTests(): bool - { - return $this->skipTests; - } - - /** - * @param bool $skipTests - * @return static - */ - public function setSkipTests(bool $skipTests): AbstractConfig - { - $this->skipTests = $skipTests; - return $this; - } - - /** - * @return int|null - */ - public function getLibxmlOpts(): ?int - { - return $this->libxmlOpts; - } - - /** - * @param int|null $libxmlOpts - * @return static - */ - public function setLibxmlOpts(?int $libxmlOpts): AbstractConfig - { - $this->libxmlOpts = $libxmlOpts; - return $this; - } - - /** - * @return \DCarbone\PHPFHIR\Logger - */ - public function getLogger(): Logger - { - return $this->_log; - } - - /** - * @return string - */ - public function getSchemaPath(): string - { - return $this->schemaPath; - } - - /** - * @param string $schemaPath - * @return $this - */ - public function setSchemaPath(string $schemaPath): AbstractConfig - { - // Bunch'o validation - if (false === is_dir($schemaPath)) { - throw new \RuntimeException('Unable to locate XSD dir "' . $schemaPath . '"'); - } - if (false === is_readable($schemaPath)) { - throw new \RuntimeException('This process does not have read access to directory "' . $schemaPath . '"'); - } - $this->schemaPath = rtrim($schemaPath, "/\\"); - return $this; - } - - /** - * @return string - */ - public function getClassesPath(): string - { - return $this->classesPath; - } - - /** - * @param string $classesPath - * @return $this - */ - public function setClassesPath(string $classesPath): AbstractConfig - { - if (!is_dir($classesPath)) { - throw new \RuntimeException('Unable to locate output dir "' . $classesPath . '"'); - } - if (!is_writable($classesPath)) { - throw new \RuntimeException( - sprintf( - 'Specified output path "%s" is not writable by this process.', - $classesPath - ) - ); - } - if (!is_readable($classesPath)) { - throw new \RuntimeException( - sprintf( - 'Specified output path "%s" is not readable by this process.', - $classesPath - ) - ); - } - $this->classesPath = $classesPath; - return $this; - } - - /** - * @param array $versions - * @return $this - */ - public function setVersions(array $versions): AbstractConfig - { - $this->versions = []; - foreach ($versions as $name => $version) { - $this->versions[$name] = ($version instanceof Version) ? $version : new Version($name, $version); - } - return $this; - } - - /** - * @return \DCarbone\PHPFHIR\Config\Version[] - */ - public function getVersions(): array - { - return $this->versions; - } - - /** - * @param string $version - * @return bool - */ - public function hasVersion(string $version): bool - { - return isset($this->versions[$version]); - } - - /** - * @param string $version - * @return \DCarbone\PHPFHIR\Config\Version - */ - public function getVersion(string $version): Version - { - if (!$this->hasVersion($version)) { - throw new \OutOfBoundsException( - 'No version with name "' . $version . '" has been configured. Available: ["' . implode( - '", "', - array_keys($this->versions) - ) . '"]' - ); - } - return $this->versions[$version]; - } - - /** - * @return array - */ - public function listVersions(): array - { - return array_keys($this->versions); - } -} \ No newline at end of file diff --git a/src/Config/Config80.php b/src/Config/Config80.php deleted file mode 100644 index d1d348e0..00000000 --- a/src/Config/Config80.php +++ /dev/null @@ -1,37 +0,0 @@ -_log = new Logger($logger); - } -} \ No newline at end of file diff --git a/src/Config/Version.php b/src/Config/Version.php index e02a4143..1e55c6d0 100644 --- a/src/Config/Version.php +++ b/src/Config/Version.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Config; /* - * Copyright 2018-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2018-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -111,7 +111,7 @@ public function setNamespace(?string $namespace): Version return $this; } // handle no or empty namespace - $namespace = trim((string)$namespace, PHPFHIR_NAMESPACE_TRIM_CUTSET); + $namespace = trim($namespace, PHPFHIR_NAMESPACE_TRIM_CUTSET); if ('' === $namespace) { $this->namespace = ''; return $this; diff --git a/src/Config/VersionConfig.php b/src/Config/VersionConfig.php index d6a833ad..60220d65 100644 --- a/src/Config/VersionConfig.php +++ b/src/Config/VersionConfig.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Config; /* - * Copyright 2018-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2018-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/Definition.php b/src/Definition.php index 2d418420..168b6752 100644 --- a/src/Definition.php +++ b/src/Definition.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/Definition/Decorator/AnnotationElementPropertyTypeDecorator.php b/src/Definition/Decorator/AnnotationElementPropertyTypeDecorator.php index cb746986..8a001f59 100644 --- a/src/Definition/Decorator/AnnotationElementPropertyTypeDecorator.php +++ b/src/Definition/Decorator/AnnotationElementPropertyTypeDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,7 @@ use DCarbone\PHPFHIR\Definition\Property; use DCarbone\PHPFHIR\Definition\Type; use DCarbone\PHPFHIR\Definition\Types; -use DCarbone\PHPFHIR\Enum\ElementNameEnum; +use DCarbone\PHPFHIR\Enum\ElementName; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; use DCarbone\PHPFHIR\Utilities\TypeBuilderUtils; @@ -55,15 +55,10 @@ public static function decorate( foreach ($annotation->children('xs', true) as $child) { switch ($child->getName()) { - case ElementNameEnum::DOCUMENTATION: - TypeBuilderUtils::setPropertyStringFromElementValue( - $property, - $annotation, - $child, - 'addDocumentationFragment' - ); + case ElementName::DOCUMENTATION->value: + $property->addDocumentationFragment((string)$child); break; - case ElementNameEnum::COMPLEX_CONTENT: + case ElementName::COMPLEX_CONTENT->value: ComplexContentElementTypeDecorator::decorate($config, $types, $type, $child); break; diff --git a/src/Definition/Decorator/AnnotationElementTypeDecorator.php b/src/Definition/Decorator/AnnotationElementTypeDecorator.php index a5aab9bc..2976df96 100644 --- a/src/Definition/Decorator/AnnotationElementTypeDecorator.php +++ b/src/Definition/Decorator/AnnotationElementTypeDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,7 @@ use DCarbone\PHPFHIR\Config\VersionConfig; use DCarbone\PHPFHIR\Definition\Type; use DCarbone\PHPFHIR\Definition\Types; -use DCarbone\PHPFHIR\Enum\ElementNameEnum; +use DCarbone\PHPFHIR\Enum\ElementName; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; use DCarbone\PHPFHIR\Utilities\TypeBuilderUtils; @@ -50,7 +50,7 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, // parse through child elements foreach ($annotation->children('xs', true) as $child) { switch ($child->getName()) { - case ElementNameEnum::DOCUMENTATION: + case ElementName::DOCUMENTATION->value: TypeBuilderUtils::setTypeStringFromElementValue( $type, $annotation, @@ -58,7 +58,7 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, 'addDocumentationFragment' ); break; - case ElementNameEnum::COMPLEX_CONTENT: + case ElementName::COMPLEX_CONTENT->value: ComplexContentElementTypeDecorator::decorate($config, $types, $type, $child); break; diff --git a/src/Definition/Decorator/AnyElementTypeDecorator.php b/src/Definition/Decorator/AnyElementTypeDecorator.php index 6194bf32..22922c0e 100644 --- a/src/Definition/Decorator/AnyElementTypeDecorator.php +++ b/src/Definition/Decorator/AnyElementTypeDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,7 @@ use DCarbone\PHPFHIR\Definition\Property; use DCarbone\PHPFHIR\Definition\Type; use DCarbone\PHPFHIR\Definition\Types; -use DCarbone\PHPFHIR\Enum\AttributeNameEnum; +use DCarbone\PHPFHIR\Enum\AttributeName; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; /** @@ -46,13 +46,13 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, // parse through attributes foreach ($any->attributes() as $attribute) { switch ($attrName = $attribute->getName()) { - case AttributeNameEnum::NAMESPACE: + case AttributeName::NAMESPACE->value: $property->setNamespace((string)$attribute); break; - case AttributeNameEnum::MIN_OCCURS: + case AttributeName::MIN_OCCURS->value: $property->setMinOccurs(intval((string)$attribute)); break; - case AttributeNameEnum::MAX_OCCURS: + case AttributeName::MAX_OCCURS->value: $property->setMaxOccurs((string)$attribute); break; diff --git a/src/Definition/Decorator/AttributeElementTypeDecorator.php b/src/Definition/Decorator/AttributeElementTypeDecorator.php index 3fb655a2..f53d1a1f 100644 --- a/src/Definition/Decorator/AttributeElementTypeDecorator.php +++ b/src/Definition/Decorator/AttributeElementTypeDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,9 +22,9 @@ use DCarbone\PHPFHIR\Definition\Property; use DCarbone\PHPFHIR\Definition\Type; use DCarbone\PHPFHIR\Definition\Types; -use DCarbone\PHPFHIR\Enum\AttributeNameEnum; -use DCarbone\PHPFHIR\Enum\ElementNameEnum; -use DCarbone\PHPFHIR\Enum\PropertyUseEnum; +use DCarbone\PHPFHIR\Enum\AttributeName; +use DCarbone\PHPFHIR\Enum\ElementName; +use DCarbone\PHPFHIR\Enum\PropertyUse; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; /** @@ -51,16 +51,16 @@ public static function decorate( // parse through attributes foreach ($attributeElement->attributes() as $attribute) { switch ($attribute->getName()) { - case AttributeNameEnum::NAME: + case AttributeName::NAME->value: $property->setName((string)$attribute); break; - case AttributeNameEnum::TYPE: + case AttributeName::TYPE->value: $property->setValueFHIRTypeName((string)$attribute); break; - case AttributeNameEnum::USE: - $property->setUse(new PropertyUseEnum((string)$attribute)); + case AttributeName::USE->value: + $property->setUse(PropertyUse::from((string)$attribute)); break; - case AttributeNameEnum::FIXED: + case AttributeName::FIXED->value: $property->setFixed((string)$attribute); break; @@ -72,7 +72,7 @@ public static function decorate( // parse through child elements foreach ($attributeElement->children('xs', true) as $child) { switch ($child->getName()) { - case ElementNameEnum::SIMPLE_TYPE: + case ElementName::SIMPLE_TYPE->value: SimpleTypeElementPropertyDecorator::decorate($config, $types, $type, $property, $child); break; diff --git a/src/Definition/Decorator/ChoiceElementElementPropertyDecorator.php b/src/Definition/Decorator/ChoiceElementElementPropertyDecorator.php index 5cbf0571..995546d0 100644 --- a/src/Definition/Decorator/ChoiceElementElementPropertyDecorator.php +++ b/src/Definition/Decorator/ChoiceElementElementPropertyDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,7 @@ use DCarbone\PHPFHIR\Definition\Property; use DCarbone\PHPFHIR\Definition\Type; use DCarbone\PHPFHIR\Definition\Types; -use DCarbone\PHPFHIR\Enum\AttributeNameEnum; +use DCarbone\PHPFHIR\Enum\AttributeName; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; /** @@ -69,19 +69,19 @@ public static function decorate( foreach ($element->attributes() as $attribute) { switch ($attribute->getName()) { - case AttributeNameEnum::REF: + case AttributeName::REF->value: $property->setRef((string)$attribute); break; - case AttributeNameEnum::NAME: + case AttributeName::NAME->value: $property->setName((string)$attribute); break; - case AttributeNameEnum::TYPE: + case AttributeName::TYPE->value: $property->setValueFHIRTypeName((string)$attribute); break; - case AttributeNameEnum::MIN_OCCURS: + case AttributeName::MIN_OCCURS->value: $property->setMinOccurs(intval((string)$attribute)); break; - case AttributeNameEnum::MAX_OCCURS: + case AttributeName::MAX_OCCURS->value: $property->setMaxOccurs((string)$attribute); break; diff --git a/src/Definition/Decorator/ChoiceElementTypeDecorator.php b/src/Definition/Decorator/ChoiceElementTypeDecorator.php index 9846626c..0724a457 100644 --- a/src/Definition/Decorator/ChoiceElementTypeDecorator.php +++ b/src/Definition/Decorator/ChoiceElementTypeDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,8 +21,8 @@ use DCarbone\PHPFHIR\Config\VersionConfig; use DCarbone\PHPFHIR\Definition\Type; use DCarbone\PHPFHIR\Definition\Types; -use DCarbone\PHPFHIR\Enum\AttributeNameEnum; -use DCarbone\PHPFHIR\Enum\ElementNameEnum; +use DCarbone\PHPFHIR\Enum\AttributeName; +use DCarbone\PHPFHIR\Enum\ElementName; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; /** @@ -43,10 +43,10 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, $maxOccurs = null; foreach ($choice->attributes() as $attribute) { switch ($attribute->getName()) { - case AttributeNameEnum::MIN_OCCURS: + case AttributeName::MIN_OCCURS->value: $minOccurs = (string)$attribute; break; - case AttributeNameEnum::MAX_OCCURS: + case AttributeName::MAX_OCCURS->value: $maxOccurs = (string)$attribute; break; @@ -58,11 +58,11 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, $annotationElement = null; foreach ($choice->children('xs', true) as $child) { switch ($child->getName()) { - case ElementNameEnum::ANNOTATION: + case ElementName::ANNOTATION->value: $annotationElement = $child; break; - case ElementNameEnum::ELEMENT: + case ElementName::ELEMENT->value: ChoiceElementElementPropertyDecorator::decorate( $config, $types, diff --git a/src/Definition/Decorator/ComplexContentElementTypeDecorator.php b/src/Definition/Decorator/ComplexContentElementTypeDecorator.php index b3002634..a0d95246 100644 --- a/src/Definition/Decorator/ComplexContentElementTypeDecorator.php +++ b/src/Definition/Decorator/ComplexContentElementTypeDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,7 @@ use DCarbone\PHPFHIR\Config\VersionConfig; use DCarbone\PHPFHIR\Definition\Type; use DCarbone\PHPFHIR\Definition\Types; -use DCarbone\PHPFHIR\Enum\ElementNameEnum; +use DCarbone\PHPFHIR\Enum\ElementName; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; use SimpleXMLElement; @@ -55,13 +55,13 @@ public static function decorate( // parse through child elements foreach ($complexContent->children('xs', true) as $child) { switch ($child->getName()) { - case ElementNameEnum::ANNOTATION: + case ElementName::ANNOTATION->value: AnnotationElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::EXTENSION: + case ElementName::EXTENSION->value: ExtensionElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::RESTRICTION: + case ElementName::RESTRICTION->value: RestrictionElementTypeDecorator::decorate($config, $types, $type, $child); break; diff --git a/src/Definition/Decorator/ComplexTypeElementTypeDecorator.php b/src/Definition/Decorator/ComplexTypeElementTypeDecorator.php index e4bd4e80..39874a7e 100644 --- a/src/Definition/Decorator/ComplexTypeElementTypeDecorator.php +++ b/src/Definition/Decorator/ComplexTypeElementTypeDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,8 +21,8 @@ use DCarbone\PHPFHIR\Config\VersionConfig; use DCarbone\PHPFHIR\Definition\Type; use DCarbone\PHPFHIR\Definition\Types; -use DCarbone\PHPFHIR\Enum\AttributeNameEnum; -use DCarbone\PHPFHIR\Enum\ElementNameEnum; +use DCarbone\PHPFHIR\Enum\AttributeName; +use DCarbone\PHPFHIR\Enum\ElementName; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; use SimpleXMLElement; @@ -43,9 +43,9 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, // parse through attributes foreach ($complexType->attributes() as $attribute) { switch ($attribute->getName()) { - case AttributeNameEnum::NAME: + case AttributeName::NAME->value: continue 2; - case AttributeNameEnum::MIXED: + case AttributeName::MIXED->value: $type->setMixed(boolval((string)$attribute)); break; @@ -56,22 +56,22 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, foreach ($complexType->children('xs', true) as $child) { switch ($child->getName()) { - case ElementNameEnum::ANNOTATION: + case ElementName::ANNOTATION->value: AnnotationElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::COMPLEX_CONTENT: + case ElementName::COMPLEX_CONTENT->value: ComplexContentElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::SEQUENCE: + case ElementName::SEQUENCE->value: SequenceElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::ATTRIBUTE: + case ElementName::ATTRIBUTE->value: AttributeElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::SIMPLE_CONTENT: + case ElementName::SIMPLE_CONTENT->value: SimpleContentElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::CHOICE: + case ElementName::CHOICE->value: ChoiceElementTypeDecorator::decorate($config, $types, $type, $child); break; diff --git a/src/Definition/Decorator/ElementElementTypeDecorator.php b/src/Definition/Decorator/ElementElementTypeDecorator.php index 5c6c6745..36081469 100644 --- a/src/Definition/Decorator/ElementElementTypeDecorator.php +++ b/src/Definition/Decorator/ElementElementTypeDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,8 +22,8 @@ use DCarbone\PHPFHIR\Definition\Property; use DCarbone\PHPFHIR\Definition\Type; use DCarbone\PHPFHIR\Definition\Types; -use DCarbone\PHPFHIR\Enum\AttributeNameEnum; -use DCarbone\PHPFHIR\Enum\ElementNameEnum; +use DCarbone\PHPFHIR\Enum\AttributeName; +use DCarbone\PHPFHIR\Enum\ElementName; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; use SimpleXMLElement; @@ -45,9 +45,9 @@ public static function rootDecorate(VersionConfig $config, Types $types, Type $t { foreach ($element->attributes() as $attribute) { switch ($attribute->getName()) { - case AttributeNameEnum::NAME: + case AttributeName::NAME->value: break; - case AttributeNameEnum::TYPE: + case AttributeName::TYPE->value: /* * This is to support weird shit like this: * @@ -68,7 +68,7 @@ public static function rootDecorate(VersionConfig $config, Types $types, Type $t // if the "type" value is exactly equivalent to the "name" value, just assume // weirdness and move on. break; - } elseif (0 === strpos($v, 'xs:')) { + } elseif (str_starts_with($v, 'xs:')) { break; } $type->setParentTypeName($v); @@ -81,7 +81,7 @@ public static function rootDecorate(VersionConfig $config, Types $types, Type $t foreach ($element->children('xs', true) as $child) { switch ($child->getName()) { - case ElementNameEnum::ANNOTATION: + case ElementName::ANNOTATION->value: AnnotationElementTypeDecorator::decorate($config, $types, $type, $child); break; @@ -99,28 +99,28 @@ public static function rootDecorate(VersionConfig $config, Types $types, Type $t * @param \DCarbone\PHPFHIR\Definition\Type $type * @param \SimpleXMLElement $element */ - public static function decorate(VersionConfig $config, Types $types, Type $type, SimpleXMLElement $element) + public static function decorate(VersionConfig $config, Types $types, Type $type, SimpleXMLElement $element): void { $property = new Property($type, $element, $type->getSourceFilename()); // parse through attributes foreach ($element->attributes() as $attribute) { switch ($attribute->getName()) { - case AttributeNameEnum::REF: + case AttributeName::REF->value: $property->setRef((string)$attribute); break; - case AttributeNameEnum::NAME: + case AttributeName::NAME->value: $property->setName((string)$attribute); break; - case AttributeNameEnum::MIN_OCCURS: + case AttributeName::MIN_OCCURS->value: $property->setMinOccurs(intval((string)$attribute)); break; - case AttributeNameEnum::MAX_OCCURS: + case AttributeName::MAX_OCCURS->value: $property->setMaxOccurs((string)$attribute); break; - case AttributeNameEnum::TYPE: + case AttributeName::TYPE->value: $property->setValueFHIRTypeName((string)$attribute); break; @@ -132,10 +132,10 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, // parse through child elements foreach ($element->children('xs', true) as $child) { switch ($child->getName()) { - case ElementNameEnum::ANNOTATION: + case ElementName::ANNOTATION->value: AnnotationElementPropertyTypeDecorator::decorate($config, $types, $type, $property, $child); break; - case ElementNameEnum::COMPLEX_TYPE: + case ElementName::COMPLEX_TYPE->value: ComplexTypeElementTypeDecorator::decorate($config, $types, $type, $child); break; diff --git a/src/Definition/Decorator/ExtensionElementTypeDecorator.php b/src/Definition/Decorator/ExtensionElementTypeDecorator.php index 4daaa09a..62bdf5f9 100644 --- a/src/Definition/Decorator/ExtensionElementTypeDecorator.php +++ b/src/Definition/Decorator/ExtensionElementTypeDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,8 +21,8 @@ use DCarbone\PHPFHIR\Config\VersionConfig; use DCarbone\PHPFHIR\Definition\Type; use DCarbone\PHPFHIR\Definition\Types; -use DCarbone\PHPFHIR\Enum\AttributeNameEnum; -use DCarbone\PHPFHIR\Enum\ElementNameEnum; +use DCarbone\PHPFHIR\Enum\AttributeName; +use DCarbone\PHPFHIR\Enum\ElementName; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; use SimpleXMLElement; @@ -57,7 +57,7 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, { foreach ($extension->attributes() as $attribute) { switch ($attribute->getName()) { - case AttributeNameEnum::BASE: + case AttributeName::BASE->value: self::parseBaseAttribute($type, $attribute); break; @@ -68,10 +68,10 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, foreach ($extension->children('xs', true) as $child) { switch ($child->getName()) { - case ElementNameEnum::ATTRIBUTE: + case ElementName::ATTRIBUTE->value: AttributeElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::SEQUENCE: + case ElementName::SEQUENCE->value: SequenceElementTypeDecorator::decorate($config, $types, $type, $child); break; diff --git a/src/Definition/Decorator/RestrictionElementTypeDecorator.php b/src/Definition/Decorator/RestrictionElementTypeDecorator.php index 8bf5b65d..70068907 100644 --- a/src/Definition/Decorator/RestrictionElementTypeDecorator.php +++ b/src/Definition/Decorator/RestrictionElementTypeDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,8 +21,8 @@ use DCarbone\PHPFHIR\Config\VersionConfig; use DCarbone\PHPFHIR\Definition\Type; use DCarbone\PHPFHIR\Definition\Types; -use DCarbone\PHPFHIR\Enum\AttributeNameEnum; -use DCarbone\PHPFHIR\Enum\ElementNameEnum; +use DCarbone\PHPFHIR\Enum\AttributeName; +use DCarbone\PHPFHIR\Enum\ElementName; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; use DCarbone\PHPFHIR\Utilities\TypeBuilderUtils; use SimpleXMLElement; @@ -43,7 +43,7 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, { foreach ($restriction->attributes() as $attribute) { switch ($attribute->getName()) { - case AttributeNameEnum::BASE: + case AttributeName::BASE->value: $type->setRestrictionBaseFHIRName((string)$attribute); break; @@ -54,23 +54,23 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, foreach ($restriction->children('xs', true) as $child) { switch ($child->getName()) { - case ElementNameEnum::SIMPLE_TYPE: + case ElementName::SIMPLE_TYPE->value: SimpleTypeElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::PATTERN: + case ElementName::PATTERN->value: TypeBuilderUtils::setTypeStringFromElementAttribute($type, $child, 'setPattern'); break; - case ElementNameEnum::MIN_LENGTH: - case ElementNameEnum::MAX_LENGTH: + case ElementName::MIN_LENGTH->value: + case ElementName::MAX_LENGTH->value: TypeBuilderUtils::setTypeIntegerFromElementAttribute($type, $child, 'set' . $child->getName()); break; - case ElementNameEnum::ENUMERATION: + case ElementName::ENUMERATION->value: TypeBuilderUtils::addTypeEnumeratedValue($type, $restriction, $child); break; - case ElementNameEnum::SEQUENCE: + case ElementName::SEQUENCE->value: SequenceElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::ATTRIBUTE: + case ElementName::ATTRIBUTE->value: AttributeElementTypeDecorator::decorate($config, $types, $type, $child); break; diff --git a/src/Definition/Decorator/SequenceElementTypeDecorator.php b/src/Definition/Decorator/SequenceElementTypeDecorator.php index 1c6bfa95..4b76588f 100644 --- a/src/Definition/Decorator/SequenceElementTypeDecorator.php +++ b/src/Definition/Decorator/SequenceElementTypeDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,7 @@ use DCarbone\PHPFHIR\Config\VersionConfig; use DCarbone\PHPFHIR\Definition\Type; use DCarbone\PHPFHIR\Definition\Types; -use DCarbone\PHPFHIR\Enum\ElementNameEnum; +use DCarbone\PHPFHIR\Enum\ElementName; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; use SimpleXMLElement; @@ -50,16 +50,16 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, // parse through child elements foreach ($sequence->children('xs', true) as $child) { switch ($child->getName()) { - case ElementNameEnum::ANNOTATION: + case ElementName::ANNOTATION->value: AnnotationElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::ELEMENT: + case ElementName::ELEMENT->value: ElementElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::CHOICE: + case ElementName::CHOICE->value: ChoiceElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::ANY: + case ElementName::ANY->value: AnyElementTypeDecorator::decorate($config, $types, $type, $child); break; diff --git a/src/Definition/Decorator/SimpleContentElementTypeDecorator.php b/src/Definition/Decorator/SimpleContentElementTypeDecorator.php index 994cf7e3..e368faed 100644 --- a/src/Definition/Decorator/SimpleContentElementTypeDecorator.php +++ b/src/Definition/Decorator/SimpleContentElementTypeDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,7 @@ use DCarbone\PHPFHIR\Config\VersionConfig; use DCarbone\PHPFHIR\Definition\Type; use DCarbone\PHPFHIR\Definition\Types; -use DCarbone\PHPFHIR\Enum\ElementNameEnum; +use DCarbone\PHPFHIR\Enum\ElementName; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; use SimpleXMLElement; @@ -48,7 +48,7 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, foreach ($simpleContent->children('xs', true) as $child) { switch ($child->getName()) { - case ElementNameEnum::EXTENSION: + case ElementName::EXTENSION->value: ExtensionElementTypeDecorator::decorate($config, $types, $type, $child); break; diff --git a/src/Definition/Decorator/SimpleTypeElementPropertyDecorator.php b/src/Definition/Decorator/SimpleTypeElementPropertyDecorator.php index 873a71b5..559aa447 100644 --- a/src/Definition/Decorator/SimpleTypeElementPropertyDecorator.php +++ b/src/Definition/Decorator/SimpleTypeElementPropertyDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/Definition/Decorator/SimpleTypeElementTypeDecorator.php b/src/Definition/Decorator/SimpleTypeElementTypeDecorator.php index 8683cff5..b2241189 100644 --- a/src/Definition/Decorator/SimpleTypeElementTypeDecorator.php +++ b/src/Definition/Decorator/SimpleTypeElementTypeDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,8 +21,8 @@ use DCarbone\PHPFHIR\Config\VersionConfig; use DCarbone\PHPFHIR\Definition\Type; use DCarbone\PHPFHIR\Definition\Types; -use DCarbone\PHPFHIR\Enum\AttributeNameEnum; -use DCarbone\PHPFHIR\Enum\ElementNameEnum; +use DCarbone\PHPFHIR\Enum\AttributeName; +use DCarbone\PHPFHIR\Enum\ElementName; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; use SimpleXMLElement; @@ -42,7 +42,7 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, { foreach ($simpleType->attributes() as $attribute) { switch ($attribute->getName()) { - case AttributeNameEnum::NAME: + case AttributeName::NAME->value: continue 2; default: @@ -52,10 +52,10 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, foreach ($simpleType->children('xs', true) as $child) { switch ($child->getName()) { - case ElementNameEnum::RESTRICTION: + case ElementName::RESTRICTION->value: RestrictionElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::UNION: + case ElementName::UNION->value: UnionElementTypeDecorator::decorate($config, $types, $type, $child); break; diff --git a/src/Definition/Decorator/UnionElementTypeDecorator.php b/src/Definition/Decorator/UnionElementTypeDecorator.php index 593900b8..b9bcc54a 100644 --- a/src/Definition/Decorator/UnionElementTypeDecorator.php +++ b/src/Definition/Decorator/UnionElementTypeDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition\Decorator; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,8 +21,8 @@ use DCarbone\PHPFHIR\Config\VersionConfig; use DCarbone\PHPFHIR\Definition\Type; use DCarbone\PHPFHIR\Definition\Types; -use DCarbone\PHPFHIR\Enum\AttributeNameEnum; -use DCarbone\PHPFHIR\Enum\ElementNameEnum; +use DCarbone\PHPFHIR\Enum\AttributeName; +use DCarbone\PHPFHIR\Enum\ElementName; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; use DCarbone\PHPFHIR\Utilities\TypeBuilderUtils; use SimpleXMLElement; @@ -43,7 +43,7 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, { foreach ($union->attributes() as $attribute) { switch ($attribute->getName()) { - case AttributeNameEnum::MEMBER_TYPES: + case AttributeName::MEMBER_TYPES->value: TypeBuilderUtils::setTypeArrayFromAttribute($type, $union, $attribute, 'setUnionOf'); break; @@ -54,7 +54,7 @@ public static function decorate(VersionConfig $config, Types $types, Type $type, foreach ($union->children('xs', true) as $child) { switch ($child->getName()) { - case ElementNameEnum::SIMPLE_TYPE: + case ElementName::SIMPLE_TYPE->value: SimpleTypeElementTypeDecorator::decorate($config, $types, $type, $child); break; diff --git a/src/Definition/DocumentationTrait.php b/src/Definition/DocumentationTrait.php index 8bed02ac..bdeedc94 100644 --- a/src/Definition/DocumentationTrait.php +++ b/src/Definition/DocumentationTrait.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,14 +24,14 @@ */ trait DocumentationTrait { - /** @var null|array */ + /** @var array */ private array $documentation = []; /** - * @param string|array $documentation + * @param null|string|array $documentation * @return \DCarbone\PHPFHIR\Definition\Type|\DCarbone\PHPFHIR\Definition\Property */ - public function addDocumentationFragment($documentation): object + public function addDocumentationFragment(null|string|array $documentation): Type|Property { if (null === $documentation) { return $this; @@ -95,7 +95,7 @@ public function getDocBlockDocumentationFragment(int $spaces, bool $trailingNewl } $pieces = []; $spaces = str_repeat(' ', $spaces); - foreach ($this->documentation as $i => $doc) { + foreach ($this->documentation as $doc) { $pieces[] = str_replace('@', '\\@', "{$spaces}* {$doc}"); } return implode("\n", $pieces) . ($trailingNewline ? "\n" : ''); diff --git a/src/Definition/Enumeration.php b/src/Definition/Enumeration.php index fe8909a4..aa572dbd 100644 --- a/src/Definition/Enumeration.php +++ b/src/Definition/Enumeration.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,7 +47,7 @@ public function addValue(EnumerationValue $value): Enumeration * @param mixed $rawValue * @return bool */ - public function hasRawValue($rawValue): bool + public function hasRawValue(mixed $rawValue): bool { foreach ($this->values as $value) { if ($value->getValue() === $rawValue) { @@ -69,7 +69,7 @@ public function hasValue(EnumerationValue $value): bool /** * @return \DCarbone\PHPFHIR\Definition\EnumerationValue|false */ - public function current() + public function current(): bool|EnumerationValue { return current($this->values); } diff --git a/src/Definition/EnumerationValue.php b/src/Definition/EnumerationValue.php index b013f85a..89eff6b5 100644 --- a/src/Definition/EnumerationValue.php +++ b/src/Definition/EnumerationValue.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,8 +28,8 @@ class EnumerationValue { use DocumentationTrait; - /** @var mixed */ - private $value; + /** @var mixed mixed */ + private mixed $value; /** @var \SimpleXMLElement */ private \SimpleXMLElement $sourceSXE; @@ -47,7 +47,7 @@ public function __construct(string $value, SimpleXMLElement $sourceSXE) /** * @return mixed */ - public function getValue() + public function getValue(): mixed { return $this->value; } diff --git a/src/Definition/Properties.php b/src/Definition/Properties.php index a73ab92f..6820ef83 100644 --- a/src/Definition/Properties.php +++ b/src/Definition/Properties.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,7 +35,7 @@ class Properties implements Countable private array $_sortedProperties; /** @var \DCarbone\PHPFHIR\Definition\Property[] */ - private array $_directProperties; + private array $_localProperties; /** @var \DCarbone\PHPFHIR\Definition\Property[] */ private array $_directSortedProperties; @@ -156,35 +156,43 @@ public function hasProperty(string $name): bool } /** + * Returns an iterator containing all properties, including those inherited from parent types + * * @return \DCarbone\PHPFHIR\Definition\Property[] */ - public function getIterator(): iterable + public function allPropertiesIterator(): iterable { return SplFixedArray::fromArray($this->properties, false); } /** + * Returns an iterator contanining all properties, including those inherited from parent types, sorted ascending by name + * * @return \DCarbone\PHPFHIR\Definition\Property[] */ - public function getSortedIterator(): iterable + public function allSortedPropertiesIterator(): iterable { $this->_buildLocalCaches(); return SplFixedArray::fromArray($this->_sortedProperties, false); } /** + * Returns an iterator containing only properties local to this type + * * @return \DCarbone\PHPFHIR\Definition\Property[] */ - public function getDirectIterator(): iterable + public function localPropertiesIterator(): iterable { $this->_buildLocalCaches(); - return SplFixedArray::fromArray($this->_directProperties, false); + return SplFixedArray::fromArray($this->_localProperties, false); } /** + * Returns an iterator containing only properties local to this type, sorted ascending by name + * * @return \DCarbone\PHPFHIR\Definition\Property[] */ - public function getDirectSortedIterator(): iterable + public function localSortedPropertiesIterator(): iterable { $this->_buildLocalCaches(); return SplFixedArray::fromArray($this->_directSortedProperties, false); @@ -202,7 +210,7 @@ private function _buildLocalCaches(): void { if (!$this->cacheBuilt) { $this->_sortedProperties = $this->properties; - $this->_directProperties = []; + $this->_localProperties = []; $this->_directSortedProperties = []; usort( $this->_sortedProperties, @@ -212,7 +220,7 @@ function (Property $a, Property $b) { ); foreach ($this->properties as $property) { if (!$property->isOverloaded()) { - $this->_directProperties[] = $property; + $this->_localProperties[] = $property; } } foreach ($this->_sortedProperties as $property) { diff --git a/src/Definition/Property.php b/src/Definition/Property.php index 6599289f..866d1332 100644 --- a/src/Definition/Property.php +++ b/src/Definition/Property.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,9 +18,9 @@ * limitations under the License. */ -use DCarbone\PHPFHIR\Enum\PrimitiveTypeEnum; -use DCarbone\PHPFHIR\Enum\PropertyUseEnum; -use DCarbone\PHPFHIR\Enum\TypeKindEnum; +use DCarbone\PHPFHIR\Enum\PrimitiveType; +use DCarbone\PHPFHIR\Enum\PropertyUse; +use DCarbone\PHPFHIR\Enum\TypeKind; use DCarbone\PHPFHIR\Utilities\NameUtils; use InvalidArgumentException; use SimpleXMLElement; @@ -57,8 +57,8 @@ class Property /** @var null|\DCarbone\PHPFHIR\Definition\Type */ private ?Type $valueFHIRType = null; - /** @var \DCarbone\PHPFHIR\Enum\PropertyUseEnum */ - private PropertyUseEnum $use; + /** @var \DCarbone\PHPFHIR\Enum\PropertyUse */ + private PropertyUse $use; /** @var string|null */ private ?string $ref = null; @@ -83,7 +83,7 @@ public function __construct(Type $memberOf, SimpleXMLElement $sxe, string $sourc $this->memberOf = $memberOf; $this->sourceSXE = $sxe; $this->sourceFilename = $sourceFilename; - $this->use = new PropertyUseEnum(PropertyUseEnum::OPTIONAL); + $this->use = PropertyUse::OPTIONAL; } /** @@ -101,7 +101,7 @@ public function __debugInfo() 'pattern' => $this->getPattern(), 'rawPHPValue' => $this->getRawPHPValue(), 'fhirType' => (string)$this->getValueFHIRType(), - 'use' => (string)$this->getUse(), + 'use' => $this->getUse()->value, 'fixed' => (string)$this->getFixed(), 'namespace' => (string)$this->getNamespace(), ]; @@ -208,7 +208,7 @@ public function getMaxOccurs(): int * @param int|string $maxOccurs * @return \DCarbone\PHPFHIR\Definition\Property */ - public function setMaxOccurs($maxOccurs): Property + public function setMaxOccurs(int|string $maxOccurs): Property { if (is_string($maxOccurs) && 'unbounded' === strtolower($maxOccurs)) { $this->maxOccurs = PHPFHIR_UNLIMITED; @@ -282,18 +282,18 @@ public function unlimitedOccurrences(): bool } /** - * @return \DCarbone\PHPFHIR\Enum\PropertyUseEnum + * @return \DCarbone\PHPFHIR\Enum\PropertyUse */ - public function getUse(): PropertyUseEnum + public function getUse(): PropertyUse { return $this->use; } /** - * @param \DCarbone\PHPFHIR\Enum\PropertyUseEnum $use + * @param \DCarbone\PHPFHIR\Enum\PropertyUse $use * @return \DCarbone\PHPFHIR\Definition\Property */ - public function setUse(PropertyUseEnum $use): Property + public function setUse(PropertyUse $use): Property { $this->use = $use; return $this; diff --git a/src/Definition/SourceTrait.php b/src/Definition/SourceTrait.php index 8b5a1511..da2ef574 100644 --- a/src/Definition/SourceTrait.php +++ b/src/Definition/SourceTrait.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/Definition/Type.php b/src/Definition/Type.php index 4cc22c3f..6cb15b30 100644 --- a/src/Definition/Type.php +++ b/src/Definition/Type.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +19,8 @@ */ use DCarbone\PHPFHIR\Config\VersionConfig; -use DCarbone\PHPFHIR\Enum\PrimitiveTypeEnum; -use DCarbone\PHPFHIR\Enum\TypeKindEnum; +use DCarbone\PHPFHIR\Enum\PrimitiveType; +use DCarbone\PHPFHIR\Enum\TypeKind; use DCarbone\PHPFHIR\Utilities\NameUtils; use DomainException; use LogicException; @@ -44,8 +44,8 @@ class Type */ private string $fhirName; - /** @var \DCarbone\PHPFHIR\Enum\TypeKindEnum|null */ - private ?TypeKindEnum $kind = null; + /** @var \DCarbone\PHPFHIR\Enum\TypeKind|null */ + private ?TypeKind $kind = null; /** @var string */ private string $className; @@ -74,8 +74,8 @@ class Type /** @var array */ private array $unionOf = []; - /** @var \DCarbone\PHPFHIR\Enum\PrimitiveTypeEnum */ - private PrimitiveTypeEnum $primitiveType; + /** @var \DCarbone\PHPFHIR\Enum\PrimitiveType */ + private PrimitiveType $primitiveType; /** @var null|string */ private ?string $restrictionBaseFHIRName = null; @@ -186,26 +186,26 @@ public function getClassNameConst(bool $withClass): string } /** - * @return \DCarbone\PHPFHIR\Enum\TypeKindEnum|null + * @return \DCarbone\PHPFHIR\Enum\TypeKind|null */ - public function getKind(): ?TypeKindEnum + public function getKind(): ?TypeKind { return $this->kind; } /** - * @param \DCarbone\PHPFHIR\Enum\TypeKindEnum $kind + * @param \DCarbone\PHPFHIR\Enum\TypeKind $kind * @return \DCarbone\PHPFHIR\Definition\Type */ - public function setKind(TypeKindEnum $kind): Type + public function setKind(TypeKind $kind): Type { - if (isset($this->kind) && !$this->kind->equals($kind)) { + if (isset($this->kind) && $this->kind !== $kind) { throw new LogicException( sprintf( 'Cannot overwrite Type % s Kind from %s to %s', $this->getFHIRName(), - $this->kind, - $kind + $this->kind->value, + $kind->value ) ); } @@ -214,18 +214,18 @@ public function setKind(TypeKindEnum $kind): Type } /** - * @param \DCarbone\PHPFHIR\Enum\PrimitiveTypeEnum $primitiveType + * @param \DCarbone\PHPFHIR\Enum\PrimitiveType $primitiveType * @return \DCarbone\PHPFHIR\Definition\Type */ - public function setPrimitiveType(PrimitiveTypeEnum $primitiveType): Type + public function setPrimitiveType(PrimitiveType $primitiveType): Type { - if (isset($this->primitiveType) && $this->primitiveType->equals($primitiveType)) { + if (isset($this->primitiveType) && $this->primitiveType === $primitiveType) { throw new LogicException( sprintf( 'Cannot overwrite Type "%s" PrimitiveType from "%s" to "%s"', $this->getFHIRName(), - $this->primitiveType, - $primitiveType + $this->primitiveType->value, + $primitiveType->value ) ); } @@ -344,7 +344,7 @@ public function getProperties(): Properties public function getAllPropertiesIterator(): iterable { $properties = []; - foreach($this->getProperties()->getDirectIterator() as $property) { + foreach($this->getProperties()->localPropertiesIterator() as $property) { $properties[$property->getName()] = $property; } foreach($this->getParentTypes() as $parentType) { @@ -442,7 +442,7 @@ public function hasParent(): bool public function hasResourceParent(): bool { foreach ($this->getParentTypes() as $parentType) { - if ($parentType->getKind()->isResource()) { + if ($parentType->getKind() === TypeKind::RESOURCE) { return true; } } @@ -455,7 +455,7 @@ public function hasResourceParent(): bool public function hasPrimitiveParent(): bool { foreach ($this->getParentTypes() as $parentType) { - if ($parentType->getKind()->isPrimitive()) { + if ($parentType->getKind() === TypeKind::PRIMITIVE) { return true; } } @@ -468,7 +468,7 @@ public function hasPrimitiveParent(): bool public function hasPrimitiveContainerParent(): bool { foreach ($this->getParentTypes() as $parentType) { - if ($parentType->getKind()->isPrimitiveContainer()) { + if ($parentType->getKind() === TypeKind::PRIMITIVE_CONTAINER) { return true; } } @@ -482,7 +482,7 @@ public function hasPrimitiveContainerParent(): bool */ public function isDomainResource(): bool { - return false !== strpos($this->getFullyQualifiedNamespace(false), 'DomainResource'); + return str_contains($this->getFullyQualifiedNamespace(false), 'DomainResource'); } /** @@ -584,9 +584,9 @@ public function isEnumerated(): bool } /** - * @return \DCarbone\PHPFHIR\Enum\PrimitiveTypeEnum + * @return \DCarbone\PHPFHIR\Enum\PrimitiveType */ - public function getPrimitiveType(): PrimitiveTypeEnum + public function getPrimitiveType(): PrimitiveType { return $this->primitiveType; } @@ -749,6 +749,7 @@ public function getDirectlyUsedTraits(): array } $traits[] = PHPFHIR_TRAIT_VALIDATION_ASSERTIONS; $traits[] = PHPFHIR_TRAIT_CHANGE_TRACKING; + $traits[] = PHPFHIR_TRAIT_XMLNS; } return $traits; diff --git a/src/Definition/TypeDecorationValidator.php b/src/Definition/TypeDecorationValidator.php index c8ab17b7..8081dd3d 100644 --- a/src/Definition/TypeDecorationValidator.php +++ b/src/Definition/TypeDecorationValidator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ */ use DCarbone\PHPFHIR\Config\VersionConfig; +use DCarbone\PHPFHIR\Enum\TypeKind; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; use DCarbone\PHPFHIR\Utilities\NameUtils; @@ -60,14 +61,13 @@ public static function validateDecoration(VersionConfig $config, Types $types): throw ExceptionUtils::createUnknownTypeKindException($type); } - if ($typeKind->isPrimitive()) { - $primitiveType = $type->getPrimitiveType(); - if (null === $primitiveType) { + if ($typeKind === TypeKind::PRIMITIVE) { + if (null === $type->getPrimitiveType()) { throw ExceptionUtils::createUnknownPrimitiveTypeException($type); } } - if ($typeKind->isList()) { + if ($typeKind === TypeKind::LIST) { $rbType = $type->getRestrictionBaseFHIRType(); if (null === $rbType) { throw ExceptionUtils::createUndefinedListRestrictionBaseException($type); @@ -78,7 +78,7 @@ public static function validateDecoration(VersionConfig $config, Types $types): throw ExceptionUtils::createContainedTypeFlagMismatchException($types->isContainedType($type), $type); } - foreach ($type->getProperties()->getIterator() as $property) { + foreach ($type->getProperties()->allPropertiesIterator() as $property) { $name = $property->getName(); if (null === $name || '' === $name) { throw ExceptionUtils::createPropertyMissingNameException($type, $property); diff --git a/src/Definition/TypeDecorator.php b/src/Definition/TypeDecorator.php index cf344566..66228985 100644 --- a/src/Definition/TypeDecorator.php +++ b/src/Definition/TypeDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +19,8 @@ */ use DCarbone\PHPFHIR\Config\VersionConfig; -use DCarbone\PHPFHIR\Enum\PrimitiveTypeEnum; -use DCarbone\PHPFHIR\Enum\TypeKindEnum; +use DCarbone\PHPFHIR\Enum\PrimitiveType; +use DCarbone\PHPFHIR\Enum\TypeKind; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; /** @@ -40,7 +40,7 @@ public static function findComponentOfTypes(VersionConfig $config, Types $types) { foreach ($types->getIterator() as $type) { $fhirName = $type->getFHIRName(); - if (false === strpos($fhirName, '.')) { + if (!str_contains($fhirName, '.')) { continue; } $split = explode('.', $fhirName, 2); @@ -70,7 +70,7 @@ public static function findRestrictionBaseTypes(VersionConfig $config, Types $ty $fhirName = $type->getFHIRName(); // skip primitive types as they are already as base as they can go - if (false !== strpos($fhirName, PHPFHIR_PRIMITIVE_SUFFIX)) { + if (str_contains($fhirName, PHPFHIR_PRIMITIVE_SUFFIX)) { continue; } @@ -80,7 +80,7 @@ public static function findRestrictionBaseTypes(VersionConfig $config, Types $ty continue; } - if (0 === strpos($rbName, 'xs:')) { + if (str_starts_with($rbName, 'xs:')) { $rbName = substr($rbName, 3); if ('token' === $rbName || ctype_upper($rbName[0])) { $logger->warning( @@ -92,7 +92,7 @@ public static function findRestrictionBaseTypes(VersionConfig $config, Types $ty ); $rbName = 'string'; } - $rbName = "{$rbName}-primitive"; + $rbName = sprintf('%s-primitive', $rbName); } $rbType = $types->getTypeByName($rbName); @@ -142,7 +142,7 @@ public static function findParentTypes(VersionConfig $config, Types $types): voi } // skip "base" types 'cuz php. - if (0 === strpos($parentTypeName, 'xs:')) { + if (str_starts_with($parentTypeName, 'xs:')) { $logger->warning( sprintf( 'Type "%s" has un-resolvable parent "%s"', @@ -177,12 +177,12 @@ public static function determinePrimitiveTypes(VersionConfig $config, Types $typ $logger = $config->getLogger(); foreach ($types->getIterator() as $type) { if (in_array($type->getFHIRName(), self::DSTU1_PRIMITIVES, true)) { - $ptn = PrimitiveTypeEnum::STRING; + $ptn = PrimitiveType::STRING->value; $logger->debug(sprintf('(DSTU1 suppport) Type "%s" determined to be DSTU1 primitive', $type->getFHIRName())); } elseif ($type->hasPrimitiveParent()) { $ptn = $type->getParentType()->getFHIRName(); $logger->debug(sprintf('Type "%s" determined to have a primitive parent', $type->getFHIRName())); - } elseif ($type->getKind()->isPrimitive()) { + } elseif ($type->getKind() === TypeKind::PRIMITIVE) { $ptn = $type->getFHIRName(); $logger->debug(sprintf('Type "%s" determined to be a primitive itself', $type->getFHIRName())); } else { @@ -192,13 +192,13 @@ public static function determinePrimitiveTypes(VersionConfig $config, Types $typ $logger->debug(sprintf('Setting assumed primitive Type "%s" kind to "%s"', $type->getFHIRName(), $ptn)); $ptn = str_replace('-primitive', '', $ptn); - $pt = new PrimitiveTypeEnum($ptn); + $pt = PrimitiveType::from($ptn); $type->setPrimitiveType($pt); $logger->info( sprintf( 'Type "%s" is a Primitive of type "%s"', $type, - $pt + $pt->value ) ); } @@ -234,17 +234,19 @@ public static function ensureValueOnPrimitiveChildTypes(VersionConfig $config, T * @param \DCarbone\PHPFHIR\Config\VersionConfig $config * @param \DCarbone\PHPFHIR\Definition\Types $types * @param \DCarbone\PHPFHIR\Definition\Type $type - * @param string $kindName + * @param TypeKind|string $kind */ - private static function setTypeKind(VersionConfig $config, Types $types, Type $type, string $kindName): void + private static function setTypeKind(VersionConfig $config, Types $types, Type $type, TypeKind|string $kind): void { - $kind = new TypeKindEnum($kindName); + if (is_string($kind)) { + $kind = TypeKind::from($kind); + } $type->setKind($kind); $config->getLogger()->info( sprintf( 'Setting Type "%s" to Kind "%s"', $type->getFHIRName(), - $type->getKind() + $type->getKind()->value ) ); } @@ -267,14 +269,14 @@ private static function determineParsedTypeKind(VersionConfig $config, Types $ty sprintf( 'Type "%s" already has Kind "%s", will not set again', $fhirName, - $type->getKind() + $type->getKind()->value ) ); return; } // check if this is a known root and determine kind immediately - if (TypeKindEnum::isKnownRoot($fhirName)) { + if (TypeKind::isKnownRoot($fhirName)) { $logger->debug(sprintf('Type "%s" is a known root, setting kind to "%s"', $type->getFHIRName(), $fhirName)); self::setTypeKind($config, $types, $type, $fhirName); return; @@ -290,39 +292,39 @@ private static function determineParsedTypeKind(VersionConfig $config, Types $ty // check if this type is a DSTU1-specific primitive if (in_array($fhirName, self::DSTU1_PRIMITIVES, true)) { - $logger->debug(sprintf('Setting Type "%s" kind to "%s"', $type->getFHIRName(), TypeKindEnum::PRIMITIVE)); - $type->setKind(new TypeKindEnum(TypeKindEnum::PRIMITIVE)); + $logger->debug(sprintf('Setting Type "%s" kind to "%s"', $type->getFHIRName(), TypeKind::PRIMITIVE->value)); + $type->setKind(TypeKind::PRIMITIVE); return; } // check if type is primitive... - if (false !== strpos($fhirName, PHPFHIR_PRIMITIVE_SUFFIX)) { - $logger->debug(sprintf('Type "%s" has primitive suffix, setting kind to "%s"', $type->getFHIRName(), TypeKindEnum::PRIMITIVE)); - self::setTypeKind($config, $types, $type, TypeKindEnum::PRIMITIVE); + if (str_contains($fhirName, PHPFHIR_PRIMITIVE_SUFFIX)) { + $logger->debug(sprintf('Type "%s" has primitive suffix, setting kind to "%s"', $type->getFHIRName(), TypeKind::PRIMITIVE->value)); + self::setTypeKind($config, $types, $type, TypeKind::PRIMITIVE); return; } // check if type is list... - if (false !== strpos($fhirName, PHPFHIR_LIST_SUFFIX)) { + if (str_contains($fhirName, PHPFHIR_LIST_SUFFIX)) { // for all intents and purposes, a List type is a multiple choice primitive type - $logger->debug(sprintf('Type "%s" has list suffix, setting kind to "%s"', $type->getFHIRName(), TypeKindEnum::_LIST)); - self::setTypeKind($config, $types, $type, TypeKindEnum::_LIST); + $logger->debug(sprintf('Type "%s" has list suffix, setting kind to "%s"', $type->getFHIRName(), TypeKind::LIST->value)); + self::setTypeKind($config, $types, $type, TypeKind::LIST); return; } // This block indicates the type is only present as the child of a Resource. Its name may (and in many // cases does) conflict with a top level Element or Resource. Because of this, they are treated differently // and must be marked as such. - if (false !== strpos($fhirName, '.') && TypeKindEnum::RESOURCE_INLINE !== $fhirName) { - $logger->debug(sprintf('Type "%s" is not "%s" but has dot in name, setting kind to "%s"', $type->getFHIRName(), TypeKindEnum::RESOURCE_INLINE, TypeKindEnum::RESOURCE_COMPONENT)); - self::setTypeKind($config, $types, $type, TypeKindEnum::RESOURCE_COMPONENT); + if (str_contains($fhirName, '.') && TypeKind::RESOURCE_INLINE->value !== $fhirName) { + $logger->debug(sprintf('Type "%s" is not "%s" but has dot in name, setting kind to "%s"', $type->getFHIRName(), TypeKind::RESOURCE_INLINE->value, TypeKind::RESOURCE_COMPONENT->value)); + self::setTypeKind($config, $types, $type, TypeKind::RESOURCE_COMPONENT); return; } // this is for primitive "wrapper" types, e.g. String -> 'string-primitive' - if (null !== $types->getTypeByName("{$fhirName}-primitive")) { - $logger->debug(sprintf('Type "%s" has primitive counterpart, setting kind to "%s"', $type->getFHIRName(), TypeKindEnum::PRIMITIVE_CONTAINER)); - self::setTypeKind($config, $types, $type, TypeKindEnum::PRIMITIVE_CONTAINER); + if (null !== $types->getTypeByName(sprintf('%s-primitive', $fhirName))) { + $logger->debug(sprintf('Type "%s" has primitive counterpart, setting kind to "%s"', $type->getFHIRName(), TypeKind::PRIMITIVE_CONTAINER->value)); + self::setTypeKind($config, $types, $type, TypeKind::PRIMITIVE_CONTAINER); return; } @@ -334,28 +336,28 @@ private static function determineParsedTypeKind(VersionConfig $config, Types $ty // this final block is necessary as in DSTU1 all Resources extend Elements, so we cannot just use the upper- // most parent to determine type as then they would all just be elements. $set = false; - if ($rootTypeKind->isElement() && [] !== ($parentTypes = $type->getParentTypes())) { + if ($rootTypeKind === TypeKind::ELEMENT && [] !== ($parentTypes = $type->getParentTypes())) { foreach ($parentTypes as $parentType) { if ('Resource' === $parentType->getFHIRName()) { $set = true; - $logger->debug(sprintf('(DSTU1 support) Setting Type "%s" kind to "%s"', $type->getFHIRName(), TypeKindEnum::RESOURCE)); - self::setTypeKind($config, $types, $type, TypeKindEnum::RESOURCE); + $logger->debug(sprintf('(DSTU1 support) Setting Type "%s" kind to "%s"', $type->getFHIRName(), TypeKind::RESOURCE->value)); + self::setTypeKind($config, $types, $type, TypeKind::RESOURCE); break; } } } if (!$set) { - $logger->debug(sprintf('Setting Type "%s" kind to root type kind "%s"', $type->getFHIRName(), $rootTypeKind)); - self::setTypeKind($config, $types, $type, (string)$rootTypeKind); + $logger->debug(sprintf('Setting Type "%s" kind to root type kind "%s"', $type->getFHIRName(), $rootTypeKind->value)); + self::setTypeKind($config, $types, $type, $rootTypeKind->value); } return; } // this is a catchall that may bomb if we encounter new stuff - $logger->debug(sprintf('Setting Type "%s" kind to itself ("%s")', $type->getFHIRName(), TypeKindEnum::PHPFHIR_XHTML)); - self::setTypeKind($config, $types, $type, TypeKindEnum::PHPFHIR_XHTML); + $logger->debug(sprintf('Setting Type "%s" kind to itself ("%s")', $type->getFHIRName(), TypeKind::PHPFHIR_XHTML->value)); + self::setTypeKind($config, $types, $type, TypeKind::PHPFHIR_XHTML); } /** @@ -392,16 +394,16 @@ public static function setContainedTypeFlag(VersionConfig $config, Types $types) public static function setValueContainerFlag(VersionConfig $config, Types $types): void { static $skip = [ - TypeKindEnum::PRIMITIVE, - TypeKindEnum::PHPFHIR_XHTML, - TypeKindEnum::QUANTITY, + TypeKind::PRIMITIVE, + TypeKind::PHPFHIR_XHTML, + TypeKind::QUANTITY, ]; foreach ($types->getIterator() as $type) { // TODO: handle valueString, valueQuantity, etc. types? // skip primitive types and their child types - if ($type->getKind()->isOneOf($skip) || $type->hasPrimitiveParent()) { + if ($type->getKind()->isOneOf(...$skip) || $type->hasPrimitiveParent()) { continue; } @@ -430,10 +432,10 @@ public static function setValueContainerFlag(VersionConfig $config, Types $types */ public static function setCommentContainerFlag(VersionConfig $config, Types $types): void { - static $skip = [TypeKindEnum::PRIMITIVE, TypeKindEnum::PHPFHIR_XHTML]; + static $skip = [TypeKind::PRIMITIVE, TypeKind::PHPFHIR_XHTML]; foreach ($types->getIterator() as $type) { $type->setCommentContainer( - !$type->hasPrimitiveParent() && !$type->getKind()->isOneOf($skip) + !$type->hasPrimitiveParent() && !$type->getKind()->isOneOf(...$skip) ); } } @@ -463,7 +465,7 @@ public static function parseUnionMemberTypes(VersionConfig $config, Types $types $utype->getFHIRName() ) ); - foreach ($utype->getProperties()->getIterator() as $property) { + foreach ($utype->getProperties()->allPropertiesIterator() as $property) { $type->getProperties()->addProperty(clone $property); } } else { diff --git a/src/Definition/TypeExtractor.php b/src/Definition/TypeExtractor.php index 370ea005..f38636d3 100644 --- a/src/Definition/TypeExtractor.php +++ b/src/Definition/TypeExtractor.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,7 @@ use DCarbone\PHPFHIR\Definition\Decorator\ComplexTypeElementTypeDecorator; use DCarbone\PHPFHIR\Definition\Decorator\ElementElementTypeDecorator; use DCarbone\PHPFHIR\Definition\Decorator\SimpleTypeElementTypeDecorator; -use DCarbone\PHPFHIR\Enum\ElementNameEnum; +use DCarbone\PHPFHIR\Enum\ElementName; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; use DomainException; use RuntimeException; @@ -105,7 +105,7 @@ protected static function extractTypesFromXSD(VersionConfig $config, Types $type $childName = $child->getName(); // skip these root level elements - if (ElementNameEnum::_INCLUDE === $childName || ElementNameEnum::IMPORT === $childName || ElementNameEnum::ANNOTATION === $childName) { + if (ElementName::_INCLUDE->value === $childName || ElementName::IMPORT->value === $childName || ElementName::ANNOTATION->value === $childName) { continue; } @@ -127,7 +127,7 @@ protected static function extractTypesFromXSD(VersionConfig $config, Types $type // parse top level elements switch ($childName) { - case ElementNameEnum::SIMPLE_TYPE: + case ElementName::SIMPLE_TYPE->value: $logger->debug(sprintf('Parsing "%s" from SimpleType', $fhirName)); // build type $type = TypeBuilder::build($config, $fhirName, $child, $sourceFile); @@ -139,7 +139,7 @@ protected static function extractTypesFromXSD(VersionConfig $config, Types $type SimpleTypeElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::COMPLEX_TYPE: + case ElementName::COMPLEX_TYPE->value: $logger->debug(sprintf('Parsing "%s" from ComplexType', $fhirName)); // build type $type = TypeBuilder::build($config, $fhirName, $child, $sourceFile); @@ -151,7 +151,7 @@ protected static function extractTypesFromXSD(VersionConfig $config, Types $type ComplexTypeElementTypeDecorator::decorate($config, $types, $type, $child); break; - case ElementNameEnum::ELEMENT: + case ElementName::ELEMENT->value: /* TODO: this is producing some oddities as the result of things like this: * src: R4 bundle.xsd * @@ -197,10 +197,10 @@ public static function parseTypes(VersionConfig $config): Types // first, parse all .xsd files without the "fhir-" prefix foreach (glob(sprintf('%s/*.xsd', $config->getSchemaPath()), GLOB_NOSORT) as $xsdFile) { $basename = basename($xsdFile); - if (0 === strpos($basename, PHPFHIR_SKIP_FHIR_XSD_PREFIX)) { + if (str_starts_with($basename, PHPFHIR_SKIP_FHIR_XSD_PREFIX)) { continue; } - if (0 === strpos($basename, PHPFHIR_SKIP_ATOM_XSD_PREFIX)) { + if (str_starts_with($basename, PHPFHIR_SKIP_ATOM_XSD_PREFIX)) { continue; } if (PHPFHIR_SKIP_XML_XSD === $basename || PHPFHIR_SKIP_XHTML_XSD === $basename || PHPFHIR_SKIP_TOMBSTONE_XSD === $basename) { @@ -221,7 +221,7 @@ public static function parseTypes(VersionConfig $config): Types $logger->debug(sprintf('Skipping file "%s"', $xsdFile)); continue; } - if (0 === strpos($basename, PHPFHIR_SKIP_ATOM_XSD_PREFIX)) { + if (str_starts_with($basename, PHPFHIR_SKIP_ATOM_XSD_PREFIX)) { continue; } static::extractTypesFromXSD($config, $types, $xsdFile); diff --git a/src/Definition/TypeImport.php b/src/Definition/TypeImport.php index e274b886..8ef08382 100644 --- a/src/Definition/TypeImport.php +++ b/src/Definition/TypeImport.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/Definition/TypeImports.php b/src/Definition/TypeImports.php index 35cb57a4..ed99343f 100644 --- a/src/Definition/TypeImports.php +++ b/src/Definition/TypeImports.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,7 @@ */ use Countable; -use DCarbone\PHPFHIR\Enum\TypeKindEnum; +use DCarbone\PHPFHIR\Enum\TypeKind; use Iterator; /** @@ -260,16 +260,20 @@ private function buildImports(): void if (null === $propertyType) { continue; } - if ($propertyType->getKind()->isOneOf( - [ - TypeKindEnum::RESOURCE_CONTAINER, - TypeKindEnum::RESOURCE_INLINE, - ] - ) && + + $ptk = $propertyType->getKind(); + + if ($ptk->isOneOf(TypeKind::RESOURCE_CONTAINER, TypeKind::RESOURCE_INLINE) && $typeNS !== $configNS) { $this->addImport(PHPFHIR_INTERFACE_CONTAINED_TYPE, $configNS); $this->addImport(PHPFHIR_CLASSNAME_TYPEMAP, $configNS); } else { + + if ($ptk === TypeKind::PRIMITIVE_CONTAINER) { + $primType = $propertyType->getProperties()->getProperty('value')->getValueFHIRType(); + $this->addImport($primType->getClassName(), $primType->getFullyQualifiedNamespace(false)); + } + $propertyTypeNS = $propertyType->getFullyQualifiedNamespace(false); $this->addImport($propertyType->getClassName(), $propertyTypeNS); } diff --git a/src/Definition/TypePropertyDecorator.php b/src/Definition/TypePropertyDecorator.php index 73da95a5..39bf7a22 100644 --- a/src/Definition/TypePropertyDecorator.php +++ b/src/Definition/TypePropertyDecorator.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ */ use DCarbone\PHPFHIR\Config\VersionConfig; +use DCarbone\PHPFHIR\Enum\TypeKind; use DCarbone\PHPFHIR\Utilities\ExceptionUtils; abstract class TypePropertyDecorator @@ -37,15 +38,15 @@ public static function findPropertyType(VersionConfig $config, Types $types, Typ // handle primitive and list value properties if ($property->isValueProperty()) { - if ($typeKind->isPrimitive()) { + if ($typeKind === TypeKind::PRIMITIVE) { $primitiveType = $type->getPrimitiveType(); - $log->debug(sprintf('Type "%s" is primitive of kind "%s", setting property "%s" raw PHP value type to "%s"', $type->getFHIRName(), $primitiveType, $property->getName(), $primitiveType->getPHPValueType())); - $property->setRawPHPValue($primitiveType->getPHPValueType()); + $log->debug(sprintf('Type "%s" is primitive of kind "%s", setting property "%s" raw PHP value type to "%s"', $type->getFHIRName(), $primitiveType->value, $property->getName(), $primitiveType->getPHPValueTypes())); + $property->setRawPHPValue($primitiveType->getPHPValueTypes()); return; } - if ($typeKind->isList()) { - $parentPHPValueType = $type->getParentType()->getPrimitiveType()->getPHPValueType(); + if ($typeKind === TypeKind::LIST) { + $parentPHPValueType = $type->getParentType()->getPrimitiveType()->getPHPValueTypes(); $log->debug(sprintf('Type "%s" is list, setting property "%s" raw PHP value type to "%s"', $type->getFHIRName(), $property->getName(), $parentPHPValueType)); $property->setRawPHPValue($parentPHPValueType); return; @@ -86,7 +87,7 @@ public static function findPropertyType(VersionConfig $config, Types $types, Typ return; } - if (0 === strpos($valueFHIRTypeName, 'xs:')) { + if (str_starts_with($valueFHIRTypeName, 'xs:')) { $pt = $types->getTypeByName(substr($valueFHIRTypeName, 3) . '-primitive'); } elseif (null !== ($refName = $property->getRef())) { $pt = $types->getTypeByName($refName); @@ -117,7 +118,7 @@ public static function findPropertyType(VersionConfig $config, Types $types, Typ public static function findPropertyTypes(VersionConfig $config, Types $types): void { foreach ($types->getIterator() as $type) { - foreach ($type->getProperties()->getIterator() as $property) { + foreach ($type->getProperties()->allPropertiesIterator() as $property) { self::findPropertyType($config, $types, $type, $property); } } @@ -136,9 +137,9 @@ public static function findOverloadedProperties(VersionConfig $config, Types $ty } $parent = $type->getParentType(); while (null !== $parent) { - foreach ($type->getProperties()->getIterator() as $property) { + foreach ($type->getProperties()->allPropertiesIterator() as $property) { $propertyName = $property->getName(); - foreach ($parent->getProperties()->getIterator() as $parentProperty) { + foreach ($parent->getProperties()->allPropertiesIterator() as $parentProperty) { if ($propertyName === $parentProperty->getName()) { $logger->debug( sprintf( @@ -166,13 +167,13 @@ public static function setMissingPropertyNames(VersionConfig $config, Types $typ { $log = $config->getLogger(); foreach ($types->getIterator() as $type) { - foreach ($type->getProperties()->getIterator() as $property) { + foreach ($type->getProperties()->allPropertiesIterator() as $property) { $propName = $property->getName(); if ('' === $propName || null === $propName) { $ref = $property->getRef(); if (null !== $ref && '' !== $ref) { $newName = $ref; - if (0 === strpos($ref, 'xhtml:')) { + if (str_starts_with($ref, 'xhtml:')) { $split = explode(':', $ref, 2); if (2 === count($split) && '' !== $split[1]) { $newName = $split[1]; diff --git a/src/Definition/Types.php b/src/Definition/Types.php index d1e7cf3a..64ed391c 100644 --- a/src/Definition/Types.php +++ b/src/Definition/Types.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Definition; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,7 @@ use ArrayIterator; use Countable; use DCarbone\PHPFHIR\Config\VersionConfig; -use DCarbone\PHPFHIR\Enum\TypeKindEnum; +use DCarbone\PHPFHIR\Enum\TypeKind; /** * Class Types @@ -41,6 +41,14 @@ class Types implements Countable */ private Type $containerType; + private const _CONTAINED_IGNORED_TYPES = [ + TypeKind::RESOURCE_INLINE, + TypeKind::RESOURCE_CONTAINER, + TypeKind::LIST, + TypeKind::PRIMITIVE, + TypeKind::PRIMITIVE_CONTAINER, + ]; + /** * FHIRTypes constructor. * @param \DCarbone\PHPFHIR\Config\VersionConfig $config @@ -49,7 +57,7 @@ public function __construct(VersionConfig $config) { $this->config = $config; $rt = new Type($config, PHPFHIR_XHTML_TYPE_NAME); - $rt->setKind(new TypeKindEnum(TypeKindEnum::PHPFHIR_XHTML)); + $rt->setKind(TypeKind::PHPFHIR_XHTML); $rt->addDocumentationFragment(PHPFHIR_XHTML_TYPE_DESCRIPTION); $this->addType($rt); } @@ -150,7 +158,7 @@ public function getIterator(): iterable * * @return \DCarbone\PHPFHIR\Definition\Type[] */ - public function getSortedIterator(): iterable + public function getNameSortedIterator(): iterable { $tmp = $this->types; usort( @@ -186,7 +194,7 @@ public function getContainerType(): ?Type { if (!isset($this->containerType)) { foreach ($this->types as $type) { - if ($type->getKind()->isOneOf([TypeKindEnum::RESOURCE_INLINE, TypeKindEnum::RESOURCE_CONTAINER])) { + if ($type->getKind()->isOneOf(TypeKind::RESOURCE_INLINE, TypeKind::RESOURCE_CONTAINER)) { $this->containerType = $type; } } @@ -200,23 +208,15 @@ public function getContainerType(): ?Type */ public function isContainedType(Type $type): bool { - static $ignoredTypes = [ - TypeKindEnum::RESOURCE_INLINE, - TypeKindEnum::RESOURCE_CONTAINER, - TypeKindEnum::_LIST, - TypeKindEnum::PRIMITIVE, - TypeKindEnum::PRIMITIVE_CONTAINER, - ]; - // only bother with actual Resource types. - if ($type->getKind()->isOneOf($ignoredTypes)) { + if ($type->getKind()->isOneOf(...self::_CONTAINED_IGNORED_TYPES)) { return false; } $container = $this->getContainerType(); if (null === $container) { return false; } - foreach ($container->getProperties()->getIterator() as $property) { + foreach ($container->getProperties()->allPropertiesIterator() as $property) { if (($ptype = $property->getValueFHIRType()) && $ptype->getFHIRName() === $type->getFHIRName()) { return true; } diff --git a/src/Enum/AttributeName.php b/src/Enum/AttributeName.php new file mode 100644 index 00000000..0cbbe34d --- /dev/null +++ b/src/Enum/AttributeName.php @@ -0,0 +1,57 @@ +value; + + case PrimitiveType::DECIMAL: + return 'double'; + + case PrimitiveType::POSITIVE_INTEGER: + case PrimitiveType::NEGATIVE_INTEGER: + return 'integer'; + + case PrimitiveType::DATE: + case PrimitiveType::DATETIME: + case PrimitiveType::TIME: + case PrimitiveType::INSTANT: + case PrimitiveType::CODE: + case PrimitiveType::OID: + case PrimitiveType::CANONICAL: + case PrimitiveType::URI: + case PrimitiveType::URL: + case PrimitiveType::ID: + case PrimitiveType::UUID: + case PrimitiveType::SAMPLE_DATA_TYPE: + case PrimitiveType::BASE_64_BINARY: // TODO: add content decoding? + case PrimitiveType::MARKDOWN: // TODO: markdown lib, maybe? + case PrimitiveType::UNSIGNED_INTEGER: // TODO: utilize big number lib, maybe? + return 'string'; + + default: + throw new \DomainException(sprintf('No PHP value type case for "%s"', $this->value)); + } + } + + /** + * @return string + */ + public function getPHPReturnValueTypeHint(): string + { + return match ($hint = $this->getPHPValueTypes()) { + 'boolean' => 'bool', + 'double' => 'float', + 'integer' => 'int', + default => $hint, + }; + } + + /** + * @return string[] + */ + public function getPHPReceiveValueTypeHints(): array + { + $hintTypes = [$this->getPHPReturnValueTypeHint()]; + + // make sure 'string' is second item list. + if (!in_array('string', $hintTypes, true)) { + array_unshift($hintTypes, 'string'); + } + + switch ($this) { + // Date types may always accept a \DateTimeInterface instance + case PrimitiveType::DATE: + case PrimitiveType::DATETIME: + case PrimitiveType::INSTANT: + case PrimitiveType::TIME: + $hintTypes[] = '\\DateTimeInterface'; + break; + + // floats may stem from integers + case PrimitiveType::DECIMAL: + $hintTypes[] = 'int'; + break; + + // integers may stem from floats + case PrimitiveType::INTEGER: + $hintTypes[] = 'float'; + break; + + // unsigned integers may stem from integers or floats + case PrimitiveType::UNSIGNED_INTEGER: + array_push($hintTypes, 'int', 'float'); + break; + } + + return $hintTypes; + } + + /** + * @param \DCarbone\PHPFHIR\Enum\PrimitiveType|string ...$other + * @return bool + */ + public function isOneOf(PrimitiveType|string ...$other): bool + { + $vals = self::values(); + foreach ($other as $name) { + if ($this === $name || in_array($name, $vals, true)) { + return true; + } + } + + return false; + } +} diff --git a/src/Enum/PrimitiveTypeEnum.php b/src/Enum/PrimitiveTypeEnum.php deleted file mode 100644 index 092339ae..00000000 --- a/src/Enum/PrimitiveTypeEnum.php +++ /dev/null @@ -1,112 +0,0 @@ -getValue()) { - case PrimitiveTypeEnum::STRING: - case PrimitiveTypeEnum::BOOLEAN: - case PrimitiveTypeEnum::INTEGER: - return $v; - - case PrimitiveTypeEnum::DECIMAL: - return 'double'; - - case PrimitiveTypeEnum::POSITIVE_INTEGER: - case PrimitiveTypeEnum::NEGATIVE_INTEGER: - return 'integer'; - - case PrimitiveTypeEnum::DATE: - case PrimitiveTypeEnum::DATETIME: - case PrimitiveTypeEnum::TIME: - case PrimitiveTypeEnum::INSTANT: - case PrimitiveTypeEnum::CODE: - case PrimitiveTypeEnum::OID: - case PrimitiveTypeEnum::CANONICAL: - case PrimitiveTypeEnum::URI: - case PrimitiveTypeEnum::URL: - case PrimitiveTypeEnum::ID: - case PrimitiveTypeEnum::UUID: - case PrimitiveTypeEnum::SAMPLE_DATA_TYPE: - case PrimitiveTypeEnum::BASE_64_BINARY: // TODO: add content decoding? - case PrimitiveTypeEnum::MARKDOWN: // TODO: markdown lib, maybe? - case PrimitiveTypeEnum::UNSIGNED_INTEGER: // TODO: utilize big number lib, maybe? - return 'string'; - - default: - throw ExceptionUtils::createUnknownPrimitiveTypeEnumException($this); - } - } - - /** - * @return string - */ - public function getPHPValueTypeHint(): string - { - $hint = $this->getPHPValueType(); - switch ($hint) { - case PrimitiveTypeEnum::BOOLEAN: - return 'bool'; - case 'double': - return 'float'; - case 'integer': - return 'int'; - default: - return $hint; - } - } -} diff --git a/src/Enum/AbstractEnum.php b/src/Enum/PropertyUse.php similarity index 58% rename from src/Enum/AbstractEnum.php rename to src/Enum/PropertyUse.php index 7b2eeb99..2adbbe07 100644 --- a/src/Enum/AbstractEnum.php +++ b/src/Enum/PropertyUse.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Enum; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,37 +18,31 @@ * limitations under the License. */ -use MyCLabs\Enum\Enum; - /** - * Class AbstractEnum + * Class PropertyUseEnum * @package DCarbone\PHPFHIR\Enum */ -abstract class AbstractEnum extends Enum +enum PropertyUse: string { - /** - * @param mixed $enumValue - * @return bool - */ - public function is($enumValue): bool - { - if (is_scalar($enumValue)) { - return $enumValue === $this->getValue(); - } - return $this->equals($enumValue); - } + use ValuesTrait; + + case PROHIBITED = 'prohibited'; + case OPTIONAL = 'optional'; + case REQUIRED = 'required'; /** - * @param array $enumValues + * @param \DCarbone\PHPFHIR\Enum\PropertyUse|string ...$other * @return bool */ - public function isOneOf(array $enumValues): bool + public function isOneOf(PropertyUse|string ...$other): bool { - foreach ($enumValues as $kind) { - if ($this->is($kind)) { + $vals = self::values(); + foreach ($other as $name) { + if ($this === $name || in_array($name, $vals, true)) { return true; } } + return false; } } \ No newline at end of file diff --git a/src/Enum/PropertyUseEnum.php b/src/Enum/PropertyUseEnum.php deleted file mode 100644 index 9c7ccf13..00000000 --- a/src/Enum/PropertyUseEnum.php +++ /dev/null @@ -1,30 +0,0 @@ -value; }, self::KNOWN_ROOTS); + return in_array($fhirName, $rootStrings, true); + } + + /** + * @param \DCarbone\PHPFHIR\Enum\TypeKind|string ...$other + * @return bool + */ + public function isOneOf(TypeKind|string ...$other): bool + { + $vals = self::values(); + foreach ($other as $name) { + if ($this === $name || in_array($name, $vals, true)) { + return true; + } + } + + return false; + } +} \ No newline at end of file diff --git a/src/Enum/TypeKindEnum.php b/src/Enum/TypeKindEnum.php deleted file mode 100644 index bd6e1e7b..00000000 --- a/src/Enum/TypeKindEnum.php +++ /dev/null @@ -1,141 +0,0 @@ -is(TypeKindEnum::PRIMITIVE); - } - - /** - * @return bool - */ - public function isPrimitiveContainer(): bool - { - return $this->is(TypeKindEnum::PRIMITIVE_CONTAINER); - } - - /** - * @return bool - */ - public function isList(): bool - { - return $this->is(self::_LIST); - } - - /** - * @return bool - */ - public function isElement(): bool - { - return $this->is(self::ELEMENT); - } - - /** - * @return bool - */ - public function isQuantity(): bool - { - return $this->is(self::QUANTITY); - } - - /** - * @return bool - */ - public function isResource(): bool - { - return $this->is(self::RESOURCE); - } - - /** - * @return bool - */ - public function isResourceContainer(): bool - { - return $this->is(self::RESOURCE_CONTAINER); - } - - /** - * @return bool - */ - public function isInlineResource(): bool - { - return $this->is(self::RESOURCE_INLINE); - } - - /** - * @return bool - */ - public function isPHPFHIRXHTML(): bool - { - return $this->is(self::PHPFHIR_XHTML); - } -} \ No newline at end of file diff --git a/template/serialization/json/list.php b/src/Enum/ValuesTrait.php similarity index 72% rename from template/serialization/json/list.php rename to src/Enum/ValuesTrait.php index 115e7b29..288340e7 100644 --- a/template/serialization/json/list.php +++ b/src/Enum/ValuesTrait.php @@ -1,7 +1,9 @@ +trait ValuesTrait +{ /** - * @return null|string + * @return string[] */ - public function jsonSerialize() + public function values(): array { - return $this->getValue(); + return array_column(self::cases(), 'values'); } -_log = new Logger($logger); - } + use ValuesTrait; + + case EMERGENCY = PLogLevel::EMERGENCY; + case ALERT = PLogLevel::ALERT; + case CRITICAL = PLogLevel::CRITICAL; + case ERROR = PLogLevel::ERROR; + case WARNING = PLogLevel::WARNING; + case NOTICE = PLogLevel::NOTICE; + case INFO = PLogLevel::INFO; + case DEBUG = PLogLevel::DEBUG; } \ No newline at end of file diff --git a/src/Logger.php b/src/Logger.php index 19a078fe..5241ee8c 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,11 +18,59 @@ * limitations under the License. */ -// if the user is running php version 8.0 or greater, the psr/log interface implementation -// has a stricter definition, and we must be able to support that while maintaining backwards -// compatibility -if (80000 <= PHP_VERSION_ID) { - require __DIR__ . '/Logger/Logger80.php'; -} else { - require __DIR__ . '/Logger/Logger74.php'; -} +use Psr\Log\AbstractLogger; +use Psr\Log\LoggerInterface; + +/** + * Class Logger + * @package DCarbone\PHPFHIR + */ +class Logger extends AbstractLogger +{ + /** @var LoggerInterface */ + protected LoggerInterface $actualLogger; + + /** @var LogLevel */ + protected LogLevel $breakLevel; + + /** + * Logger constructor. + * @param LoggerInterface $actualLogger + * @param string|\DCarbone\PHPFHIR\LogLevel $breakLevel + */ + public function __construct(LoggerInterface $actualLogger, string|LogLevel $breakLevel = LogLevel::WARNING) + { + $this->actualLogger = $actualLogger; + + if (is_string($breakLevel)) { + $breakLevel = LogLevel::from($breakLevel); + } + $this->breakLevel = $breakLevel; + } + + /** + * @param string|\Stringable $action + */ + public function startBreak(string|\Stringable $action): void + { + $this->log($this->breakLevel->value, substr(sprintf('%\'-5s Start %s %1$-\'-75s', '-', $action), 0, 75)); + } + + /** + * @param string $level + * @param string|\Stringable $message + * @param array $context + */ + public function log($level, string|\Stringable $message, array $context = array()): void + { + $this->actualLogger->log($level, $message, $context); + } + + /** + * @param string|\Stringable $action + */ + public function endBreak(string|\Stringable $action): void + { + $this->log($this->breakLevel->value, substr(sprintf('%\'-5s End %s %1$-\'-75s', '-', $action), 0, 75)); + } +} \ No newline at end of file diff --git a/src/Logger/Logger74.php b/src/Logger/Logger74.php deleted file mode 100644 index 9c1eed7b..00000000 --- a/src/Logger/Logger74.php +++ /dev/null @@ -1,73 +0,0 @@ -actualLogger = $actualLogger; - $this->breakLevel = $breakLevel; - } - - /** - * @param string $action - */ - public function startBreak(string $action): void - { - $this->log($this->breakLevel, substr(sprintf('%\'-5s Start %s %1$-\'-75s', '-', $action), 0, 75)); - } - - /** - * @param string $level - * @param string $message - * @param array $context - */ - public function log($level, $message, array $context = []) - { - $this->actualLogger->log($level, $message, $context); - } - - /** - * @param string $action - */ - public function endBreak(string $action): void - { - $this->log($this->breakLevel, substr(sprintf('%\'-5s End %s %1$-\'-75s', '-', $action), 0, 75)); - } -} \ No newline at end of file diff --git a/src/Logger/Logger80.php b/src/Logger/Logger80.php deleted file mode 100644 index ae646987..00000000 --- a/src/Logger/Logger80.php +++ /dev/null @@ -1,73 +0,0 @@ -actualLogger = $actualLogger; - $this->breakLevel = $breakLevel; - } - - /** - * @param string|\Stringable $action - */ - public function startBreak(string|\Stringable $action): void - { - $this->log($this->breakLevel, substr(sprintf('%\'-5s Start %s %1$-\'-75s', '-', $action), 0, 75)); - } - - /** - * @param string $level - * @param string|\Stringable $message - * @param array $context - */ - public function log($level, string|\Stringable $message, array $context = array()): void - { - $this->actualLogger->log($level, $message, $context); - } - - /** - * @param string|\Stringable $action - */ - public function endBreak(string|\Stringable $action): void - { - $this->log($this->breakLevel, substr(sprintf('%\'-5s End %s %1$-\'-75s', '-', $action), 0, 75)); - } -} \ No newline at end of file diff --git a/src/Generator/TemplateBuilder.php b/src/Render/Templates.php similarity index 51% rename from src/Generator/TemplateBuilder.php rename to src/Render/Templates.php index 0dea0164..83b80e06 100644 --- a/src/Generator/TemplateBuilder.php +++ b/src/Render/Templates.php @@ -1,9 +1,9 @@ getTestClassName()}.php"; } - /** - * @param \DCarbone\PHPFHIR\Config\VersionConfig $config - * @param \DCarbone\PHPFHIR\Definition\Type $type - * @param bool $gt8 - * @param string $testType - * @return string - */ - public static function buildPHPUnitVersionedTestFilePath(VersionConfig $config, Type $type, bool $gt8, string $testType): string - { - if ($gt8) { - return static::mkdirRecurse( - $config, - self::cleanupPath($type->getFullyQualifiedTestNamespace($testType, false)) - ) . "/{$type->getTestClassName()}_phpunit_gte_8.php"; - } - return static::mkdirRecurse( - $config, - self::cleanupPath($type->getFullyQualifiedTestNamespace($testType, false)) - ) . "/{$type->getTestClassName()}_phpunit_lt_8.php"; - } - /** * @param \DCarbone\PHPFHIR\Config\VersionConfig $config * @param \DCarbone\PHPFHIR\Definition\Type $type diff --git a/src/Utilities/NameUtils.php b/src/Utilities/NameUtils.php index 35df8640..f3cdd236 100644 --- a/src/Utilities/NameUtils.php +++ b/src/Utilities/NameUtils.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Utilities; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -96,8 +96,10 @@ abstract class NameUtils 'y', 'z', ]; + /** @var array */ private const _NUMS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; + /** @var array */ private const _PUNCTUATION_MAP = [ '.' => '_DOT_', @@ -176,10 +178,7 @@ public static function getConstName(string $name): string $lastUpper = false; foreach (str_split($name) as $chr) { if (in_array($chr, self::_UPPER, true) || in_array($chr, self::_NUMS, true)) { - if ('' !== $constName && !$lastUpper && '_' !== substr( - $constName, - -1 - )) { // really simplistic abbreviation detection... + if ('' !== $constName && !$lastUpper && !str_ends_with($constName, '_')) { // really simplistic abbreviation detection... $constName .= '_'; } $constName .= $chr; diff --git a/src/Utilities/TypeBuilderUtils.php b/src/Utilities/TypeBuilderUtils.php index 4be6e019..f39e44d4 100644 --- a/src/Utilities/TypeBuilderUtils.php +++ b/src/Utilities/TypeBuilderUtils.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Utilities; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,7 +40,7 @@ public static function callTypeSetter( \SimpleXMLElement $parentElement, \SimpleXMLElement $source, string $setterMethod, - $value + mixed $value ): void { if (!method_exists($type, $setterMethod)) { throw ExceptionUtils::createTypeSetterMethodNotFoundException( @@ -170,7 +170,7 @@ public static function setTypeIntegerFromAttribute( ) ); } - self::callTypeSetter($type, $parentElement, $attribute, $setterMethod, intval($int, 10)); + self::callTypeSetter($type, $parentElement, $attribute, $setterMethod, intval($int)); } /** @@ -195,21 +195,15 @@ public static function setTypeIntegerFromElementAttribute( /** * @param \DCarbone\PHPFHIR\Definition\Type $type * @param \SimpleXMLElement $parentElement - * @param \SimpleXMLElement $enum + * @param \SimpleXMLElement $enumElement */ - public static function addTypeEnumeratedValue(Type $type, \SimpleXMLElement $parentElement, \SimpleXMLElement $enum): void + public static function addTypeEnumeratedValue(Type $type, \SimpleXMLElement $parentElement, \SimpleXMLElement $enumElement): void { - $value = $enum->attributes()->value; + $value = $enumElement->attributes()->value; if (null === $value) { - throw ExceptionUtils::createExpectedTypeElementAttributeNotFoundException($type, $enum, 'value'); + throw ExceptionUtils::createExpectedTypeElementAttributeNotFoundException($type, $enumElement, 'value'); } - self::callTypeSetter( - $type, - $parentElement, - $enum, - 'addEnumerationValue', - new EnumerationValue((string)$value, $enum) - ); + $type->addEnumerationValue(new EnumerationValue((string)$value, $enumElement)); } /** @@ -224,7 +218,7 @@ public static function callPropertySetter( \SimpleXMLElement $parentElement, \SimpleXMLElement $source, string $setterMethod, - $value + mixed $value ): void { if (!method_exists($property, $setterMethod)) { throw ExceptionUtils::createPropertySetterMethodNotFoundException( diff --git a/src/Utilities/TypeHintUtils.php b/src/Utilities/TypeHintUtils.php index 753c29e0..5af56451 100644 --- a/src/Utilities/TypeHintUtils.php +++ b/src/Utilities/TypeHintUtils.php @@ -3,7 +3,7 @@ namespace DCarbone\PHPFHIR\Utilities; /* - * Copyright 2016-2022 Daniel Carbone (daniel.p.carbone@gmail.com) + * Copyright 2016-2024 Daniel Carbone (daniel.p.carbone@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,62 +21,61 @@ use DCarbone\PHPFHIR\Config\VersionConfig; use DCarbone\PHPFHIR\Definition\Property; use DCarbone\PHPFHIR\Definition\Type; -use DCarbone\PHPFHIR\Enum\PrimitiveTypeEnum; -use DCarbone\PHPFHIR\Enum\TypeKindEnum; +use DCarbone\PHPFHIR\Enum\PrimitiveType; +use DCarbone\PHPFHIR\Enum\TypeKind; abstract class TypeHintUtils { - /** - * @param array $additionalTypes - * @return string - */ - protected static function buildAdditionalTypes(array $additionalTypes): string - { - $additionalTypes = array_unique(array_map('trim', $additionalTypes)); - if ([] === $additionalTypes) { - return ''; - } - return sprintf('%s|', implode('|', $additionalTypes)); - } - /** * @param \DCarbone\PHPFHIR\Config\VersionConfig $config - * @param \DCarbone\PHPFHIR\Enum\PrimitiveTypeEnum $primitiveType + * @param \DCarbone\PHPFHIR\Enum\PrimitiveType $primitiveType * @param bool $nullable * @return string */ - public static function primitivePHPValueTypeHint(VersionConfig $config, PrimitiveTypeEnum $primitiveType, bool $nullable): string + public static function primitivePHPValueTypeHint(VersionConfig $config, PrimitiveType $primitiveType, bool $nullable): string { // this assumes the property's value type is a primiive. // it will bomb if not. return sprintf( '%s%s', - $nullable ? '?' : '', - $primitiveType->getPHPValueTypeHint() + $nullable ? 'null|' : '', + $primitiveType->getPHPReturnValueTypeHint() ); } + public static function primitivePHPReturnValueTypeDoc(VersionConfig $config, PrimitiveType $primitiveType, bool $nullable, bool $asCollection): string + { + $hint = $primitiveType->getPHPReturnValueTypeHint(); + + if ($asCollection) { + return sprintf('%s[]', $hint); + } + + if ($nullable) { + return sprintf('null|%s', $hint); + } + + return $hint; + } + /** * @param \DCarbone\PHPFHIR\Config\VersionConfig $config - * @param \DCarbone\PHPFHIR\Enum\PrimitiveTypeEnum $primitiveType + * @param \DCarbone\PHPFHIR\Enum\PrimitiveType $primitiveType * @param bool $nullable - * @param string ...$additionalTypes + * @param bool $asCollection * @return string */ - public static function primitivePHPValueTypeDoc(VersionConfig $config, PrimitiveTypeEnum $primitiveType, bool $nullable, bool $asCollection, string...$additionalTypes): string + public static function primitivePHPValueTypeSetterDoc(VersionConfig $config, PrimitiveType $primitiveType, bool $nullable, bool $asCollection): string { - // if nullable, add to list of additional types - if ($nullable) { - array_unshift($additionalTypes, 'null'); + $hintTypes = $primitiveType->getPHPReceiveValueTypeHints(); + + if ($asCollection) { + $hintTypes[] = array_map(function(string $v) { return sprintf('%s[]', $v); }, $hintTypes); + } else if ($nullable) { + array_unshift($hintTypes, 'null'); } - $a = self::buildAdditionalTypes($additionalTypes); - return sprintf( - '%s%s%s', - $a, - $primitiveType->getPHPValueTypeHint(), - $asCollection ? '[]' : '' - ); + return implode('|', $hintTypes); } /** @@ -95,12 +94,11 @@ public static function primitiveValuePropertyTypeHint(VersionConfig $config, Pro * @param \DCarbone\PHPFHIR\Definition\Property $property * @param bool $nullable * @param bool $asCollection - * @param string ...$additoinalTypes * @return string */ - public static function primitiveValuePropertyTypeDoc(VersionConfig $config, Property $property, bool $nullable, bool $asCollection, string...$additoinalTypes): string + public static function primitiveValuePropertyTypeDoc(VersionConfig $config, Property $property, bool $nullable, bool $asCollection): string { - return self::primitivePHPValueTypeDoc($config, $property->getMemberOf()->getPrimitiveType(), $nullable, $asCollection); + return self::primitivePHPReturnValueTypeDoc($config, $property->getMemberOf()->getPrimitiveType(), $nullable, $asCollection); } /** @@ -114,7 +112,7 @@ public static function typeTypeHint(VersionConfig $config, Type $type, bool $nul $tk = $type->getKind(); // if this is an inline resource - if ($tk->isOneOf([TypeKindEnum::RESOURCE_INLINE, TypeKindEnum::RESOURCE_CONTAINER])) { + if ($tk->isOneOf(TypeKind::RESOURCE_INLINE, TypeKind::RESOURCE_CONTAINER)) { return sprintf( '%s%s', $nullable ? '?' : '', @@ -130,58 +128,72 @@ public static function typeTypeHint(VersionConfig $config, Type $type, bool $nul ); } + /** + * @param \DCarbone\PHPFHIR\Config\VersionConfig $config + * @param \DCarbone\PHPFHIR\Definition\Type $type + * @return string + */ + public static function typeSetterTypeHint(VersionConfig $config, Type $type): string + { + $tk = $type->getKind(); + $types = ['null']; + + if ($tk === TypeKind::PRIMITIVE_CONTAINER) { + $pt = $type->getProperties()->getProperty('value')->getValueFHIRType(); + $types = array_merge($types, $pt->getPrimitiveType()->getPHPReceiveValueTypeHints()); + array_push( + $types, + $pt->getClassName(), + $type->getClassName(), + ); + } else if ($tk->isOneOf(TypeKind::PRIMITIVE, TypeKind::LIST)) { + $types = array_merge($types, $type->getprimitiveType()->getPHPReceiveValueTypeHints()); + } else { + $types[] = $type->getClassName(); + } + + return implode('|', array_unique($types)); + } + /** * @param \DCarbone\PHPFHIR\Config\VersionConfig $config * @param \DCarbone\PHPFHIR\Definition\Type $type * @param bool $nullable * @param bool $asCollection - * @param string ...$additionalTypes * @return string */ - public static function typeTypeDoc(VersionConfig $config, Type $type, bool $nullable, bool $asCollection, string...$additionalTypes): string + public static function typeTypeDoc(VersionConfig $config, Type $type, bool $nullable, bool $asCollection): string { // if nullable, add to list of additional types - if ($nullable) { - array_unshift($additionalTypes, 'null'); - } - $a = self::buildAdditionalTypes($additionalTypes); - - // whether type is hinted as a collection - $c = $asCollection ? '[]' : ''; + $types = []; // fetch type's kind $tk = $type->getKind(); // if this is an inline resource - if ($tk->isOneOf([TypeKindEnum::RESOURCE_INLINE, TypeKindEnum::RESOURCE_CONTAINER])) { - return sprintf( - '%s%s%s%s', - $a, + if ($tk->isOneOf(TypeKind::RESOURCE_INLINE, TypeKind::RESOURCE_CONTAINER)) { + array_push( + $types, sprintf('\\%s\\', trim($config->getNamespace(true), '\\')), PHPFHIR_INTERFACE_CONTAINED_TYPE, - $c ); + } else { + $types[] = $type->getFullyQualifiedClassName(true); } - // if this is a primitive container type, then we must accept an instance of the primitive type itself - // and the raw php value, thus we must hint for both - if ($tk->isPrimitiveContainer()) { - return sprintf( - '%s%s%s|%s%s', - $a, - self::propertyTypeDoc($config, $type->getProperties()->getProperty('value'), false), - $c, - $type->getFullyQualifiedClassName(true), - $c - ); + // if this type is a collection, add [] to end of each element + if ($asCollection) { + $types = array_map(function (string $n): string { + return sprintf('%s[]', $n); + }, $types); } - return sprintf( - '%s%s%s', - $a, - $type->getFullyQualifiedClassName(true), - $c - ); + // if this type is nullable, prepend list with 'null' + if ($nullable) { + array_unshift($types, 'null'); + } + + return implode('|', $types); } /** @@ -213,17 +225,71 @@ public static function propertyTypeHint(VersionConfig $config, Property $propert * @param \DCarbone\PHPFHIR\Config\VersionConfig $config * @param \DCarbone\PHPFHIR\Definition\Property $property * @param bool $nullable - * @param string ...$additionalTypes * @return string */ - public static function propertyTypeDoc(VersionConfig $config, Property $property, bool $nullable, string...$additionalTypes): string + public static function propertyGetterTypeDoc(VersionConfig $config, Property $property, bool $nullable): string { // determine if this property contains a FHIR type or a raw php type $t = $property->getValueFHIRType(); if (null === $t) { - return self::primitiveValuePropertyTypeDoc($config, $property, $nullable, $property->isCollection(), ...$additionalTypes); + return self::primitiveValuePropertyTypeDoc($config, $property, $nullable, $property->isCollection()); + } + + return self::typeTypeDoc($config, $t, $nullable, $property->isCollection()); + } + + /** + * @param \DCarbone\PHPFHIR\Config\VersionConfig $config + * @param \DCarbone\PHPFHIR\Definition\Property $property + * @param bool $asCollection + * @return string + */ + public static function propertySetterTypeDoc(VersionConfig $config, Property $property, bool $asCollection): string + { + // determine if this property contains a FHIR type or a raw php type + $pt = $property->getValueFHIRType(); + if (null === $pt) { + return self::primitiveValuePropertyTypeDoc($config, $property, !$asCollection, $asCollection); + } + + $ptk = $pt->getKind(); + + $hintTypes = ['null']; + + if ($ptk === TypeKind::PRIMITIVE_CONTAINER) { + $ptp = $pt->getProperties()->getProperty('value')->getValueFHIRType(); + $hintTypes = array_merge($hintTypes, $ptp->getPrimitiveType()->getPHPReceiveValueTypeHints()); + array_push( + $hintTypes, + self::typeTypeDoc($config, $ptp, false, $asCollection), + self::typeTypeDoc($config, $pt, false, $asCollection), + ); + } else if ($ptk->isOneOf(TypeKind::PRIMITIVE, TypeKind::LIST)) { + $hintTypes = array_merge($hintTypes, $pt->getPrimitiveType()->getPHPReceiveValueTypeHints()); + $hintTypes[] = self::typeTypeDoc($config, $pt, false, $asCollection); + } else { + $hintTypes[] = self::typeTypeDoc($config, $pt, false, $asCollection); + } + + return implode('|', array_unique($hintTypes)); + } + + /** + * @param \DCarbone\PHPFHIR\Config\VersionConfig $config + * @param \DCarbone\PHPFHIR\Definition\Property $property + * @return string + */ + public static function propertySetterTypeHint(VersionConfig $config, Property $property): string + { + $pt = $property->getValueFHIRType(); + $ptk = $pt->getKind(); + + $hint = self::typeSetterTypeHint($config, $pt); + + if ($ptk->isOneOf(TypeKind::PRIMITIVE, TypeKind::LIST)) { + return sprintf('%s|%s', $hint, $pt->getClassName()); } - return self::typeTypeDoc($config, $t, $nullable, $property->isCollection(), ...$additionalTypes); + return $hint; } } \ No newline at end of file diff --git a/template/file/header_type.php b/template/file/header_type.php index 0baea145..ac4ef9fc 100644 --- a/template/file/header_type.php +++ b/template/file/header_type.php @@ -1,7 +1,7 @@ getNamespace(false); @@ -28,8 +28,8 @@ if (null === $containerType) { throw new \RuntimeException(sprintf( 'Unable to locate either "%s" or "%s" type', - TypeKindEnum::RESOURCE_CONTAINER, - TypeKindEnum::RESOURCE_INLINE + TypeKind::RESOURCE_CONTAINER->value, + TypeKind::RESOURCE_INLINE->value )); } diff --git a/template/interfaces/phpfhir_type.php b/template/interfaces/phpfhir_type.php index 61742bef..359dde4f 100644 --- a/template/interfaces/phpfhir_type.php +++ b/template/interfaces/phpfhir_type.php @@ -1,7 +1,7 @@ extends \JsonSerializable { - /** - * @param array|null $data - */ - public function __construct(?array $data = null); - /** * Returns the FHIR name represented by this Type * @return string @@ -64,7 +59,7 @@ public function _getFHIRXMLNamespace(): string; * @param string $xmlNamespace * @return static */ - public function _setFHIRXMLNamespace(string $xmlNamespace): object; + public function _setFHIRXMLNamespace(string $xmlNamespace): self; /** * Returns the base xml element definition for this type @@ -97,14 +92,14 @@ public function _isValued(): bool; * @param null|int $libxmlOpts * @return null|static */ - public static function xmlUnserialize($element = null, $type = null, ?int $libxmlOpts = getLibxmlOpts()) ? 'null' : $opts; ?>): ?object; + public static function xmlUnserialize(null|string|\DOMElement $element = null, $type = null, ?int $libxmlOpts = getLibxmlOpts()) ? 'null' : $opts; ?>): ?self; /** * @param \DOMElement|null $element * @param null|int $libxmlOpts * @return string|\DOMElement */ - public function xmlSerialize(?\DOMElement $element = null, ?int $libxmlOpts = getLibxmlOpts()) ? 'null' : $opts; ?>); + public function xmlSerialize(null|\DOMElement $element = null, ?int $libxmlOpts = getLibxmlOpts()) ? 'null' : $opts; ?>): string|\DOMElement; /** * @return string diff --git a/template/methods/constructors/primitive.php b/template/methods/constructors/primitive.php deleted file mode 100644 index 7e8967f7..00000000 --- a/template/methods/constructors/primitive.php +++ /dev/null @@ -1,60 +0,0 @@ -getClassName(); -$primitiveType = $type->getPrimitiveType(); -if (null !== $parentType) { - $parentTypeKind = $parentType->getKind(); -} - -ob_start(); ?> - /** - * getClassName(); ?> Constructor - * @param $value - */ - public function __construct($value = null) - { - isPrimitive() || $parentType->isValueContainer()) : - ?>parent::__construct($value);if (null === $value) { - parent::__construct(); - } elseif (is_scalar($value)) { - parent::__construct(); - $this->setValue($value); - } elseif (is_array($value)) { - parent::__construct($value); - if (isset($value['value'])) { - $this->setValue($value['value']); - } - } else { - throw new \InvalidArgumentException(sprintf( - '::_construct - $data expected to be null, getPHPValueType(); ?>, or array, %s seen', - gettype($value) - )); - }$this->setValue($value); - - } -getKind()->isOneOf([TypeKindEnum::PRIMITIVE, TypeKindEnum::_LIST]); - -ob_start(); -foreach ($properties as $property) : - if ($property->isOverloaded()) : - continue; - endif; - if ($isPrimitiveType && $property->isValueProperty()) : - echo require_with( - __DIR__ . '/default/getter_primitive_value.php', - [ - 'config' => $config, - 'type' => $type, - 'property' => $property, - ] - ); - echo require_with( - __DIR__ . '/default/getter_primitive_value.php', - [ - 'config' => $config, - 'type' => $type, - 'property' => $property, - ] - ); - continue; - endif; - - $propertyType = $property->getValueFHIRType(); - $propertyTypeKind = $propertyType->getKind(); - $requireArgs = [ - 'config' => $config, - 'type' => $type, - 'property' => $property, - ]; - - echo require_with( - __DIR__ . '/default/getter_default.php', - [ - 'config' => $config, - 'property' => $property, - ] - ); - - echo "\n"; - - if ($propertyTypeKind->isOneOf([TypeKindEnum::PRIMITIVE, TypeKindEnum::_LIST, TypeKindEnum::PRIMITIVE_CONTAINER])) : - echo require_with( - __DIR__ . '/default/setter_primitive.php', - $requireArgs - ); - elseif ($propertyTypeKind->isOneOf([TypeKindEnum::RESOURCE_CONTAINER, TypeKindEnum::RESOURCE_INLINE])) : - echo require_with( - __DIR__ . '/default/setter_contained_resource.php', - $requireArgs - ); - else : - echo require_with( - __DIR__ . '/default/setter_default.php', - $requireArgs - ); - endif; - if ($property->isCollection()) : - echo "\n"; - if ($propertyTypeKind->isOneOf([TypeKindEnum::RESOURCE_CONTAINER, TypeKindEnum::RESOURCE_INLINE])) : - echo require_with( - __DIR__ . '/default/setter_contained_resource_collection.php', - $requireArgs - ); - else : - echo require_with( - __DIR__ . '/default/setter_collection.php', - $requireArgs - ); - endif; - endif; - - echo "\n"; - -endforeach; -return substr(ob_get_clean(), 0, -1); // trim off final \n \ No newline at end of file diff --git a/template/properties/methods/primitive.php b/template/properties/methods/primitive.php deleted file mode 100644 index 92da1270..00000000 --- a/template/properties/methods/primitive.php +++ /dev/null @@ -1,108 +0,0 @@ -getPrimitiveType(); - -ob_start(); ?> - /** - * @return - - */ - public function getValue(): - - { - return $this->value; - } - -getValue()) { - // string types - case PrimitiveTypeEnum::CANONICAL: - case PrimitiveTypeEnum::CODE: - case PrimitiveTypeEnum::STRING: - case PrimitiveTypeEnum::SAMPLE_DATA_TYPE: - case PrimitiveTypeEnum::ID: - case PrimitiveTypeEnum::OID: - case PrimitiveTypeEnum::URI: - case PrimitiveTypeEnum::MARKDOWN: // TODO: markdown lib, maybe? - case PrimitiveTypeEnum::UUID: // TODO: implement uuid lib? - case PrimitiveTypeEnum::URL: // TODO: create specific URL type? - $typeFile = __DIR__ . '/primitive/string_type.php'; - break; - - case PrimitiveTypeEnum::BOOLEAN: - $typeFile = __DIR__ . '/primitive/bool_type.php'; - break; - - // int types - case PrimitiveTypeEnum::INTEGER: - case PrimitiveTypeEnum::POSITIVE_INTEGER: - case PrimitiveTypeEnum::NEGATIVE_INTEGER: - $typeFile = __DIR__ . '/primitive/integer_type.php'; - break; - - // treat uint64's as strings for the moment. - case PrimitiveTypeEnum::UNSIGNED_INTEGER: - $typeFile = __DIR__ . '/primitive/unsigned_integer_type.php'; - break; - - case PrimitiveTypeEnum::DECIMAL: - $typeFile = __DIR__ . '/primitive/decimal_type.php'; - break; - - // date types - case PrimitiveTypeEnum::DATE: - $typeFile = __DIR__ . '/primitive/date_type.php'; - break; - case PrimitiveTypeEnum::DATETIME: - $typeFile = __DIR__ . '/primitive/datetime_type.php'; - break; - case PrimitiveTypeEnum::TIME: - $typeFile = __DIR__ . '/primitive/time_type.php'; - break; - case PrimitiveTypeEnum::INSTANT: - $typeFile = __DIR__ . '/primitive/instant_type.php'; - break; - - case PrimitiveTypeEnum::BASE_64_BINARY: - // TODO: add content decoding? - $typeFile = __DIR__ . '/primitive/base64_binary_type.php'; - break; - - default: - throw ExceptionUtils::createUnknownPrimitiveTypeException($type); -} - -echo require_with( - $typeFile, - [ - 'config' => $config, - 'type' => $type, - 'primitiveType' => $primitiveType - ] -); - -return ob_get_clean(); \ No newline at end of file diff --git a/template/tests/test_class_constants.php b/template/tests/test_class_constants.php index 699fac04..077bad56 100644 --- a/template/tests/test_class_constants.php +++ b/template/tests/test_class_constants.php @@ -41,7 +41,7 @@ */ class extends TestCase { -getSortedIterator() as $type) : ?> +getNameSortedIterator() as $type) : ?> public function testTypeConstantsDefinedgetFullyQualifiedClassName(false)); ?>() { $this->assertEquals('getFHIRName(); ?>', getTypeNameConst(true); ?>); diff --git a/template/tests/test_class_type_map.php b/template/tests/test_class_type_map.php index 993ccaff..a56b6946 100644 --- a/template/tests/test_class_type_map.php +++ b/template/tests/test_class_type_map.php @@ -48,7 +48,7 @@ public function testGetTypeClassWithNonStringReturnsNull() public function testGetContainedTypeClassName() { -getSortedIterator() as $type) : +getNameSortedIterator() as $type) : if ($type->isContainedType()) : ?> $this->assertEquals('getFullyQualifiedClassName(true); ?>', ::getContainedTypeClassName('getFHIRName(); ?>')); @@ -59,7 +59,7 @@ public function testGetContainedTypeClassName() public function testIsContainableResourceWithClassname() { -getSortedIterator() as $type) : +getNameSortedIterator() as $type) : if ($type->isContainedType()) : ?> $this->assertTrue(::isContainableResource('getFullyQualifiedClassName(false); ?>')); $this->assertTrue(::isContainableResource('getFullyQualifiedClassName(true); ?>')); @@ -72,7 +72,7 @@ public function testIsContainableResourceWithClassname() public function testIsContainableResourceWithTypeName() { -getSortedIterator() as $type) : +getNameSortedIterator() as $type) : if ($type->isContainedType()) : ?> $this->assertTrue(::isContainableResource('getFHIRName(); ?>')); @@ -83,7 +83,7 @@ public function testIsContainableResourceWithTypeName() public function testIsContainableResourceWithInstance() { -getSortedIterator() as $type) : ?> +getNameSortedIterator() as $type) : ?> $type = new getFullyQualifiedClassName(true); ?>; isContainedType()) : ?> $this->assertTrue(::isContainableResource($type)); diff --git a/template/tests/types/integration/body_domain_resource.php b/template/tests/types/integration/body_domain_resource.php index b36b24c0..6b73087e 100644 --- a/template/tests/types/integration/body_domain_resource.php +++ b/template/tests/types/integration/body_domain_resource.php @@ -1,7 +1,7 @@ isDomainResource()) : echo require_with( - PHPFHIR_TEMPLATE_TESTS_TYPES_DIR . '/' . $testType . '/header_domain_resource.php', + PHPFHIR_TEMPLATE_TESTS_TYPES_DIR . DIRECTORY_SEPARATOR . $testType . DIRECTORY_SEPARATOR . 'header_domain_resource.php', [ 'config' => $config, 'type' => $type, @@ -65,7 +65,7 @@ ); echo require_with( - PHPFHIR_TEMPLATE_TESTS_TYPES_DIR . '/' . $testType . '/body_domain_resource.php', + PHPFHIR_TEMPLATE_TESTS_TYPES_DIR . DIRECTORY_SEPARATOR . $testType . DIRECTORY_SEPARATOR . 'body_domain_resource.php', [ 'config' => $config, 'type' => $type, diff --git a/template/tests/types/integration/header_domain_resource.php b/template/tests/types/integration/header_domain_resource.php index c1cb7808..5a04d2ad 100644 --- a/template/tests/types/integration/header_domain_resource.php +++ b/template/tests/types/integration/header_domain_resource.php @@ -1,7 +1,7 @@ getClassName(); -$properties = $type->getProperties(); -$directProperties = $properties->getDirectSortedIterator(); +$allProperties = $type->getProperties(); +$localProperties = $allProperties->localSortedPropertiesIterator(); ob_start(); ?> diff --git a/template/tests/types/unit/body_primitive.php b/template/tests/types/unit/body_primitive.php index 9656c932..0e1c1d46 100644 --- a/template/tests/types/unit/body_primitive.php +++ b/template/tests/types/unit/body_primitive.php @@ -1,7 +1,7 @@ getPrimitiveType(); // TODO: more different types of strvals... -switch ($primitiveType->getValue()) { - case PrimitiveTypeEnum::INTEGER: - case PrimitiveTypeEnum::POSITIVE_INTEGER: - $strVal = '10'; - break; - - case PrimitiveTypeEnum::NEGATIVE_INTEGER: - $strVal = '-10'; - break; - - case PrimitiveTypeEnum::DECIMAL: - $strVal = '10.5'; - break; - - case PrimitiveTypeEnum::UNSIGNED_INTEGER: - $strVal = (string)PHP_INT_MAX; - break; - - case PrimitiveTypeEnum::BOOLEAN: - $strVal = 'true'; - break; - - default: - $strVal = 'randomstring'; -} +$strVal = match ($primitiveType) { + PrimitiveType::INTEGER, PrimitiveType::POSITIVE_INTEGER => '10', + PrimitiveType::NEGATIVE_INTEGER => '-10', + PrimitiveType::DECIMAL => '10.5', + PrimitiveType::UNSIGNED_INTEGER => (string)PHP_INT_MAX, + PrimitiveType::BOOLEAN => 'true', + default => 'randomstring', +}; ob_start(); ?> diff --git a/template/tests/types/unit/class.php b/template/tests/types/unit/class.php index a180a0c3..b7e12eab 100644 --- a/template/tests/types/unit/class.php +++ b/template/tests/types/unit/class.php @@ -1,7 +1,7 @@ $config, 'type' => $type, @@ -34,16 +36,16 @@ ); echo require_with( - PHPFHIR_TEMPLATE_TESTS_TYPES_DIR . '/' . $testType . '/body_base.php', + PHPFHIR_TEMPLATE_TESTS_TYPES_DIR . DIRECTORY_SEPARATOR . $testType . DIRECTORY_SEPARATOR . 'body_base.php', [ 'config' => $config, 'type' => $type, ] ); -if ($typeKind->isPrimitive()) : +if ($typeKind === TypeKind::PRIMITIVE) : echo require_with( - PHPFHIR_TEMPLATE_TESTS_TYPES_DIR . '/' . $testType . '/body_primitive.php', + PHPFHIR_TEMPLATE_TESTS_TYPES_DIR . DIRECTORY_SEPARATOR . $testType . DIRECTORY_SEPARATOR . 'body_primitive.php', [ 'config' => $config, 'type' => $type, diff --git a/template/tests/types/unit/header.php b/template/tests/types/unit/header.php index 06359bca..0d33ce76 100644 --- a/template/tests/types/unit/header.php +++ b/template/tests/types/unit/header.php @@ -1,7 +1,7 @@ getNamespace(false); ob_start(); -echo "getNamespace(false); ob_start(); -echo "_fhirComments = $fhirComments; return $this; @@ -71,7 +71,7 @@ public function _setFHIRComments(array $fhirComments): object * @param string $fhirComment * @return static */ - public function _addFHIRComment(string $fhirComment): object + public function _addFHIRComment(string $fhirComment): self { $this->_fhirComments[] = $fhirComment; return $this; diff --git a/template/traits/phpfhir_validation_assertions.php b/template/traits/phpfhir_validation_assertions.php index f398c12f..17c20852 100644 --- a/template/traits/phpfhir_validation_assertions.php +++ b/template/traits/phpfhir_validation_assertions.php @@ -1,7 +1,7 @@ getNamespace(false); ob_start(); -echo "getNamespace(false); + +ob_start(); +echo " + /** + * Trait + + * @package \ + + + */ + trait + +{ + /** @var string */ + protected string $_xmlns = ''; + + /** + * @param null|string $xmlNamespace + * @return self + */ + public function _setFHIRXMLNamespace(null|string $xmlNamespace): self + { + $this->_xmlns = trim((string)$xmlNamespace); + return $this; + } + + /** + * @return string + */ + public function _getFHIRXMLNamespace(): string + { + return $this->_xmlns; + } +} + +getClassName(); $typeKind = $type->getKind(); $parentType = $type->getParentType(); -$directProperties = $type->getProperties()->getDirectIterator(); +$localProperties = $type->getProperties()->localPropertiesIterator(); $classDocumentation = $type->getDocBlockDocumentationFragment(1, true); ob_start(); // build file header echo require_with( - PHPFHIR_TEMPLATE_FILE_DIR . '/header_type.php', + PHPFHIR_TEMPLATE_FILE_DIR . DIRECTORY_SEPARATOR . 'header_type.php', [ 'config' => $config, 'fqns' => $fqns, @@ -58,7 +58,7 @@ */ $config, 'type' => $type, @@ -72,10 +72,10 @@ - $config, 'property' => $property, @@ -84,13 +84,10 @@ endforeach; endif; ?> - /** @var string */ - private $_xmlns = ''; - - $config, 'property' => $property, @@ -100,7 +97,7 @@ endif; echo require_with( - PHPFHIR_TEMPLATE_VALIDATION_DIR . '/field_map.php', + PHPFHIR_TEMPLATE_TYPES_VALIDATION_DIR . DIRECTORY_SEPARATOR . 'field_map.php', [ 'config' => $config, 'type' => $type, @@ -110,17 +107,17 @@ echo "\n"; echo require_with( - PHPFHIR_TEMPLATE_METHODS_DIR . '/constructor.php', + PHPFHIR_TEMPLATE_TYPES_METHODS_DIR . DIRECTORY_SEPARATOR . 'constructor.php', [ 'config' => $config, 'type' => $type, - 'properties' => $directProperties, + 'properties' => $localProperties, 'parentType' => $parentType, ] ); echo require_with( - PHPFHIR_TEMPLATE_METHODS_DIR . '/common.php', + PHPFHIR_TEMPLATE_TYPES_METHODS_DIR . DIRECTORY_SEPARATOR . 'common.php', [ 'config' => $config, 'type' => $type, @@ -130,7 +127,7 @@ if ($type->isContainedType()) : echo require_with( - PHPFHIR_TEMPLATE_METHODS_DIR . '/contained_type.php', + PHPFHIR_TEMPLATE_TYPES_METHODS_DIR . DIRECTORY_SEPARATOR . 'contained_type.php', [ 'config' => $config, 'type' => $type, @@ -138,20 +135,20 @@ ); endif; - if (0 < count($directProperties)) : + if (0 < count($localProperties)) : echo "\n"; echo require_with( - PHPFHIR_TEMPLATE_PROPERTIES_DIR . '/methods.php', + PHPFHIR_TEMPLATE_TYPES_PROPERTIES_DIR . DIRECTORY_SEPARATOR . 'methods.php', [ 'config' => $config, 'type' => $type, - 'properties' => $directProperties, + 'properties' => $localProperties, ] ); endif; ?> $config, 'type' => $type, @@ -159,7 +156,7 @@ ); ?> $config, 'type' => $type, @@ -169,10 +166,10 @@ ] ); -if (0 < count($directProperties)) : +if (0 < count($localProperties)) : echo "\n"; echo require_with( - PHPFHIR_TEMPLATE_SERIALIZATION_DIR . '/json.php', + PHPFHIR_TEMPLATE_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'json.php', [ 'config' => $config, 'type' => $type, @@ -189,11 +186,11 @@ */ public function __toString(): string { -isPrimitive()) : +getPrimitiveType(); - if ($primitiveType->is(PrimitiveTypeEnum::BOOLEAN)) : ?> + if ($primitiveType === PrimitiveType::BOOLEAN) : ?> return $this->getValue() ? PHPFHIRConstants::STRING_TRUE : PHPFHIRConstants::STRING_FALSE; -is(PrimitiveTypeEnum::DECIMAL)) : ?> + if (null !== ($v = $this->getValue())) { if (isset($this->_decimals)) { return number_format($v, $this->_decimals); @@ -203,7 +200,7 @@ public function __toString(): string return ''; return (string)$this->getValue(); -isOneOf([TypeKindEnum::_LIST, TypeKindEnum::PRIMITIVE_CONTAINER])) : ?> +isOneOf(TypeKind::LIST, TypeKind::PRIMITIVE_CONTAINER)) : ?> return (string)$this->getValue(); return self::FHIR_TYPE_NAME; diff --git a/template/types/definition.php b/template/types/definition.php index c0eda7d9..19cb2490 100644 --- a/template/types/definition.php +++ b/template/types/definition.php @@ -1,7 +1,7 @@ /** @@ -37,29 +35,6 @@ public function _getFHIRTypeName(): string { return self::FHIR_TYPE_NAME; } - - - /** - * @return string - */ - public function _getFHIRXMLNamespace(): string - { - return $this->_xmlns; - } - - - /** - * @param null|string $xmlNamespace - * @return static - */ - public function _setFHIRXMLNamespace(string $xmlNamespace): object - { - $this->_xmlns = trim((string)$xmlNamespace); - return $this; - } - - - /** * @return string diff --git a/template/methods/constructor.php b/template/types/methods/constructor.php similarity index 62% rename from template/methods/constructor.php rename to template/types/methods/constructor.php index 1fe131e3..943d1141 100644 --- a/template/methods/constructor.php +++ b/template/types/methods/constructor.php @@ -1,7 +1,7 @@ isOneOf([TypeKindEnum::PRIMITIVE, TypeKindEnum::_LIST]) || $type->hasPrimitiveParent()): - echo require_with(PHPFHIR_TEMPLATE_CONSTRUCTORS_DIR . '/primitive.php', $requireArgs); - break; - case $typeKind->isPrimitiveContainer(): - echo require_with(PHPFHIR_TEMPLATE_CONSTRUCTORS_DIR . '/primitive_container.php', $requireArgs); - break; - - default: - echo require_with(PHPFHIR_TEMPLATE_CONSTRUCTORS_DIR . '/default.php', $requireArgs); -endswitch; +echo match ($typeKind) { + TypeKind::PRIMITIVE, TypeKind::LIST => require_with(PHPFHIR_TEMPLATE_TYPES_CONSTRUCTORS_DIR . DIRECTORY_SEPARATOR . 'primitive.php', $requireArgs), + TypeKind::PRIMITIVE_CONTAINER => require_with(PHPFHIR_TEMPLATE_TYPES_CONSTRUCTORS_DIR . DIRECTORY_SEPARATOR . 'primitive_container.php', $requireArgs), + default => require_with(PHPFHIR_TEMPLATE_TYPES_CONSTRUCTORS_DIR . DIRECTORY_SEPARATOR . 'default.php', $requireArgs), +}; echo "\n"; diff --git a/template/methods/constructors/default.php b/template/types/methods/constructors/default.php similarity index 82% rename from template/methods/constructors/default.php rename to template/types/methods/constructors/default.php index d4c7da3a..e7e5877e 100644 --- a/template/methods/constructors/default.php +++ b/template/types/methods/constructors/default.php @@ -1,7 +1,7 @@ Constructor * @param null|array $data */ - public function __construct($data = null) + public function __construct(?array $data = null) { if (null === $data || [] === $data) { return; @@ -41,7 +41,7 @@ public function __construct($data = null) return; throw new \InvalidArgumentException(sprintf( - '::_construct - $data expected to be null or array, %s seen', + '::__construct - $data expected to be null or array, %s seen', gettype($data) )); @@ -61,9 +61,9 @@ public function __construct($data = null) if ($property->isOverloaded()) : continue; endif; - if (($propType = $property->getValueFHIRType()) && $propType->getKind()->isOneOf([TypeKindEnum::RESOURCE_INLINE, TypeKindEnum::RESOURCE_CONTAINER])) : + if (($propType = $property->getValueFHIRType()) && $propType->getKind()->isOneOf(TypeKind::RESOURCE_INLINE, TypeKind::RESOURCE_CONTAINER)) : echo require_with( - PHPFHIR_TEMPLATE_CONSTRUCTORS_DIR . '/resource_container_property_setter_call.php', + PHPFHIR_TEMPLATE_TYPES_CONSTRUCTORS_DIR . DIRECTORY_SEPARATOR . 'resource_container_property_setter_call.php', [ 'config' => $config, 'type' => $type, @@ -72,7 +72,7 @@ public function __construct($data = null) ); else : echo require_with( - PHPFHIR_TEMPLATE_CONSTRUCTORS_DIR . '/default_property_setter_call.php', + PHPFHIR_TEMPLATE_TYPES_CONSTRUCTORS_DIR . DIRECTORY_SEPARATOR . 'default_property_setter_call.php', [ 'config' => $config, 'type' => $type, diff --git a/template/methods/constructors/default_property_setter_call.php b/template/types/methods/constructors/default_property_setter_call.php similarity index 65% rename from template/methods/constructors/default_property_setter_call.php rename to template/types/methods/constructors/default_property_setter_call.php index 0005dd6b..54d56fa9 100644 --- a/template/methods/constructors/default_property_setter_call.php +++ b/template/types/methods/constructors/default_property_setter_call.php @@ -1,7 +1,7 @@ getKind()->isPrimitive() || $propertyType->hasPrimitiveParent()) : +if ($propertyType->getKind()->isOneOf(TypeKind::PRIMITIVE, TypeKind::LIST) || $propertyType->hasPrimitiveParent()) : echo require_with( - __DIR__ . '/property_setter_primitive.php', + __DIR__ . DIRECTORY_SEPARATOR . 'property_setter_primitive.php', $requireArgs ); -elseif ($propertyType->getKind()->isPrimitiveContainer() || $propertyType->hasPrimitiveContainerParent() || $propertyType->isValueContainer()) : +elseif ($propertyType->getKind() === TypeKind::PRIMITIVE_CONTAINER || $propertyType->hasPrimitiveContainerParent() || $propertyType->isValueContainer()) : echo require_with( - __DIR__ . '/property_setter_primitive_container.php', + __DIR__ . DIRECTORY_SEPARATOR . 'property_setter_primitive_container.php', $requireArgs ); else : echo require_with( - __DIR__ . '/property_setter_default.php', + __DIR__ . DIRECTORY_SEPARATOR . 'property_setter_default.php', $requireArgs ); endif; diff --git a/template/types/methods/constructors/primitive.php b/template/types/methods/constructors/primitive.php new file mode 100644 index 00000000..79aa5c6c --- /dev/null +++ b/template/types/methods/constructors/primitive.php @@ -0,0 +1,75 @@ +getClassName(); +$primitiveType = $type->getPrimitiveType(); +$valueProperty = $type->getProperties()->getProperty('value'); + +if (null !== $parentType) { + // if this is a primitive that inherits from a parent primitive, there is no reason to define a constructor here. + if ($parentType->getKind() === TypeKind::PRIMITIVE || $parentType->isValueContainer()) { + return; + } + + // otherwise, assume this is a primitive type who's parent has properties other than just "value" + ob_start(); ?> + /** + * getClassName(); ?> Constructor + * @param |array $value + */ + public function __construct(|array $value = null) + { + if (null === $value) { + parent::__construct(); + } elseif (is_scalar($value)) { + parent::__construct(); + $this->setValue($value); + } elseif (is_array($value)) { + parent::__construct($value); + if (isset($value[self::FIELD_VALUE])) { + $this->setValue($value[self::FIELD_VALUE]); + } + } else { + throw new \InvalidArgumentException(sprintf( + '::__construct - $data expected to be null, getPHPValueTypes(); ?>, or array, %s seen', + gettype($value) + )); + } + } + + /** + * getClassName(); ?> Constructor + * @param $value + */ + public function __construct( $value = null) + { + $this->setValue($value); + } +getValueFHIRType(); -$valuePrimitiveTypeKind = $valuePrimitiveType->getPrimitiveType(); +$valuePropertyType = $valueProperty->getValueFHIRType(); +$valuePropertyPrimitiveType = $valuePropertyType->getPrimitiveType(); $typeImports = $type->getImports(); ob_start(); ?> /** * getClassName(); ?> Constructor - * @param $data + * @param |getClassName(); ?>|array $data */ - public function __construct($data = null) + public function __construct(|array $data = null) { if (null === $data) { return; } - if ($data instanceof getImportByType($valuePrimitiveType); ?>) { + if ($data instanceof getImportByType($valuePropertyType); ?>) { $this->setValue($data); return; } if (is_scalar($data)) { - $this->setValue(new getImportByType($valuePrimitiveType); ?>($data)); + $this->setValue(new getImportByType($valuePropertyType); ?>($data)); return; - } - if (!is_array($data)) { - throw new \InvalidArgumentException(sprintf( - '$data must be null, getPHPValueType(); ?>, instance of getFullyQualifiedClassName(true); ?>, or array. %s seen.', - gettype($data) - )); } parent::__construct($data); @@ -74,7 +68,7 @@ public function __construct($data = null) continue; endif; echo require_with( - PHPFHIR_TEMPLATE_CONSTRUCTORS_DIR . '/default_property_setter_call.php', + PHPFHIR_TEMPLATE_TYPES_CONSTRUCTORS_DIR . DIRECTORY_SEPARATOR . 'default_property_setter_call.php', [ 'config' => $config, 'type' => $type, diff --git a/template/methods/constructors/property_setter_default.php b/template/types/methods/constructors/property_setter_default.php similarity index 97% rename from template/methods/constructors/property_setter_default.php rename to template/types/methods/constructors/property_setter_default.php index 3e17cee9..8bb21132 100644 --- a/template/methods/constructors/property_setter_default.php +++ b/template/types/methods/constructors/property_setter_default.php @@ -1,7 +1,7 @@ getValueFHIRType(); // for children of primitive types, they do not need their own "value" constant as the parent has it -if ($memberOf->hasPrimitiveParent()) : +if ($memberOf->hasPrimitiveParent()) { return ''; -endif; +} ob_start(); ?> const getFieldConstantName(); ?> = 'getName(); ?>'; getKind()->is(TypeKindEnum::PRIMITIVE_CONTAINER) || $propertyType->isValueContainer())) : + ($propertyType->getKind() === TypeKind::PRIMITIVE_CONTAINER || $propertyType->isValueContainer())) : ?> const getFieldConstantName(); ?>_EXT = '_getName(); ?>'; * - * @var + * @var */ protected $getName(); ?> = ; diff --git a/template/properties/methods.php b/template/types/properties/methods.php similarity index 73% rename from template/properties/methods.php rename to template/types/properties/methods.php index 4594147d..5a1825c2 100644 --- a/template/properties/methods.php +++ b/template/types/properties/methods.php @@ -1,7 +1,7 @@ getKind()->isOneOf([TypeKindEnum::PRIMITIVE, TypeKindEnum::_LIST])) : +if ($type->getKind()->isOneOf(TypeKind::PRIMITIVE, TypeKind::LIST)) : echo require_with( - PHPFHIR_TEMPLATE_PROPERTIES_DIR . '/methods/primitive.php', + PHPFHIR_TEMPLATE_TYPES_PROPERTIES_DIR . DIRECTORY_SEPARATOR . 'methods' . DIRECTORY_SEPARATOR . 'primitive.php', [ 'config' => $config, 'type' => $type @@ -34,7 +34,7 @@ ); else : echo require_with( - PHPFHIR_TEMPLATE_PROPERTIES_DIR . '/methods/default.php', + PHPFHIR_TEMPLATE_TYPES_PROPERTIES_DIR . DIRECTORY_SEPARATOR . 'methods' . DIRECTORY_SEPARATOR . 'default.php', [ 'config' => $config, 'type' => $type, diff --git a/template/types/properties/methods/default.php b/template/types/properties/methods/default.php new file mode 100644 index 00000000..f35f3a5e --- /dev/null +++ b/template/types/properties/methods/default.php @@ -0,0 +1,104 @@ +getKind()->isOneOf(TypeKind::PRIMITIVE, TypeKind::LIST); + +ob_start(); +foreach ($properties as $property) { + if ($property->isOverloaded()) { + continue; + } + if ($isPrimitiveType && $property->isValueProperty()) { + echo require_with( + __DIR__ . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'getter_primitive_value.php', + [ + 'config' => $config, + 'type' => $type, + 'property' => $property, + ] + ); + echo require_with( + __DIR__ . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'getter_primitive_value.php', + [ + 'config' => $config, + 'type' => $type, + 'property' => $property, + ] + ); + continue; + } + + $propertyType = $property->getValueFHIRType(); + $propertyTypeKind = $propertyType->getKind(); + $requireArgs = [ + 'config' => $config, + 'type' => $type, + 'property' => $property, + ]; + + echo require_with( + __DIR__ . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'getter_default.php', + [ + 'config' => $config, + 'property' => $property, + ] + ); + + echo "\n"; + + if ($propertyTypeKind->isOneOf(TypeKind::PRIMITIVE, TypeKind::LIST, TypeKind::PRIMITIVE_CONTAINER)) { + echo require_with( + __DIR__ . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'setter_primitive.php', + $requireArgs + ); + } else if ($propertyTypeKind->isOneOf(TypeKind::RESOURCE_CONTAINER, TypeKind::RESOURCE_INLINE)) { + echo require_with( + __DIR__ . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'setter_contained_resource.php', + $requireArgs + ); + } else { + echo require_with( + __DIR__ . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'setter_default.php', + $requireArgs + ); + } + if ($property->isCollection()) { + echo "\n"; + if ($propertyTypeKind->isOneOf(TypeKind::RESOURCE_CONTAINER, TypeKind::RESOURCE_INLINE)) { + echo require_with( + __DIR__ . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'setter_contained_resource_collection.php', + $requireArgs + ); + } else { + echo require_with( + __DIR__ . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'setter_collection.php', + $requireArgs + ); + } + } + + echo "\n"; + +} +return substr(ob_get_clean(), 0, -1); // trim off final \n \ No newline at end of file diff --git a/template/properties/methods/default/getter_default.php b/template/types/properties/methods/default/getter_default.php similarity index 89% rename from template/properties/methods/default/getter_default.php rename to template/types/properties/methods/default/getter_default.php index 939eb544..e9508695 100644 --- a/template/properties/methods/default/getter_default.php +++ b/template/types/properties/methods/default/getter_default.php @@ -1,7 +1,7 @@ * - * @return + * @return */ public function get(): diff --git a/template/properties/methods/default/getter_primitive_value.php b/template/types/properties/methods/default/getter_primitive_value.php similarity index 86% rename from template/properties/methods/default/getter_primitive_value.php rename to template/types/properties/methods/default/getter_primitive_value.php index b48e1f30..e43d54ed 100644 --- a/template/properties/methods/default/getter_primitive_value.php +++ b/template/types/properties/methods/default/getter_primitive_value.php @@ -1,7 +1,7 @@ /** - * @return + * @return */ public function getValue(): diff --git a/template/properties/methods/default/setter_collection.php b/template/types/properties/methods/default/setter_collection.php similarity index 93% rename from template/properties/methods/default/setter_collection.php rename to template/types/properties/methods/default/setter_collection.php index d4051fe9..d48985ff 100644 --- a/template/properties/methods/default/setter_collection.php +++ b/template/types/properties/methods/default/setter_collection.php @@ -1,7 +1,7 @@ (array $ = []): object + public function set(array $ = []): self { if ([] !== $this->) { $this->_trackValuesRemoved(count($this->)); diff --git a/template/properties/methods/default/setter_contained_resource.php b/template/types/properties/methods/default/setter_contained_resource.php similarity index 93% rename from template/properties/methods/default/setter_contained_resource.php rename to template/types/properties/methods/default/setter_contained_resource.php index 567481fe..c1d8befb 100644 --- a/template/properties/methods/default/setter_contained_resource.php +++ b/template/types/properties/methods/default/setter_contained_resource.php @@ -1,7 +1,7 @@ (? $ = null): object + public function (? $ = null): self { $this->_trackValueAdded($this->_trackValueSet($this->, $); $this-> = $; diff --git a/template/properties/methods/default/setter_contained_resource_collection.php b/template/types/properties/methods/default/setter_contained_resource_collection.php similarity index 95% rename from template/properties/methods/default/setter_contained_resource_collection.php rename to template/types/properties/methods/default/setter_contained_resource_collection.php index 52d9f333..a746befc 100644 --- a/template/properties/methods/default/setter_contained_resource_collection.php +++ b/template/types/properties/methods/default/setter_contained_resource_collection.php @@ -1,7 +1,7 @@ (array $ = []): object + public function set(array $ = []): self { if ([] !== $this->) { $this->_trackValuesRemoved(count($this->)); diff --git a/template/properties/methods/default/setter_default.php b/template/types/properties/methods/default/setter_default.php similarity index 81% rename from template/properties/methods/default/setter_default.php rename to template/types/properties/methods/default/setter_default.php index 2538698c..83d57437 100644 --- a/template/properties/methods/default/setter_default.php +++ b/template/types/properties/methods/default/setter_default.php @@ -1,7 +1,7 @@ * - * @param $ + * @param $ * @return static */ - public function ( $ = null): object + public function ( $ = null): self { + if (null === $) { + $ = new (); + } $this->_trackValueAdded($this->_trackValueSet($this->, $); $this-> = $; return $this; diff --git a/template/properties/methods/default/setter_primitive.php b/template/types/properties/methods/default/setter_primitive.php similarity index 74% rename from template/properties/methods/default/setter_primitive.php rename to template/types/properties/methods/default/setter_primitive.php index 90d9fcbe..3b26caac 100644 --- a/template/properties/methods/default/setter_primitive.php +++ b/template/types/properties/methods/default/setter_primitive.php @@ -1,7 +1,7 @@ * - * @param $ + * @param $ * @return static */ - public function ($ = null): object + public function ( $ = null): self { - if (null !== $ && !($ instanceof )) { + if (null === $) { + $this-> = null; + $this->_trackValuesRemoved(1); + return $this; + } + if (!($ instanceof )) { $ = new ($); } $this->_trackValueAdded($this->_trackValueSet($this->, $); diff --git a/template/types/properties/methods/primitive.php b/template/types/properties/methods/primitive.php new file mode 100644 index 00000000..d87b9455 --- /dev/null +++ b/template/types/properties/methods/primitive.php @@ -0,0 +1,108 @@ +getPrimitiveType(); + +ob_start(); ?> + /** + * @return + + */ + public function getValue(): + + { + return $this->value; + } + + $config, + 'type' => $type, + 'primitiveType' => $primitiveType + ] +); + +return ob_get_clean(); \ No newline at end of file diff --git a/template/properties/methods/primitive/base64_binary_type.php b/template/types/properties/methods/primitive/base64_binary_type.php similarity index 75% rename from template/properties/methods/primitive/base64_binary_type.php rename to template/types/properties/methods/primitive/base64_binary_type.php index a2d1efdd..072d6328 100644 --- a/template/properties/methods/primitive/base64_binary_type.php +++ b/template/types/properties/methods/primitive/base64_binary_type.php @@ -1,7 +1,7 @@ /** - * @param $value + * @param $value * @return static */ - public function setValue($value): object + public function setValue( $value): self { if (null === $value) { $this->value = null; - } elseif (is_string($value)) { - $this->value = $value; } else { - throw new \InvalidArgumentException(sprintf('Value must be null or string, %s seen', gettype($value))); + $this->value = $value; } return $this; } diff --git a/template/properties/methods/primitive/bool_type.php b/template/types/properties/methods/primitive/bool_type.php similarity index 75% rename from template/properties/methods/primitive/bool_type.php rename to template/types/properties/methods/primitive/bool_type.php index 3c11bca9..53e3a408 100644 --- a/template/properties/methods/primitive/bool_type.php +++ b/template/types/properties/methods/primitive/bool_type.php @@ -1,7 +1,7 @@ /** - * @param $value + * @param $value * @return static */ - public function setValue($value = null): object + public function setValue( $value = null): self { if (null === $value) { $this->value = null; diff --git a/template/properties/methods/primitive/date_type.php b/template/types/properties/methods/primitive/date_type.php similarity index 80% rename from template/properties/methods/primitive/date_type.php rename to template/types/properties/methods/primitive/date_type.php index d1815608..cdb3d3b0 100644 --- a/template/properties/methods/primitive/date_type.php +++ b/template/types/properties/methods/primitive/date_type.php @@ -1,7 +1,7 @@ /** - * @param $value + * @param $value * @return static */ - public function setValue($value): object + public function setValue( $value): self { if (null === $value) { $this->value = null; @@ -47,19 +47,12 @@ public function setValue($value): object /** * @return null|\DateTimeInterface */ - public function _getDateTime(): ?\DateTimeInterface + public function _getDateTime(): null|\DateTimeInterface { $value = $this->getValue(); if (null === $value) { return null; } - if ([] !== $this->_getValidationErrors()) { - throw new \DomainException(sprintf( - 'Cannot convert "%s" to \\DateTime as it does not conform to "%s"', - $value, - self::$_validationRules[self::FIELD_VALUE][PHPFHIRConstants::] - )); - } switch(strlen($value)) { case 4: $dt = \DateTime::createFromFormat(PHPFHIRConstants::DATE_FORMAT_YEAR, $value); diff --git a/template/properties/methods/primitive/datetime_type.php b/template/types/properties/methods/primitive/datetime_type.php similarity index 81% rename from template/properties/methods/primitive/datetime_type.php rename to template/types/properties/methods/primitive/datetime_type.php index f62f9cd0..950d4c24 100644 --- a/template/properties/methods/primitive/datetime_type.php +++ b/template/types/properties/methods/primitive/datetime_type.php @@ -1,7 +1,7 @@ /** - * @param $value + * @param $value * @return static */ - public function setValue($value): object + public function setValue( $value): self { if (null === $value) { $this->value = null; @@ -47,19 +47,12 @@ public function setValue($value): object /** * @return null|\DateTimeInterface */ - public function _getDateTime(): ?\DateTimeInterface + public function _getDateTime(): null|\DateTimeInterface { $value = $this->getValue(); if (null === $value) { return null; } - if ([] !== $this->_getValidationErrors()) { - throw new \DomainException(sprintf( - 'Cannot convert "%s" to \\DateTime as it does not conform to "%s"', - $value, - self::$_validationRules[self::FIELD_VALUE][PHPFHIRConstants::] - )); - } switch(strlen($value)) { case 4: $dt = \DateTime::createFromFormat(PHPFHIRConstants::DATE_FORMAT_YEAR, $value); diff --git a/template/properties/methods/primitive/decimal_type.php b/template/types/properties/methods/primitive/decimal_type.php similarity index 56% rename from template/properties/methods/primitive/decimal_type.php rename to template/types/properties/methods/primitive/decimal_type.php index a9977575..d97e7743 100644 --- a/template/properties/methods/primitive/decimal_type.php +++ b/template/types/properties/methods/primitive/decimal_type.php @@ -1,7 +1,7 @@ /** @var int */ - private $_decimals; + private int $_decimals; /** - * @param $value + * @param $value * @return static */ - public function setValue($value): object + public function setValue( $value): self { if (null === $value) { $this->value = null; - } elseif (is_scalar($value)) { - if (is_string($value)) { - $dec = strstr($value, '.'); - if (false === $dec) { - $this->_decimals = 1; - } else { - $this->_decimals = strlen($dec) - 1; - } + } else { + $str = (string)$value; + $dec = strstr($str, '.'); + if (false === $dec) { + $this->_decimals = 1; + } else { + $this->_decimals = strlen($dec) - 1; } $this->value = floatval($value); - } else { - throw new \InvalidArgumentException(sprintf('getFHIRName(); ?> value must be null, float, or numeric string, %s seen.', gettype($value))); } return $this; } diff --git a/template/properties/methods/primitive/instant_type.php b/template/types/properties/methods/primitive/instant_type.php similarity index 74% rename from template/properties/methods/primitive/instant_type.php rename to template/types/properties/methods/primitive/instant_type.php index 70a8ae39..c8b3b57a 100644 --- a/template/properties/methods/primitive/instant_type.php +++ b/template/types/properties/methods/primitive/instant_type.php @@ -1,7 +1,7 @@ /** - * @param $value + * @param $value * @return static */ - public function setValue($value): object + public function setValue( $value): self { if (null === $value) { $this->value = null; @@ -53,13 +53,6 @@ public function _getDateTime(): ?\DateTimeInterface if (null === $value) { return null; } - if ([] !== $this->_getValidationErrors()) { - throw new \DomainException(sprintf( - 'Cannot convert "%s" to \\DateTime as it does not conform to "%s"', - $value, - self::$_validationRules[self::FIELD_VALUE][PHPFHIRConstants::] - )); - } $dt = \DateTime::createFromFormat(PHPFHIRConstants::DATE_FORMAT_INSTANT, $value); if (!($dt instanceof \DateTime)) { throw new \UnexpectedValueException(sprintf('Unable to parse value "%s" into \DateTime instance with expected format "%s"', $value, PHPFHIRConstants::DATE_FORMAT_INSTANT)); diff --git a/template/properties/methods/primitive/integer_type.php b/template/types/properties/methods/primitive/integer_type.php similarity index 72% rename from template/properties/methods/primitive/integer_type.php rename to template/types/properties/methods/primitive/integer_type.php index 8ca568a5..4e12fe95 100644 --- a/template/properties/methods/primitive/integer_type.php +++ b/template/types/properties/methods/primitive/integer_type.php @@ -1,7 +1,7 @@ /** - * @param $value + * @param $value * @return static */ - public function setValue($value): object + public function setValue( $value): self { if (null === $value) { $this->value = null; @@ -48,9 +46,6 @@ public function setValue($value): object $value = $neg * intval($value, 10); } } - if (!is_int($value)) { - throw new \InvalidArgumentException(sprintf('Value must be null, integer, or string containing only numbers, %s seen.', $value)); - } $this->value = $value; return $this; } diff --git a/template/properties/methods/primitive/string_type.php b/template/types/properties/methods/primitive/string_type.php similarity index 69% rename from template/properties/methods/primitive/string_type.php rename to template/types/properties/methods/primitive/string_type.php index 3c881cd8..580386b2 100644 --- a/template/properties/methods/primitive/string_type.php +++ b/template/types/properties/methods/primitive/string_type.php @@ -1,7 +1,7 @@ /** - * @param $value + * @param $value * @return static */ - public function setValue($value): object + public function setValue( $value): self { if (null === $value) { $this->value = null; - } elseif (is_string($value)) { - $this->value = $value; } else { - throw new \InvalidArgumentException(sprintf('Value must be null or string, %s seen', gettype($value))); + $this->value = $value; } return $this; } diff --git a/template/properties/methods/primitive/time_type.php b/template/types/properties/methods/primitive/time_type.php similarity index 74% rename from template/properties/methods/primitive/time_type.php rename to template/types/properties/methods/primitive/time_type.php index c6d06632..7f795e1d 100644 --- a/template/properties/methods/primitive/time_type.php +++ b/template/types/properties/methods/primitive/time_type.php @@ -1,7 +1,7 @@ /** - * @param $value + * @param $value * @return static */ - public function setValue($value): object + public function setValue( $value): self { if (null === $value) { $this->value = null; @@ -53,13 +53,6 @@ public function _getDateTime(): ?\DateTimeInterface if (null === $value) { return null; } - if ([] !== $this->_getValidationErrors()) { - throw new \DomainException(sprintf( - 'Cannot convert "%s" to \\DateTime as it does not conform to "%s"', - $value, - self::$_validationRules[self::FIELD_VALUE][PHPFHIRConstants::PHPFHIR_VALIDATION_ENUM_NAME] - )); - } $dt = \DateTime::createFromFormat(PHPFHIRConstants::TIME_FORMAT, $value); if (!($dt instanceof \DateTime)) { throw new \UnexpectedValueException(sprintf('Unable to parse value "%s" into \DateTime instance with expected format "%s"', $value, PHPFHIRConstants::TIME_FORMAT)); diff --git a/template/properties/methods/primitive/unsigned_integer_type.php b/template/types/properties/methods/primitive/unsigned_integer_type.php similarity index 76% rename from template/properties/methods/primitive/unsigned_integer_type.php rename to template/types/properties/methods/primitive/unsigned_integer_type.php index fd2c2c78..8f108fb5 100644 --- a/template/properties/methods/primitive/unsigned_integer_type.php +++ b/template/types/properties/methods/primitive/unsigned_integer_type.php @@ -1,7 +1,7 @@ /** - * @param $value + * @param $value * @return static */ - public function setValue($value): object + public function setValue( $value): self { if (null === $value) { $this->value = null; return $this; } - if (is_float($value) || is_string($value)) { - $value = intval($value, 10); + if (is_float($value)) { + $value = intval($value); } if (is_int($value)) { if (0 > $value) { throw new \OutOfBoundsException(sprintf('Value must be >= 0, %d seen.', $value)); } - $value = strval($value); + $value = (string)$value; } if (!is_string($value) || !ctype_digit($value)) { throw new \InvalidArgumentException(sprintf('Value must be null, positive integer, or string representation of positive integer, "%s" seen.', gettype($value))); diff --git a/template/serialization/json.php b/template/types/serialization/json.php similarity index 59% rename from template/serialization/json.php rename to template/types/serialization/json.php index f445c0eb..0a36e92d 100644 --- a/template/serialization/json.php +++ b/template/types/serialization/json.php @@ -1,7 +1,7 @@ getProperties()->getDirectIterator(); +$localProperties = $type->getProperties()->localPropertiesIterator(); $typeKind = $type->getKind(); ob_start(); -if ($typeKind->isOneOf([TypeKindEnum::PRIMITIVE, TypeKindEnum::_LIST])) : +if ($typeKind->isOneOf(TypeKind::PRIMITIVE, TypeKind::LIST)) : echo require_with( - PHPFHIR_TEMPLATE_SERIALIZATION_DIR . '/json/primitive.php', + PHPFHIR_TEMPLATE_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'primitive.php', [ 'config' => $config, 'type' => $type, 'typeKind' => $typeKind, ] ); -elseif ($typeKind->isOneOf([TypeKindEnum::RESOURCE_CONTAINER, TypeKindEnum::RESOURCE_INLINE])) : +elseif ($typeKind->isOneOf(TypeKind::RESOURCE_CONTAINER, TypeKind::RESOURCE_INLINE)) : echo require_with( - PHPFHIR_TEMPLATE_SERIALIZATION_DIR . '/json/resource_container.php', + PHPFHIR_TEMPLATE_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'resource_container.php', [ 'config' => $config, - 'properties' => $directProperties, + 'properties' => $localProperties, ] ); else: echo require_with( - PHPFHIR_TEMPLATE_SERIALIZATION_DIR . '/json/default.php', + PHPFHIR_TEMPLATE_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'default.php', [ 'config' => $config, 'type' => $type, - 'properties' => $directProperties, + 'properties' => $localProperties, ] ); endif; diff --git a/template/serialization/json/default.php b/template/types/serialization/json/default.php similarity index 80% rename from template/serialization/json/default.php rename to template/types/serialization/json/default.php index 02a32dd7..b7eb972f 100644 --- a/template/serialization/json/default.php +++ b/template/types/serialization/json/default.php @@ -1,7 +1,7 @@ getParentType()) : ?> $out = parent::jsonSerialize(); @@ -44,17 +44,17 @@ public function jsonSerialize() continue; endif; $propertyType = $property->getValueFHIRType(); - if ($propertyType->getKind()->isOneOf([TypeKindEnum::PRIMITIVE, TypeKindEnum::_LIST])) : + if ($propertyType->getKind()->isOneOf(TypeKind::PRIMITIVE, TypeKind::LIST)) : echo require_with( - __DIR__ . '/default_property_primitive_list.php', + __DIR__ . DIRECTORY_SEPARATOR . 'default_property_primitive_list.php', [ 'config' => $config, 'property' => $property ] ); - elseif ($propertyType->isValueContainer() || $propertyType->getKind()->isPrimitiveContainer() || $propertyType->hasPrimitiveContainerParent()) : + elseif ($propertyType->isValueContainer() || $propertyType->getKind() === TypeKind::PRIMITIVE_CONTAINER || $propertyType->hasPrimitiveContainerParent()) : echo require_with( - __DIR__ . '/default_property_value_primitive_container.php', + __DIR__ . DIRECTORY_SEPARATOR . 'default_property_value_primitive_container.php', [ 'config' => $config, 'property' => $property @@ -62,7 +62,7 @@ public function jsonSerialize() ); else : echo require_with( - __DIR__ . '/default_property_default.php', + __DIR__ . DIRECTORY_SEPARATOR . 'default_property_default.php', [ 'config' => $config, 'property' => $property diff --git a/template/serialization/json/default_property_default.php b/template/types/serialization/json/default_property_default.php similarity index 95% rename from template/serialization/json/default_property_default.php rename to template/types/serialization/json/default_property_default.php index 243c562a..c00ea113 100644 --- a/template/serialization/json/default_property_default.php +++ b/template/types/serialization/json/default_property_default.php @@ -1,7 +1,7 @@ /** - * @return getPrimitiveType(), true, false); ?> + * @return getPrimitiveType(), true, false); ?> */ - public function jsonSerialize() + public function jsonSerialize(): mixed { -is(TypeKindEnum::PRIMITIVE) && false !== strpos($type->getFHIRName(), 'unsigned')) : ?> +getFHIRName(), 'unsigned')) : ?> return intval($this->getValue(), 10); return $this->getValue(); diff --git a/template/serialization/json/resource_container.php b/template/types/serialization/json/resource_container.php similarity index 87% rename from template/serialization/json/resource_container.php rename to template/types/serialization/json/resource_container.php index 7f7e345d..b7dcec74 100644 --- a/template/serialization/json/resource_container.php +++ b/template/types/serialization/json/resource_container.php @@ -1,7 +1,7 @@ /** - * @return object|null + * @return null|object */ - public function jsonSerialize() + public function jsonSerialize(): mixed { if (null !== ($v = $this->getgetName()); ?>())) { diff --git a/template/serialization/xml.php b/template/types/serialization/xml.php similarity index 64% rename from template/serialization/xml.php rename to template/types/serialization/xml.php index 29e197ee..6cc9d624 100644 --- a/template/serialization/xml.php +++ b/template/types/serialization/xml.php @@ -1,7 +1,7 @@ getProperties()->getDirectIterator(); +$localProperties = $type->getProperties()->localPropertiesIterator(); $properties = $type->getAllPropertiesIterator(); ob_start(); // unserialize portion echo require_with( - PHPFHIR_TEMPLATE_SERIALIZATION_DIR . '/xml/unserialize/header.php', + PHPFHIR_TEMPLATE_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR . 'unserialize' . DIRECTORY_SEPARATOR . 'header.php', [ 'config' => $config, 'type' => $type, @@ -44,7 +44,7 @@ if (0 < count($properties)) : echo require_with( - PHPFHIR_TEMPLATE_SERIALIZATION_DIR . '/xml/unserialize/body.php', + PHPFHIR_TEMPLATE_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR . 'unserialize' . DIRECTORY_SEPARATOR . 'body.php', [ 'config' => $config, 'type' => $type, @@ -59,9 +59,9 @@ isOneOf([TypeKindEnum::RESOURCE_CONTAINER, TypeKindEnum::RESOURCE_INLINE])) : +if ($typeKind->isOneOf(TypeKind::RESOURCE_CONTAINER, TypeKind::RESOURCE_INLINE)) : echo require_with( - PHPFHIR_TEMPLATE_SERIALIZATION_DIR . '/xml/serialize/resource_container.php', + PHPFHIR_TEMPLATE_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR . 'serialize' . DIRECTORY_SEPARATOR . 'resource_container.php', [ 'config' => $config, 'type' => $type, @@ -71,22 +71,21 @@ // everything else shares a common header // header is always output as it is what creates the simplexml instance echo require_with( - PHPFHIR_TEMPLATE_SERIALIZATION_DIR . '/xml/serialize/default_header.php', + PHPFHIR_TEMPLATE_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR . 'serialize' . DIRECTORY_SEPARATOR . 'header.php', [ 'config' => $config, 'parentType' => $parentType, ] ); - $properties = $type->getProperties()->getDirectIterator(); - if (0 < count($directProperties)) : + if (0 < count($localProperties)) : echo require_with( - PHPFHIR_TEMPLATE_SERIALIZATION_DIR . '/xml/serialize/default_body.php', + PHPFHIR_TEMPLATE_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR . 'serialize' . DIRECTORY_SEPARATOR . 'body.php', [ 'config' => $config, 'type' => $type, 'parentType' => $parentType, - 'directProperties' => $directProperties, + 'localProperties' => $localProperties, ] ); endif; diff --git a/template/serialization/xml/serialize/default_body.php b/template/types/serialization/xml/serialize/body.php similarity index 79% rename from template/serialization/xml/serialize/default_body.php rename to template/types/serialization/xml/serialize/body.php index 88a16bf0..fcf47e4b 100644 --- a/template/serialization/xml/serialize/default_body.php +++ b/template/types/serialization/xml/serialize/body.php @@ -1,7 +1,7 @@ getValueFHIRType()) : echo require_with( - __DIR__ . '/default_body_typed.php', + __DIR__ . DIRECTORY_SEPARATOR . 'body_typed.php', [ 'config' => $config, 'property' => $property, @@ -33,7 +33,7 @@ ); elseif (null === $parentType) : echo require_with( - __DIR__ . '/default_body_untyped.php', + __DIR__ . DIRECTORY_SEPARATOR . 'body_untyped.php', [ 'config' => $config, ] diff --git a/template/serialization/xml/serialize/default_body_typed.php b/template/types/serialization/xml/serialize/body_typed.php similarity index 76% rename from template/serialization/xml/serialize/default_body_typed.php rename to template/types/serialization/xml/serialize/body_typed.php index 0303eb4a..9b0017a8 100644 --- a/template/serialization/xml/serialize/default_body_typed.php +++ b/template/types/serialization/xml/serialize/body_typed.php @@ -1,7 +1,7 @@ getValueFHIRType()->getKind(); ob_start(); -if ($propertyTypeKind->isOneOf([TypeKindEnum::RESOURCE_CONTAINER, TypeKindEnum::RESOURCE_INLINE])) : +if ($propertyTypeKind->isOneOf(TypeKind::RESOURCE_CONTAINER, TypeKind::RESOURCE_INLINE)) : echo require_with( - __DIR__ . '/default_body_typed_resource_container.php', + __DIR__ . DIRECTORY_SEPARATOR . 'body_typed_resource_container.php', [ 'config' => $config, 'property' => $property, @@ -34,7 +34,7 @@ ); else : echo require_with( - __DIR__ . '/default_body_typed_default.php', + __DIR__ . DIRECTORY_SEPARATOR . 'body_typed_default.php', [ 'config' => $config, 'property' => $property, diff --git a/template/serialization/xml/serialize/default_body_typed_default.php b/template/types/serialization/xml/serialize/body_typed_default.php similarity index 92% rename from template/serialization/xml/serialize/default_body_typed_default.php rename to template/types/serialization/xml/serialize/body_typed_default.php index 6811c06b..9c02b823 100644 --- a/template/serialization/xml/serialize/default_body_typed_default.php +++ b/template/types/serialization/xml/serialize/body_typed_default.php @@ -1,7 +1,7 @@ if (null !== ($v = $this->())) { -hasPrimitiveParent() || $propertyType->getKind()->isPrimitive()) : ?> +hasPrimitiveParent() || $propertyType->getKind() === TypeKind::PRIMITIVE) : ?> $element->setAttribute(self::, (string)$v); $telement = $element->ownerDocument->createElement(self::); diff --git a/template/serialization/xml/serialize/default_body_typed_resource_container.php b/template/types/serialization/xml/serialize/body_typed_resource_container.php similarity index 96% rename from template/serialization/xml/serialize/default_body_typed_resource_container.php rename to template/types/serialization/xml/serialize/body_typed_resource_container.php index c3ac0eaf..982d7b91 100644 --- a/template/serialization/xml/serialize/default_body_typed_resource_container.php +++ b/template/types/serialization/xml/serialize/body_typed_resource_container.php @@ -1,7 +1,7 @@ getProperties()->getDirectIterator(); +$localProperties = $type->getProperties()->localPropertiesIterator(); ob_start(); ?> /** @@ -29,7 +29,7 @@ */ public function xmlSerialize(\DOMElement $element = null, ?int $libxmlOpts = getLibxmlOpts()) ? 'null' : $opts; ?>): \DOMElement { - + if (null !== ($v = $this->getgetName()); ?>())) { return $v->xmlSerialize($element, $libxmlOpts); } diff --git a/template/serialization/xml/unserialize/body.php b/template/types/serialization/xml/unserialize/body.php similarity index 84% rename from template/serialization/xml/unserialize/body.php rename to template/types/serialization/xml/unserialize/body.php index 5aafd700..25cbd24a 100644 --- a/template/serialization/xml/unserialize/body.php +++ b/template/types/serialization/xml/unserialize/body.php @@ -1,7 +1,7 @@ $property) : if (null !== $property->getValueFHIRType()) : echo require_with( - __DIR__ . '/body_parse_node_typed.php', + __DIR__ . DIRECTORY_SEPARATOR . 'body_parse_node_typed.php', $requireArgs + [ 'property' => $property, 'i' => $i, @@ -41,7 +41,7 @@ ); else : echo require_with( - __DIR__ . '/body_parse_node_primitive.php', + __DIR__ . DIRECTORY_SEPARATOR . 'body_parse_node_primitive.php', $requireArgs + [ 'property' => $property, 'i' => $i, @@ -54,7 +54,7 @@ $property) : if (null !== $property->getValueFHIRType()) : echo require_with( - __DIR__ . '/body_parse_attr_typed.php', + __DIR__ . DIRECTORY_SEPARATOR . 'body_parse_attr_typed.php', $requireArgs + [ 'property' => $property, 'i' => $i, @@ -62,7 +62,7 @@ ); else : echo require_with( - __DIR__ . '/body_parse_attr_primitive.php', + __DIR__ . DIRECTORY_SEPARATOR . 'body_parse_attr_primitive.php', $requireArgs + [ 'property' => $property, 'i' => $i, diff --git a/template/serialization/xml/unserialize/body_parse_attr_primitive.php b/template/types/serialization/xml/unserialize/body_parse_attr_primitive.php similarity index 83% rename from template/serialization/xml/unserialize/body_parse_attr_primitive.php rename to template/types/serialization/xml/unserialize/body_parse_attr_primitive.php index 4a015c08..96ba3c50 100644 --- a/template/serialization/xml/unserialize/body_parse_attr_primitive.php +++ b/template/types/serialization/xml/unserialize/body_parse_attr_primitive.php @@ -1,7 +1,7 @@ attributes->getNamedItem(self::getFieldConstantName(); ?>); if (null !== $n) { $type->setValue($n->nodeValue); -getKind()->is(TypeKindEnum::PHPFHIR_XHTML)) : ?> +getKind() === TypeKind::PHPFHIR_XHTML) : ?> $type->_setElementName($n->nodeName); } diff --git a/template/serialization/xml/unserialize/body_parse_attr_typed.php b/template/types/serialization/xml/unserialize/body_parse_attr_typed.php similarity index 85% rename from template/serialization/xml/unserialize/body_parse_attr_typed.php rename to template/types/serialization/xml/unserialize/body_parse_attr_typed.php index 70f25cd2..946c4bcf 100644 --- a/template/serialization/xml/unserialize/body_parse_attr_typed.php +++ b/template/types/serialization/xml/unserialize/body_parse_attr_typed.php @@ -1,7 +1,7 @@ getSetterName(); ob_start(); -if ($propertyType->getKind()->isOneOf([TypeKindEnum::PRIMITIVE, TypeKindEnum::_LIST, TypeKindEnum::PRIMITIVE_CONTAINER])) : ?> +if ($propertyType->getKind()->isOneOf(TypeKind::PRIMITIVE, TypeKind::LIST, TypeKind::PRIMITIVE_CONTAINER)) : ?> $n = $element->attributes->getNamedItem(self::); if (null !== $n) { $pt = $type->getGetterName(); ?>(); diff --git a/template/serialization/xml/unserialize/body_parse_node_primitive.php b/template/types/serialization/xml/unserialize/body_parse_node_primitive.php similarity index 87% rename from template/serialization/xml/unserialize/body_parse_node_primitive.php rename to template/types/serialization/xml/unserialize/body_parse_node_primitive.php index 13861931..ddf51e3f 100644 --- a/template/serialization/xml/unserialize/body_parse_node_primitive.php +++ b/template/types/serialization/xml/unserialize/body_parse_node_primitive.php @@ -1,7 +1,7 @@ setValue($n->textContent); } -getKind()->is(TypeKindEnum::PHPFHIR_XHTML)) : ?> +getKind() === TypeKind::PHPFHIR_XHTML) : ?> $type->_setElementName($n->nodeName); }isOneOf([TypeKindEnum::RESOURCE_CONTAINER, TypeKindEnum::RESOURCE_INLINE])) : +if ($propertyTypeKind->isOneOf(TypeKind::RESOURCE_CONTAINER, TypeKind::RESOURCE_INLINE)) : echo require_with( - __DIR__ . '/body_parse_typed_resource_container.php', + __DIR__ . DIRECTORY_SEPARATOR . 'body_parse_typed_resource_container.php', $requireArgs + [ 'property' => $property, 'i' => $i, @@ -41,7 +41,7 @@ ); else : echo require_with( - __DIR__ . '/body_parse_typed_default.php', + __DIR__ . DIRECTORY_SEPARATOR . 'body_parse_typed_default.php', $requireArgs + [ 'property' => $property, 'i' => $i, diff --git a/template/serialization/xml/unserialize/body_parse_typed_default.php b/template/types/serialization/xml/unserialize/body_parse_typed_default.php similarity index 94% rename from template/serialization/xml/unserialize/body_parse_typed_default.php rename to template/types/serialization/xml/unserialize/body_parse_typed_default.php index 7a903ec9..5bb05f12 100644 --- a/template/serialization/xml/unserialize/body_parse_typed_default.php +++ b/template/types/serialization/xml/unserialize/body_parse_typed_default.php @@ -1,7 +1,7 @@ getFullyQualifiedClassName(true); ?> */ - public static function xmlUnserialize($element = null, $type = null, ?int $libxmlOpts = getLibxmlOpts()) ? 'null' : $opts; ?>): ? + public static function xmlUnserialize(null|string|\DOMElement $element = null, $type = null, ?int $libxmlOpts = getLibxmlOpts()) ? 'null' : $opts; ?>): null| { if (null === $element) { diff --git a/template/validation/field_map.php b/template/types/validation/field_map.php similarity index 87% rename from template/validation/field_map.php rename to template/types/validation/field_map.php index 01126748..48e36f3b 100644 --- a/template/validation/field_map.php +++ b/template/types/validation/field_map.php @@ -1,7 +1,7 @@ getProperties()->getSortedIterator(); +$properties = $type->getProperties()->allSortedPropertiesIterator(); ob_start(); ?> /** @@ -26,7 +26,7 @@ * @var array */ - private static array $_validationRules = [buildValidationMap(); if ([] !== $validationMap) : ?> diff --git a/template/validation/methods.php b/template/types/validation/methods.php similarity index 81% rename from template/validation/methods.php rename to template/types/validation/methods.php index 7de4ceb4..f3f05460 100644 --- a/template/validation/methods.php +++ b/template/types/validation/methods.php @@ -1,7 +1,7 @@ $validationRules = $this->_getValidationRules(); -getProperties()->getDirectIterator() as $property) : +getProperties()->localPropertiesIterator() as $property) : $propertyType = $property->getValueFHIRType(); if (null === $propertyType) : if ($property->isCollection()) : echo require_with( - PHPFHIR_TEMPLATE_VALIDATION_DIR . '/methods/collection_typed.php', + PHPFHIR_TEMPLATE_TYPES_VALIDATION_DIR . DIRECTORY_SEPARATOR . 'methods' . DIRECTORY_SEPARATOR . 'collection_typed.php', $requireArgs + ['property' => $property] ); else : echo require_with( - PHPFHIR_TEMPLATE_VALIDATION_DIR . '/methods/primitive.php', + PHPFHIR_TEMPLATE_TYPES_VALIDATION_DIR . DIRECTORY_SEPARATOR . 'methods' . DIRECTORY_SEPARATOR . 'primitive.php', $requireArgs + ['property' => $property] ); endif; else : if ($property->isCollection()) : echo require_with( - PHPFHIR_TEMPLATE_VALIDATION_DIR . '/methods/collection_typed.php', + PHPFHIR_TEMPLATE_TYPES_VALIDATION_DIR . DIRECTORY_SEPARATOR . 'methods' . DIRECTORY_SEPARATOR . 'collection_typed.php', $requireArgs + ['property' => $property] ); else : echo require_with( - PHPFHIR_TEMPLATE_VALIDATION_DIR . '/methods/typed.php', + PHPFHIR_TEMPLATE_TYPES_VALIDATION_DIR . DIRECTORY_SEPARATOR . 'methods' . DIRECTORY_SEPARATOR . 'typed.php', $requireArgs + ['property' => $property] ); endif; @@ -85,7 +85,7 @@ public function _getValidationErrors(): array if (null !== $type->getParentType()) : $ptype = $type; while (null !== $ptype) : - foreach($ptype->getProperties()->getDirectIterator() as $property) : ?> + foreach($ptype->getProperties()->localPropertiesIterator() as $property) : ?> if (isset($validationRules[self::getFieldConstantName(); ?>])) { $v = $this->getGetterName(); ?>(); foreach($validationRules[self::getFieldConstantName(); ?>] as $rule => $constraint) { diff --git a/template/validation/methods/collection_primitive.php b/template/types/validation/methods/collection_primitive.php similarity index 95% rename from template/validation/methods/collection_primitive.php rename to template/types/validation/methods/collection_primitive.php index 8768dc8d..54800040 100644 --- a/template/validation/methods/collection_primitive.php +++ b/template/types/validation/methods/collection_primitive.php @@ -1,7 +1,7 @@ $config, 'fqns' => $fqns, @@ -52,7 +52,7 @@ * @package */ - $config, 'type' => $type, 'parentType' => null]); ?> + $config, 'type' => $type, 'parentType' => null]); ?> // name of FHIR type this class describes const FHIR_TYPE_NAME = getTypeNameConst(true); ?>; @@ -62,10 +62,10 @@ /** @var null|string */ private ?string $_elementName = null; /** @var string */ - private string $_xmlns = ''; + protected string $_xmlns = ''; /** @var array */ - private static array $_validationRules = []; + private const _VALIDATION_RULES = []; /** * Constructor @@ -92,7 +92,7 @@ public function _setElementName(string $elementName): getClass $config, 'type' => $type, @@ -153,7 +153,7 @@ public function _setData($data): getClassName(); ?> $config, 'type' => $type, @@ -163,7 +163,7 @@ public function _setData($data): getClassName(); ?> $config, 'type' => $type, diff --git a/template/utilities/autoloader_class.php b/template/utilities/autoloader_class.php index 111611e2..15e7833e 100644 --- a/template/utilities/autoloader_class.php +++ b/template/utilities/autoloader_class.php @@ -1,7 +1,7 @@ ', false)) { require __DIR__ . DIRECTORY_SEPARATOR . '.php'; } +if (!trait_exists('\', false)) { + require __DIR__ . DIRECTORY_SEPARATOR . '.php'; +} // common classes if (!class_exists('\', false)) { diff --git a/template/utilities/constants.php b/template/utilities/constants.php index 5c9a7122..d3020da5 100644 --- a/template/utilities/constants.php +++ b/template/utilities/constants.php @@ -1,7 +1,7 @@ public const = ''; // Type names -getSortedIterator() as $type) : ?> +getNameSortedIterator() as $type) : ?> public const getTypeNameConst(false); ?> = 'getFHIRName(); ?>'; // Type classes -getSortedIterator() as $type) : ?> +getNameSortedIterator() as $type) : ?> public const getClassNameConst(false); ?> = 'getFullyQualifiedClassName(true)); ?>'; value, + TypeKind::RESOURCE_INLINE->value )); } /** @var \DCarbone\PHPFHIR\Definition\Type[] $innerTypes */ $innerTypes = []; -foreach ($containerType->getProperties()->getIterator() as $property) { +foreach ($containerType->getProperties()->allPropertiesIterator() as $property) { if ($ptype = $property->getValueFHIRType()) { $innerTypes[$ptype->getFHIRName()] = $ptype; } @@ -69,7 +69,7 @@ abstract class * This array represents every type known to this lib */ private const TYPE_MAP = [ -getSortedIterator() as $type) : ?> +getNameSortedIterator() as $type) : ?> getTypeNameConst(true); ?> => getClassNameConst(true); ?>, ];