Skip to content

Commit

Permalink
moving xmlns stuff into trait
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Apr 12, 2024
1 parent 273b8a6 commit 99bee0f
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 49 deletions.
1 change: 1 addition & 0 deletions files/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
const PHPFHIR_TRAIT_COMMENT_CONTAINER = 'PHPFHIRCommentContainerTrait';
const PHPFHIR_TRAIT_VALIDATION_ASSERTIONS = 'PHPFHIRValidationAssertionsTrait';
const PHPFHIR_TRAIT_CHANGE_TRACKING = 'PHPFHIRChangeTrackingTrait';
const PHPFHIR_TRAIT_XMLNS = 'PHPFHIRXmlNamespaceTrait';

// validation constants
const PHPFHIR_VALIDATION_ENUM = 'enum';
Expand Down
9 changes: 9 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,15 @@ protected function renderStaticClasses(): void
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(
FileUtils::buildGenericFilePath(
$this->config,
Expand Down
4 changes: 4 additions & 0 deletions src/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,10 @@ public function getDirectlyUsedTraits(): array
}
$traits[] = PHPFHIR_TRAIT_VALIDATION_ASSERTIONS;
$traits[] = PHPFHIR_TRAIT_CHANGE_TRACKING;

if ($this->getKind() !== TypeKind::PRIMITIVE) {
$traits[] = PHPFHIR_TRAIT_XMLNS;
}
}

return $traits;
Expand Down
35 changes: 20 additions & 15 deletions src/Render/Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class Templates
*/
public static function renderPHPFHIRTypeInterface(VersionConfig $config, Types $types): string
{
return require PHPFHIR_TEMPLATE_INTERFACES_DIR .DIRECTORY_SEPARATOR. 'phpfhir_type.php';
return require PHPFHIR_TEMPLATE_INTERFACES_DIR . DIRECTORY_SEPARATOR . 'phpfhir_type.php';
}

/**
Expand All @@ -45,7 +45,7 @@ public static function renderPHPFHIRTypeInterface(VersionConfig $config, Types $
*/
public static function renderPHPFHIRCommentContainerInterface(VersionConfig $config, Types $types): string
{
return require PHPFHIR_TEMPLATE_INTERFACES_DIR . DIRECTORY_SEPARATOR.'phpfhir_comment_container.php';
return require PHPFHIR_TEMPLATE_INTERFACES_DIR . DIRECTORY_SEPARATOR .'phpfhir_comment_container.php';
}

/**
Expand All @@ -55,7 +55,7 @@ public static function renderPHPFHIRCommentContainerInterface(VersionConfig $con
*/
public static function renderPHPFHIRCommentContainerTrait(VersionConfig $config, Types $types): string
{
return require PHPFHIR_TEMPLATE_TRAITS_DIR .DIRECTORY_SEPARATOR. 'phpfhir_comment_container.php';
return require PHPFHIR_TEMPLATE_TRAITS_DIR . DIRECTORY_SEPARATOR . 'phpfhir_comment_container.php';
}

/**
Expand All @@ -65,7 +65,7 @@ public static function renderPHPFHIRCommentContainerTrait(VersionConfig $config,
*/
public static function renderPHPFHIRValidationAssertionsTrait(VersionConfig $config, Types $types): string
{
return require PHPFHIR_TEMPLATE_TRAITS_DIR . DIRECTORY_SEPARATOR.'phpfhir_validation_assertions.php';
return require PHPFHIR_TEMPLATE_TRAITS_DIR . DIRECTORY_SEPARATOR .'phpfhir_validation_assertions.php';
}

/**
Expand All @@ -75,7 +75,12 @@ public static function renderPHPFHIRValidationAssertionsTrait(VersionConfig $con
*/
public static function renderPHPFHIRChangeTrackingTrait(VersionConfig $config, Types $types): string
{
return require PHPFHIR_TEMPLATE_TRAITS_DIR .DIRECTORY_SEPARATOR. 'phpfhir_change_tracking.php';
return require PHPFHIR_TEMPLATE_TRAITS_DIR . DIRECTORY_SEPARATOR . 'phpfhir_change_tracking.php';
}

public static function renderPHPFHIRXMLNamespaceTrait(VersionConfig $config, Types $types): string
{
return require PHPFHIR_TEMPLATE_TRAITS_DIR . DIRECTORY_SEPARATOR . 'phpfhir_xml_namespace.php';
}

/**
Expand All @@ -85,7 +90,7 @@ public static function renderPHPFHIRChangeTrackingTrait(VersionConfig $config, T
*/
public static function renderPHPFHIRContainedTypeInterface(VersionConfig $config, Types $types): string
{
return require PHPFHIR_TEMPLATE_INTERFACES_DIR .DIRECTORY_SEPARATOR. 'phpfhir_contained_type.php';
return require PHPFHIR_TEMPLATE_INTERFACES_DIR . DIRECTORY_SEPARATOR . 'phpfhir_contained_type.php';
}

