Skip to content

Commit

Permalink
Merge pull request #137 from dcarbone/dcarbone/validation-fixes
Browse files Browse the repository at this point in the history
Validation fixes
  • Loading branch information
dcarbone authored Aug 22, 2024
2 parents 9112e10 + 6ff7a54 commit a1eb69a
Show file tree
Hide file tree
Showing 17 changed files with 183 additions and 124 deletions.
2 changes: 1 addition & 1 deletion bin/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

/usr/bin/env php "${DIR}"/generate.php "$@"
php "${DIR}"/generate.php "$@"
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
"require": {
"php": "^8.1",
"ext-ctype": "*",
"ext-curl": "*",
"ext-json": "*",
"ext-libxml": "*",
"ext-simplexml": "*",
"psr/log": "^3.0"
},
"require-dev": {
"ext-curl": "*",
"ext-dom": "*",
"ext-xmlreader": "*",
"ext-xmlwriter": "*",
Expand Down
8 changes: 8 additions & 0 deletions files/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,11 @@
const PHPFHIR_TEST_CLASSNAME_AUTOLOADER = PHPFHIR_CLASSNAME_AUTOLOADER . 'Test';
const PHPFHIR_TEST_CLASSNAME_TYPEMAP = PHPFHIR_CLASSNAME_TYPEMAP . 'Test';
const PHPFHIR_TEST_CLASSNAME_CONFIG = PHPFHIR_CLASSNAME_CONFIG . 'Test';

// date & time formats
const PHPFHIR_DATE_FORMAT_YEAR = 'Y';
const PHPFHIR_DATE_FORMAT_YEAR_MONTH = 'Y-m';
const PHPFHIR_DATE_FORMAT_YEAR_MONTH_DAY = 'Y-m-d';
const PHPFHIR_DATE_FORMAT_YEAR_MONTH_DAY_TIME = 'Y-m-d\\TH:i:s\\.uP';
const PHPFHIR_DATE_FORMAT_INSTANT = 'Y-m-d\\TH:i:s\\.uP';
const PHPFHIR_TIME_FORMAT = 'H:i:s';
25 changes: 21 additions & 4 deletions src/Definition/Decorator/RestrictionElementTypeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,38 @@ public static function decorate(VersionConfig $config, Types $types, Type $type,
}

foreach ($restriction->children('xs', true) as $child) {
$attrs = $child->attributes();;
switch ($child->getName()) {
case ElementName::SIMPLE_TYPE->value:
SimpleTypeElementTypeDecorator::decorate($config, $types, $type, $child);
break;
case ElementName::PATTERN->value:
$type->setPattern((string)$child);
if (isset($attrs[AttributeName::VALUE->value])) {
$type->setpattern((string)$attrs[AttributeName::VALUE->value]);
} else if ('' !== ($v = (string)$child)) {
$type->setpattern($v);
}
break;
case ElementName::MIN_LENGTH->value:
$type->setMinLength(intval((string)$child));
if (isset($attrs[AttributeName::VALUE->value])) {
$type->setMinLength(intval((string)$attrs[AttributeName::VALUE->value]));
} else if ('' !== ($v = (string)$child)) {
$type->setMinLength(intval($v));
}
break;
case ElementName::MAX_LENGTH->value:
$type->setMaxLength(intval((string)$child));
if (isset($attrs[AttributeName::VALUE->value])) {
$type->setMaxLength(intval((string)$attrs[AttributeName::VALUE->value]));
} else if ('' !== ($v = (string)$child)) {
$type->setMaxLength(intval($v));
}
break;
case ElementName::ENUMERATION->value:
$type->addEnumerationValue(new EnumerationValue((string)$child->attributes()->value, $child));
if (isset($attrs[AttributeName::VALUE->value])) {
$type->addEnumerationValue(new EnumerationValue((string)$attrs[AttributeName::VALUE->value], $attrs[AttributeName::VALUE->value]));
} else if ('' !== ($v = (string)$child)) {
$type->addEnumerationValue(new EnumerationValue($v, $child));
}
break;
case ElementName::SEQUENCE->value:
SequenceElementTypeDecorator::decorate($config, $types, $type, $child);
Expand Down
104 changes: 51 additions & 53 deletions src/Definition/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

use DCarbone\PHPFHIR\Enum\PrimitiveType;
use DCarbone\PHPFHIR\Enum\PropertyUse;
use DCarbone\PHPFHIR\Enum\TypeKind;
use DCarbone\PHPFHIR\Utilities\NameUtils;
use InvalidArgumentException;
use SimpleXMLElement;
Expand All @@ -38,21 +37,21 @@ class Property
private Type $memberOf;

/** @var string|null */
private ?string $name = null;
private null|string $name = null;

/** @var string|null */
private ?string $valueFHIRTypeName = null;
private null|string $valueFHIRTypeName = null;

/** @var null|string */
private ?string $rawPHPValue = null;
private null|string $rawPHPValue = null;

/** @var int */
private int $minOccurs = 0;
/** @var int */
private int $maxOccurs = 1;
/** @var null|int */
private null|int $maxOccurs = null;

/** @var string|null */
private ?string $pattern = null;
/** @var null|string */
private null|string $pattern = null;

/** @var null|\DCarbone\PHPFHIR\Definition\Type */
private ?Type $valueFHIRType = null;
Expand All @@ -61,13 +60,13 @@ class Property
private PropertyUse $use;

/** @var string|null */
private ?string $ref = null;
private null|string $ref = null;

/** @var null|string */ // TODO: what the hell is this...?
private ?string $fixed = null;
private null|string $fixed = null;

/** @var null|string */ // NOTE: not a php namespace
private ?string $namespace = null;
private null|string $namespace = null;

/** @var bool */
private bool $overloaded = false;
Expand Down Expand Up @@ -118,7 +117,7 @@ public function getMemberOf(): Type
/**
* @return string|null
*/
public function getName(): ?string
public function getName(): null|string
{
return $this->name;
}
Expand All @@ -144,7 +143,7 @@ public function setName(string $name): Property
/**
* @return string|null
*/
public function getValueFHIRTypeName(): ?string
public function getValueFHIRTypeName(): null|string
{
return $this->valueFHIRTypeName;
}
Expand Down Expand Up @@ -181,7 +180,7 @@ public function setValueFHIRType(Type $valueFHIRType): Property
/**
* @return string|null
*/
public function getRawPHPValue(): ?string
public function getRawPHPValue(): null|string
{
return $this->rawPHPValue;
}
Expand All @@ -197,9 +196,9 @@ public function setRawPHPValue(?string $rawPHPValue): Property
}

/**
* @return int
* @return null|int
*/
public function getMaxOccurs(): int
public function getMaxOccurs(): null|int
{
return $this->maxOccurs;
}
Expand Down Expand Up @@ -241,9 +240,15 @@ public function setMinOccurs(int $minOccurs): Property
*
* @return null|string
*/
public function getPattern(): ?string
public function getPattern(): null|string
{
return $this->pattern;
if (isset($this->pattern)) {
return $this->pattern;
}
if (isset($this->valueFHIRType)) {
return $this->valueFHIRType->getPattern();
}
return null;
}

/**
Expand All @@ -262,23 +267,7 @@ public function setPattern(string $pattern): Property
public function isCollection(): bool
{
$maxOccurs = $this->getMaxOccurs();
return PHPFHIR_UNLIMITED === $maxOccurs || 1 < $maxOccurs;
}

/**
* @return bool
*/
public function isRequired(): bool
{
return 1 <= $this->getMinOccurs();
}

/**
* @return bool
*/
public function unlimitedOccurrences(): bool
{
return PHPFHIR_UNLIMITED === $this->getMaxOccurs();
return null !== $maxOccurs && (PHPFHIR_UNLIMITED === $maxOccurs || 1 < $maxOccurs);
}

/**
Expand All @@ -302,7 +291,7 @@ public function setUse(PropertyUse $use): Property
/**
* @return string|null
*/
public function getRef(): ?string
public function getRef(): null|string
{
return $this->ref;
}
Expand All @@ -328,7 +317,7 @@ public function setRef(string $ref): Property
/**
* @return string|null
*/
public function getFixed(): ?string
public function getFixed(): null|string
{
return $this->fixed;
}
Expand All @@ -346,7 +335,7 @@ public function setFixed(?string $fixed): Property
/**
* @return string|null
*/
public function getNamespace(): ?string
public function getNamespace(): null|string
{
return $this->namespace;
}
Expand Down Expand Up @@ -409,26 +398,33 @@ public function buildValidationMap(): array
$map = [];
$memberOf = $this->getMemberOf();

if (null !== ($v = $this->getPattern())) {
$map[PHPFHIR_VALIDATION_PATTERN_NAME] = '/^' . addcslashes($v, '/\'') . '$/';
} elseif (null !== ($v = $memberOf->getPattern())) {
$map[PHPFHIR_VALIDATION_PATTERN_NAME] = '/^' . addcslashes($v, '/\'') . '$/';

$pattern = $this->getPattern();
if (null === $pattern) {
$pattern = $memberOf->getPattern();
}

if ($this->isCollection()) {
if (null !== ($v = $this->getMinOccurs()) && 0 !== $v) {
$map[PHPFHIR_VALIDATION_MIN_OCCURS_NAME] = $v;
}
if (null !== ($v = $this->getMaxOccurs()) && PHPFHIR_UNLIMITED !== $v) {
$map[PHPFHIR_VALIDATION_MAX_OCCURS_NAME] = $v;
}
if (null !== $pattern) {
$map[PHPFHIR_VALIDATION_PATTERN_NAME] = '/^' . addcslashes($pattern, '/\'') . '$/';
}

if (null !== ($v = $memberOf->getMinLength()) && 0 !== $v) {
$map[PHPFHIR_VALIDATION_MIN_LENGTH_NAME] = $v;
$minOccurs = $this->getMinOccurs();
$maxOccurs = $this->getMaxOccurs();
$minLength = $memberOf->getMinLength();
$maxlength = $memberOf->getMaxLength();

if (0 < $minOccurs) {
$map[PHPFHIR_VALIDATION_MIN_OCCURS_NAME] = $minOccurs;
}
if (null !== ($v = $memberOf->getMaxLength()) && PHPFHIR_UNLIMITED !== $v) {
$map[PHPFHIR_VALIDATION_MAX_LENGTH_NAME] = $v;
if (null !== $maxOccurs && PHPFHIR_UNLIMITED !== $maxOccurs && 1 < $maxOccurs) {
$map[PHPFHIR_VALIDATION_MAX_OCCURS_NAME] = $maxOccurs;
}

if (0 < $minLength) {
$map[PHPFHIR_VALIDATION_MIN_LENGTH_NAME] = $minLength;
}
if (PHPFHIR_UNLIMITED !== $maxlength) {
$map[PHPFHIR_VALIDATION_MAX_LENGTH_NAME] = $maxlength;
}

if ($memberOf->isEnumerated()) {
Expand All @@ -438,6 +434,8 @@ public function buildValidationMap(): array
}
}



return $map;
}

Expand Down
21 changes: 21 additions & 0 deletions src/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,19 @@ public function hasLocalProperties(): bool
return count($this->localProperties) > 0;
}

/**
* @return bool
*/
public function hasLocalPropertiesWithValidations(): bool
{
foreach($this->getlocalProperties()->allSortedPropertiesIterator() as $property) {
if ([] !== $property->buildValidationMap()) {
return true;
}
}
return false;
}

/**
* @return \DCarbone\PHPFHIR\Definition\Property[]
*/
Expand Down Expand Up @@ -587,6 +600,14 @@ public function getPrimitiveType(): PrimitiveType
return $this->primitiveType;
}

/**
* @return bool
*/
public function hasPrimitiveType(): bool
{
return isset($this->primitiveType);
}

/**
* @return array
*/
Expand Down
12 changes: 6 additions & 6 deletions template/core/classes/class_constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ final class <?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>
public const STRING_FALSE = 'false';

// Date and time formats
public const DATE_FORMAT_YEAR = 'Y';
public const DATE_FORMAT_YEAR_MONTH = 'Y-m';
public const DATE_FORMAT_YEAR_MONTH_DAY = 'Y-m-d';
public const DATE_FORMAT_YEAR_MONTH_DAY_TIME = 'Y-m-d\\TH:i:s\\.uP';
public const DATE_FORMAT_INSTANT = 'Y-m-d\\TH:i:s\\.uP';
public const TIME_FORMAT = 'H:i:s';
public const DATE_FORMAT_YEAR = '<?php echo PHPFHIR_DATE_FORMAT_YEAR; ?>';
public const DATE_FORMAT_YEAR_MONTH = '<?php echo PHPFHIR_DATE_FORMAT_YEAR_MONTH; ?>';
public const DATE_FORMAT_YEAR_MONTH_DAY = '<?php echo PHPFHIR_DATE_FORMAT_YEAR_MONTH_DAY; ?>';
public const DATE_FORMAT_YEAR_MONTH_DAY_TIME = '<?php echo PHPFHIR_DATE_FORMAT_YEAR_MONTH_DAY_TIME; ?>';
public const DATE_FORMAT_INSTANT = '<?php echo PHPFHIR_DATE_FORMAT_INSTANT; ?>';
public const TIME_FORMAT = '<?php echo PHPFHIR_TIME_FORMAT; ?>';

public const UNLIMITED = -1;

Expand Down
Loading

0 comments on commit a1eb69a

Please sign in to comment.