Skip to content

Commit

Permalink
propertly handle resource container type references
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 6, 2025
1 parent 0c24ac3 commit e480572
Show file tree
Hide file tree
Showing 23 changed files with 198 additions and 184 deletions.
2 changes: 1 addition & 1 deletion src/Builder/TypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static function buildPrimitiveType(Config $config,
$type = self::buildDefaultType($config, $version, $fhirName, $sxe, $sourceFilename);
$value = new Property($type, $sxe, $sourceFilename);
$value->setName(PHPFHIR_VALUE_PROPERTY_NAME);
$type->getLocalProperties()->addProperty($value);
$type->getProperties()->addProperty($value);
return $type;
}

Expand Down
8 changes: 2 additions & 6 deletions src/Utilities/ImportUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ public static function buildVersionTypeImports(Type $type): void
// immediately add self
$imports->addImport($type->getFullyQualifiedNamespace(false), $type->getClassName());

$typeNS = $type->getFullyQualifiedNamespace(false);
$configNS = $type->getConfig()->getFullyQualifiedName(false);

$typeKind = $type->getKind();

if (!$type->isAbstract()) {
Expand Down Expand Up @@ -106,15 +103,14 @@ public static function buildVersionTypeImports(Type $type): void
continue;
}

if ($ptk->isOneOf(TypeKindEnum::RESOURCE_CONTAINER, TypeKindEnum::RESOURCE_INLINE) &&
$typeNS !== $configNS) {
if ($ptk->isOneOf(TypeKindEnum::RESOURCE_CONTAINER, TypeKindEnum::RESOURCE_INLINE)) {
$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_CONSTANTS);
$imports->addVersionCoreFileImportsByName($type->getVersion(), PHPFHIR_INTERFACE_VERSION_CONTAINED_TYPE);
$imports->addVersionCoreFileImportsByName($type->getVersion(), PHPFHIR_CLASSNAME_VERSION_TYPE_MAP);
$imports->addVersionCoreFileImportsByName($type->getVersion(), PHPFHIR_CLASSNAME_VERSION);
} else {
if ($ptk === TypeKindEnum::PRIMITIVE_CONTAINER) {
$primType = $propertyType->getLocalProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME)->getValueFHIRType();
$primType = $propertyType->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME)->getValueFHIRType();
$imports->addImport($primType->getFullyQualifiedNamespace(false), $primType->getClassName());
}