/**
Expand All @@ -95,7 +100,7 @@ public static function renderPHPFHIRContainedTypeInterface(VersionConfig $config
*/
public static function renderPHPFHIRResponseParserConfigClass(VersionConfig $config, Types $types): string
{
return require PHPFHIR_TEMPLATE_UTILITIES_DIR . DIRECTORY_SEPARATOR.'response_parser_config_class.php';
return require PHPFHIR_TEMPLATE_UTILITIES_DIR . DIRECTORY_SEPARATOR .'response_parser_config_class.php';
}

/**
Expand All @@ -105,7 +110,7 @@ public static function renderPHPFHIRResponseParserConfigClass(VersionConfig $con
*/
public static function renderPHPFHIRResponseParserClass(VersionConfig $config, Types $types): string
{
return require PHPFHIR_TEMPLATE_UTILITIES_DIR . DIRECTORY_SEPARATOR.'response_parser_class.php';
return require PHPFHIR_TEMPLATE_UTILITIES_DIR . DIRECTORY_SEPARATOR .'response_parser_class.php';
}

/**
Expand All @@ -116,7 +121,7 @@ public static function renderPHPFHIRResponseParserClass(VersionConfig $config, T
*/
public static function renderTypeClass(VersionConfig $config, Types $types, Type $type): string
{
return require PHPFHIR_TEMPLATE_TYPES_DIR .DIRECTORY_SEPARATOR. 'class.php';
return require PHPFHIR_TEMPLATE_TYPES_DIR . DIRECTORY_SEPARATOR . 'class.php';
}

/**
Expand All @@ -127,7 +132,7 @@ public static function renderTypeClass(VersionConfig $config, Types $types, Type
*/
public static function renderRawTypeClass(VersionConfig $config, Types $types, Type $type): string
{
return require PHPFHIR_TEMPLATE_TYPES_DIR .DIRECTORY_SEPARATOR . 'xhtml.php';
return require PHPFHIR_TEMPLATE_TYPES_DIR . DIRECTORY_SEPARATOR . 'xhtml.php';
}

/**
Expand All @@ -137,7 +142,7 @@ public static function renderRawTypeClass(VersionConfig $config, Types $types, T
*/
public static function renderConstants(VersionConfig $config, Types $types): string
{
return require PHPFHIR_TEMPLATE_UTILITIES_DIR . DIRECTORY_SEPARATOR.'constants.php';
return require PHPFHIR_TEMPLATE_UTILITIES_DIR . DIRECTORY_SEPARATOR .'constants.php';
}

/**
Expand All @@ -147,7 +152,7 @@ public static function renderConstants(VersionConfig $config, Types $types): str
*/
public static function renderTypeMapClass(VersionConfig $config, Types $types): string
{
return require PHPFHIR_TEMPLATE_UTILITIES_DIR .DIRECTORY_SEPARATOR. 'typemap_class.php';
return require PHPFHIR_TEMPLATE_UTILITIES_DIR . DIRECTORY_SEPARATOR . 'typemap_class.php';
}

/**
Expand All @@ -157,7 +162,7 @@ public static function renderTypeMapClass(VersionConfig $config, Types $types):
*/
public static function renderAutoloaderClass(VersionConfig $config, Types $types): string
{
return require PHPFHIR_TEMPLATE_UTILITIES_DIR .DIRECTORY_SEPARATOR. 'autoloader_class.php';
return require PHPFHIR_TEMPLATE_UTILITIES_DIR . DIRECTORY_SEPARATOR . 'autoloader_class.php';
}

/**
Expand All @@ -167,7 +172,7 @@ public static function renderAutoloaderClass(VersionConfig $config, Types $types
*/
public static function renderConstantsTestClass(VersionConfig $config, Types $types): string
{
return require PHPFHIR_TEMPLATE_TESTS_DIR . DIRECTORY_SEPARATOR. 'test_class_constants.php';
return require PHPFHIR_TEMPLATE_TESTS_DIR . DIRECTORY_SEPARATOR . 'test_class_constants.php';
}

/**
Expand All @@ -177,7 +182,7 @@ public static function renderConstantsTestClass(VersionConfig $config, Types $ty
*/
public static function renderTypeMapTestClass(VersionConfig $config, Types $types): string
{
return require PHPFHIR_TEMPLATE_TESTS_DIR . DIRECTORY_SEPARATOR. 'test_class_type_map.php';
return require PHPFHIR_TEMPLATE_TESTS_DIR . DIRECTORY_SEPARATOR . 'test_class_type_map.php';
}

/**
Expand Down
25 changes: 0 additions & 25 deletions template/methods/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
/** @var \DCarbone\PHPFHIR\Definition\Type|null $parentType */
/** @var null|bool $skipTypeName */
/** @var null|bool $skipGetXMLNamespace */
/** @var null|bool $skipGetXMLDefinition */

$xmlName = NameUtils::getTypeXMLElementName($type);
$skipTypeName = isset($skipTypeName) && $skipTypeName;
$skipGetXMLNamespace = isset($skipGetXMLNamespace) && $skipGetXMLNamespace;
$skipGetXMLDefinition = isset($skipGetXMLDefinition) && $skipGetXMLDefinition;

ob_start(); ?>
/**
Expand All @@ -37,29 +35,6 @@ public function _getFHIRTypeName(): string
{
return self::FHIR_TYPE_NAME;
}
<?php if (null === $parentType) : ?><?php if (!$skipGetXMLNamespace) : ?>

/**
* @return string
*/
public function _getFHIRXMLNamespace(): string
{
return $this->_xmlns;
}
<?php endif; ?>

<?php if (!$skipGetXMLDefinition) : ?> /**
* @param null|string $xmlNamespace
* @return static
*/
public function _setFHIRXMLNamespace(string $xmlNamespace): object
{
$this->_xmlns = trim((string)$xmlNamespace);
return $this;
}
<?php endif; ?>

<?php endif; ?>

/**
* @return string
Expand Down
4 changes: 2 additions & 2 deletions template/traits/phpfhir_change_tracking.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php declare(strict_types=1);

/*
* Copyright 2022 Daniel Carbone (daniel.p.carbone@gmail.com)
* Copyright 2022-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.
Expand All @@ -23,7 +23,7 @@
$rootNS = $config->getNamespace(false);

ob_start();
echo "<?php\n\n";
echo "<?php declare(strict_types=1);\n\n";

if ('' !== $rootNS) :
echo "namespace {$rootNS};\n\n";
Expand Down
2 changes: 1 addition & 1 deletion template/traits/phpfhir_comment_container.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
$rootNS = $config->getNamespace(false);

ob_start();
echo "<?php\n\n";
echo "<?php declare(strict_types=1);\n\n";

if ('' !== $rootNS) :
echo "namespace {$rootNS};\n\n";
Expand Down
4 changes: 2 additions & 2 deletions template/traits/phpfhir_validation_assertions.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php declare(strict_types=1);

/*
* Copyright 2022 Daniel Carbone (daniel.p.carbone@gmail.com)
* Copyright 2022-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.
Expand All @@ -23,7 +23,7 @@
$rootNS = $config->getNamespace(false);

ob_start();
echo "<?php\n\n";
echo "<?php declare(strict_types=1);\n\n";

if ('' !== $rootNS) :
echo "namespace {$rootNS};\n\n";
Expand Down
68 changes: 68 additions & 0 deletions template/traits/phpfhir_xml_namespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php declare(strict_types=1);

/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use DCarbone\PHPFHIR\Utilities\CopyrightUtils;

/** @var \DCarbone\PHPFHIR\Config\VersionConfig $config */

$rootNS = $config->getNamespace(false);

ob_start();
echo "<?php declare(strict_types=1);\n\n";

if ('' !== $rootNS) :
echo "namespace {$rootNS};\n\n";
endif;

echo CopyrightUtils::getFullPHPFHIRCopyrightComment();

echo "\n\n";
?>
/**
* Trait <?php echo PHPFHIR_TRAIT_XMLNS; if ('' !== $rootNS) : ?>

* @package \<?php echo $rootNS; ?>
<?php endif; ?>

*/
trait <?php echo PHPFHIR_TRAIT_XMLNS; ?>

{
/** @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;
}
}

<?php return ob_get_clean();
3 changes: 0 additions & 3 deletions template/types/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@
endforeach;
endif; ?>

/** @var string */
private $_xmlns = '';

<?php if (0 !== count($directProperties)) :
foreach($directProperties as $property) :
echo require_with(
Expand Down
2 changes: 1 addition & 1 deletion template/types/xhtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
/** @var null|string */
private ?string $_elementName = null;
/** @var string */
private string $_xmlns = '';
protected string $_xmlns = '';

/** @var array */
private static array $_validationRules = [];
Expand Down
3 changes: 3 additions & 0 deletions template/utilities/autoloader_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
if (!trait_exists('\<?php echo $nsPrefix . PHPFHIR_TRAIT_CHANGE_TRACKING; ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_TRAIT_CHANGE_TRACKING; ?>.php';
}
if (!trait_exists('\<?php echo $nsPrefix . PHPFHIR_TRAIT_XMLNS; ?>', false)) {
require __DIR__ . DIRECTORY_SEPARATOR . '<?php echo PHPFHIR_TRAIT_XMLNS; ?>.php';
}

// common classes
if (!class_exists('\<?php echo $nsPrefix . PHPFHIR_CLASSNAME_CONSTANTS; ?>', false)) {
Expand Down

0 comments on commit 99bee0f

Please sign in to comment.