Expand Down
4 changes: 2 additions & 2 deletions src/Utilities/TypeHintUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static function typeSetterTypeHint(Version $version, Type $type, bool $nu
$types = $nullable ? ['null'] : [];

if ($tk === TypeKindEnum::PRIMITIVE_CONTAINER) {
$pt = $type->getLocalProperties()->getProperty('value')->getValueFHIRType();
$pt = $type->getProperties()->getProperty('value')->getValueFHIRType();
$types = array_merge($types, $pt->getPrimitiveType()->getPHPReceiveValueTypeHints());
array_push(
$types,
Expand Down Expand Up @@ -256,7 +256,7 @@ public static function propertySetterTypeDoc(Version $version, Property $propert
$hintTypes = ['null'];

if ($ptk === TypeKindEnum::PRIMITIVE_CONTAINER) {
$ptp = $pt->getLocalProperties()->getProperty('value')->getValueFHIRType();
$ptp = $pt->getProperties()->getProperty('value')->getValueFHIRType();
$hintTypes = array_merge($hintTypes, $ptp->getPrimitiveType()->getPHPReceiveValueTypeHints());
array_push(
$hintTypes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ public static function decorate(
}

// add property to type
$type->getLocalProperties()->addProperty($property);
$type->getProperties()->addProperty($property);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,6 @@ public static function decorate(
}
}

$type->getLocalProperties()->addProperty($property);
$type->getProperties()->addProperty($property);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ public static function decorate(Config $config, Types $types, Type $type, Simple
}
}

$type->getLocalProperties()->addProperty($property);
$type->getProperties()->addProperty($property);
}
}
108 changes: 61 additions & 47 deletions src/Version/Definition/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@
* limitations under the License.
*/

use Countable;
use DCarbone\PHPFHIR\Config;
use DCarbone\PHPFHIR\Enum\TypeKindEnum;
use DCarbone\PHPFHIR\Version;

/**
* Class Properties
* @package DCarbone\PHPFHIR\Version\Definition\Type
*/
class Properties implements Countable
class Properties
{
/** @var \DCarbone\PHPFHIR\Version\Definition\Property[] */
private array $properties = [];
private array $_properties = [];
/** @var \DCarbone\PHPFHIR\Version\Definition\Property[] */
private array $_sortedProperties;

Expand All @@ -40,43 +37,33 @@ class Properties implements Countable
private array $_localSortedProperties;

/** @var bool */
private bool $cacheBuilt = false;

/** @var \DCarbone\PHPFHIR\Config */
private Config $config;

/** @var \DCarbone\PHPFHIR\Version */
private Version $version;
private bool $_cacheBuilt = false;

/** @var \DCarbone\PHPFHIR\Version\Definition\Type */
private Type $type;
private Type $_type;

/**
* @param \DCarbone\PHPFHIR\Config $config
* @param \DCarbone\PHPFHIR\Version $version
* @param \DCarbone\PHPFHIR\Version\Definition\Type $type
*/
public function __construct(Config $config, Version $version, Type $type)
public function __construct(Type $type)
{
$this->config = $config;
$this->version = $version;
$this->type = $type;
$this->_type = $type;
}

/**
* @return array
*/
public function __debugInfo()
{
return ['properties' => $this->properties];
return ['properties' => $this->_properties];
}

/**
* @return \DCarbone\PHPFHIR\Version\Definition\Type
*/
public function getType(): Type
{
return $this->type;
return $this->_type;
}

/**
Expand All @@ -95,14 +82,14 @@ public function addProperty(Property &$property): Properties
)
);
}
foreach ($this->properties as $current) {
foreach ($this->_properties as $current) {
if ($property === $current) {
return $this;
}
$cname = $current->getName();
$cref = $current->getRef();
if (null !== $pname && null !== $cname && $pname === $cname) {
$this->config->getLogger()->notice(
$this->_type->getConfig()->getLogger()->notice(
sprintf(
'Type "%s" already has Property "%s" (name), probably some duplicate definition nonsense. Keeping original.',
$this->getType()->getFHIRName(),
Expand All @@ -112,7 +99,7 @@ public function addProperty(Property &$property): Properties
$property = $current;
return $this;
} elseif (null !== $pref && null !== $cref && $cref === $pref) {
$this->config->getLogger()->notice(
$this->_type->getConfig()->getLogger()->notice(
sprintf(
'Type "%s" already has Property "%s" (ref), probably some duplicate definition nonsense. Keeping original.',
$this->getType()->getFHIRName(),
Expand All @@ -123,8 +110,8 @@ public function addProperty(Property &$property): Properties
return $this;
}
}
$this->properties[] = $property;
$this->cacheBuilt = false;
$this->_properties[] = $property;
$this->_cacheBuilt = false;
return $this;
}

Expand All @@ -134,7 +121,7 @@ public function addProperty(Property &$property): Properties
*/
public function getProperty(string $name): ?Property
{
foreach ($this->properties as $property) {
foreach ($this->_properties as $property) {
if ($property->getName() === $name) {
return $property;
}
Expand All @@ -151,14 +138,49 @@ public function hasProperty(string $name): bool
return null !== $this->getProperty($name);
}

/**
* @return int
*/
public function allPropertyCount(): int
{
return count($this->_properties);
}

/**
* @return int
*/
public function localPropertyCount(): int
{
$this->_buildCaches();
return count($this->_localProperties);
}

/**
* @return bool
*/
public function hasLocalProperties(): bool
{
return $this->localPropertyCount() > 0;
}

/**
* Returns an iterator containing all properties, including those inherited from parent types
*
* @return \DCarbone\PHPFHIR\Version\Definition\Property[]
*/
public function getAllPropertiesIterator(): iterable
{
return new \ArrayIterator($this->properties);
return new \ArrayIterator($this->_properties);
}

/**
* @return \Generator
*/
public function getAllPropertiesGenerator(): \Generator
{
foreach ($this->_properties as $p) {
yield $p;
}
}

/**
Expand All @@ -168,7 +190,7 @@ public function getAllPropertiesIterator(): iterable
*/
public function getAllSortedPropertiesIterator(): iterable
{
$this->_buildLocalCaches();
$this->_buildCaches();
return new \ArrayIterator($this->_sortedProperties);
}

Expand All @@ -179,7 +201,7 @@ public function getAllSortedPropertiesIterator(): iterable
*/
public function getIndexedLocalPropertiesIterator(): iterable
{
$this->_buildLocalCaches();
$this->_buildCaches();
return \SplFixedArray::fromArray($this->_localProperties, preserveKeys: false);
}

Expand All @@ -190,7 +212,7 @@ public function getIndexedLocalPropertiesIterator(): iterable
*/
public function getLocalPropertiesIterator(): iterable
{
$this->_buildLocalCaches();
$this->_buildCaches();
return new \ArrayIterator($this->_localProperties);
}

Expand All @@ -199,8 +221,8 @@ public function getLocalPropertiesIterator(): iterable
*/
public function getLocalPropertiesGenerator(): \Generator
{
$this->_buildLocalCaches();
foreach($this->_localProperties as $p) {
$this->_buildCaches();
foreach ($this->_localProperties as $p) {
yield $p;
}
}
Expand All @@ -212,7 +234,7 @@ public function getLocalPropertiesGenerator(): \Generator
*/
public function getLocalSortedPropertiesIterator(): iterable
{
$this->_buildLocalCaches();
$this->_buildCaches();
return new \ArrayIterator($this->_localSortedProperties);
}

Expand All @@ -235,18 +257,10 @@ public function getLocalPropertiesOfTypeKinds(bool $includeCollections, null|Typ
return new \ArrayIterator($out);
}

/**
* @return int
*/
public function count(): int
{
return count($this->properties);
}

private function _buildLocalCaches(): void
private function _buildCaches(): void
{
if (!$this->cacheBuilt) {
$this->_sortedProperties = $this->properties;
if (!$this->_cacheBuilt) {
$this->_sortedProperties = $this->_properties;
$this->_localProperties = [];
$this->_localSortedProperties = [];
usort(
Expand All @@ -255,7 +269,7 @@ function (Property $a, Property $b) {
return strnatcmp($a->getName(), $b->getName());
}
);
foreach ($this->properties as $property) {
foreach ($this->_properties as $property) {
if (!$property->isOverloaded()) {
$this->_localProperties[] = $property;
}
Expand All @@ -265,7 +279,7 @@ function (Property $a, Property $b) {
$this->_localSortedProperties[] = $property;
}
}
$this->cacheBuilt = true;
$this->_cacheBuilt = true;
}
}
}
Loading

0 comments on commit e480572

Please sign in to comment.